1

I used the ios-quickstart example in my iOS app.

When I send a push-notification to my device throught the firebase console, the first one is sent and marked as succes but never arrives.

Any notifications sent after the first one return the following response.

{
  "multicast_id": 8642270861911984600,
  "success": 0,
  "failure": 1,
  "canonical_ids": 0,
  "results": [
    {
      "error": "NotRegistered"
    }
  ]
}

So for some reason the FIRMessaging token becomes unregistered but I can't figure out why.

I have already tried without succes:

  • Created a new certificate with apple developer console
  • Removed and re-added all the code
  • Enabled and Disabled method swizzling (FirebaseAppDelegateProxyEnabled)

Edit:

To get the above respons I used PostMan with the FCM REST API. Using the REST API has the same results, first push gives a normal response, any push after that gives the NotRegistered error

When using the console I get the following: Firebase Console Example

The app delegate is exactly the same as the quickstart-ios example except for putting the registerForPushNotifications: function, which is called in -application: didFinishLaunchingWithOptions::

    -(void) registerForPushNotifications:(UIApplication*) application
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        // Register for remote notifications
        if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
            // iOS 7.1 or earlier. Disable the deprecation warnings.
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIRemoteNotificationType allNotificationTypes =
            (UIRemoteNotificationTypeSound |
             UIRemoteNotificationTypeAlert |
             UIRemoteNotificationTypeBadge);
            [application registerForRemoteNotificationTypes:allNotificationTypes];
    #pragma clang diagnostic pop
        } else {
            // iOS 8 or later
            // [START register_for_notifications]
            if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
                UIUserNotificationType allNotificationTypes =
                (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
                UIUserNotificationSettings *settings =
                [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
            } else {
                // iOS 10 or later
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
                UNAuthorizationOptions authOptions =
                UNAuthorizationOptionAlert
                | UNAuthorizationOptionSound
                | UNAuthorizationOptionBadge;
                [[UNUserNotificationCenter currentNotificationCenter]
                 requestAuthorizationWithOptions:authOptions
                 completionHandler:^(BOOL granted, NSError * _Nullable error) {
                 }
                 ];

                // For iOS 10 display notification (sent via APNS)
                [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
                // For iOS 10 data message (sent via FCM)
                [[FIRMessaging messaging] setRemoteMessageDelegate:self];
    #endif
            }

            [[UIApplication sharedApplication] registerForRemoteNotifications];
            // [END register_for_notifications]
        }
    }

    -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        if (notificationSettings != UIUserNotificationTypeNone)
        {

            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }

    -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
    //    //source: http://stackoverflow.com/a/9372848/5309449
        const unsigned *tokenBytes = [deviceToken bytes];
        NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        NSLog(@"hexToken: %@",hexToken);


        //[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];



    }

    -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"%s",__PRETTY_FUNCTION__);
        NSLog(@"Failed to register: %@",error);
    }


    // [START refresh_token]
    - (void)tokenRefreshNotification:(NSNotification *)notification {
        // Note that this callback will be fired everytime a new token is generated, including the first
        // time. So if you need to retrieve the token as soon as it is available this is where that
        // should be done.
        NSString *refreshedToken = [[FIRInstanceID instanceID] token];
        NSLog(@"InstanceID token: %@", refreshedToken);

        // Connect to FCM since connection may have failed when attempted before having a token.
        [self connectToFcm];

        // TODO: If necessary send token to application server.

        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:refreshedToken forKey:@"FIRMessagingToken"];
    }
    // [END refresh_token]

    // [START connect_to_fcm]
    - (void)connectToFcm {
        [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
            if (error != nil) {
                NSLog(@"Unable to connect to FCM. %@", error);
            } else {
                NSLog(@"Connected to FCM.");
            }
        }];
    }
    // [END connect_to_fcm]

    // [START receive_message]
    // To receive notifications for iOS 9 and below.
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // Print message ID.
        NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

        // Print full message.
        NSLog(@"%@", userInfo);
    }
    // [END receive_message]

    // [START ios_10_message_handling]
    // Receive displayed notifications for iOS 10 devices.
    #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
           willPresentNotification:(UNNotification *)notification
             withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
        // Print message ID.
        NSDictionary *userInfo = notification.request.content.userInfo;
        NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);

        // Print full message.
        NSLog(@"%@", userInfo);
    }

    // Receive data message on iOS 10 devices.
    - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
        // Print full message
        NSLog(@"%@", [remoteMessage appData]);
    }
    #endif
    // [END ios_10_message_handling]

Something else interesting I've found is that, while the apple notification permission alert is showing, I can send notifications and I see them arrive in my debug console.

After I accept notification permissions they no longer arrive...

Venomy
  • 2,076
  • 15
  • 25
  • 1
    Hi Venomy, you mentioned that you are sending from the Firebase console but then show an error that looks like it comes from the REST API. Could you clarify which one you are using? Also including the code of your appdelegate where you generate the token could be helpful. – Arthur Thompson Sep 28 '16 at 14:43
  • Yes, Sorry for the confusion. I've added more info on the problem – Venomy Sep 29 '16 at 07:37
  • 1
    When you disabled method swizzling did you uncomment the apns token regestration line `//[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];`? – light Sep 29 '16 at 15:41
  • Did you add the APNs certificate to the Firebase console? This can happen when the APNs is not correctly set up. – Arthur Thompson Sep 29 '16 at 22:39
  • @light Yes I did, still the same end-result – Venomy Sep 30 '16 at 07:29
  • @ArthurThompson Yes I added the certificate, same result. I even re-created the APNs certificate in the apple developer console and re-added it. Still no success :( – Venomy Sep 30 '16 at 07:30

1 Answers1

1

There appeared to be an issue with the entitlements.

I also recently upgraded to XCode 8 which might have broken something.

There was a 'Fix this issue' button on this screen:

enter image description here

Venomy
  • 2,076
  • 15
  • 25