0

I am trying to use Firebase for push notification. i have successfully installed the Firebase And Firebase messaging via Cocoapods.

I used the below code

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FIRApp configure];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshCallback:) name:kFIRInstanceIDTokenRefreshNotification object:nil];


    UIUserNotificationType allNotificationsType = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationsType categories:nil];
    [application registerUserNotificationSettings:settings];

    [application isRegisteredForRemoteNotifications];

    return YES;
}


 -(void)applicationDidEnterBackground:(UIApplication *)application {

    [[FIRMessaging messaging] disconnect];
    NSLog(@"Disconnect from FCM");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
     [self connectToFirebase];
}

- (void) application:(UIApplication *) application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:( void (^)(UIBackgroundFetchResult))completionHandler{

    // Pring The Message Id
    NSLog(@"Mesage Id : %@",userInfo[@"gcm.message_id"]);

    // Print Full Message
    NSLog(@"%@",userInfo);
}

#pragma mark -- Custom Firebase code


- (void)tokenRefreshCallback:(NSNotification *) notification{
    NSString *refreshedToken = [[FIRInstanceID instanceID] token];
    NSLog(@"IstanceID token: %@", refreshedToken);

    // Connect To FCM since connection may have failed when attempt before having a token
    [self connectToFirebase];
}

-(void) connectToFirebase{

    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error)
     {
         if ( error != nil)
         {
             NSLog(@"Unable to Connect To FCM. %@",error);
         }
         else
         {
             NSLog((@"Connected To FCM"));
         }
     }];
}

When i run the above code using my iPhone connected with Xcode i am facing the following issues when i send a message from Firebase Console

1). when the app is in foreground (active), Log shows the below message

Mesage Id : (null){
    CustommData = "First Message";
    "collapse_key" = "abcd.iospush.FireBasePush";
    from = 555551391562;
    notification =     {
        badge = 4;
        body = "Test Message";
        e = 1;
        sound = default;
        sound2 = default;
    };
}

Notice Message Id is null.

2). My phone doesn't show any notification in Notification Centre whether App is in Foreground , Background or Closed

I want user to receive push notifications when App is in Background , Foreground or Closed

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hya
  • 189
  • 1
  • 2
  • 10

3 Answers3

1

You must call [[UIApplication sharedApplication] registerForRemoteNotifications]; to properly register for remote notifications.

Configuring Remote Notifications For Background Usage

Have you configured your app's background modes to accept remote notifications? You can do this by clicking: Project Name -> Capabilities -> Background Modes

Turn this on and then tick the box beside Remote Notifications as seen in the screenshot below.

enter image description here

Rhuari Glen
  • 497
  • 2
  • 5
  • The "Remote notifications" background only ensures that your app processes the remote notification as soon as it arrives if your app is in the background, and will launch your app if it isn't running, rather than having to wait for the user to tap the notification. It requires the user to have iOS 7 or higher. Note that local notifications are not processed in the background. – Marcus Adams Aug 30 '16 at 12:21
  • @RhuariGlen i Configuring Remote Notifications For Background Usage as you said and used [[UIApplication sharedApplication] registerForRemoteNotifications]; as well and t works fine but it is displaying warning Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called. – Hya Aug 30 '16 at 15:51
  • @Hya Once you have finished processing the data within your notification, you must call the completion handler and pass in the `UIBackgroundFetchResult` that best describes the result of your download operation. Please refer to the [apple docs](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification:fetchCompletionHandler:) for more information. – Rhuari Glen Aug 30 '16 at 16:32
  • @Hya Can you also mark my answer as correct if it helped you, please :) – Rhuari Glen Aug 30 '16 at 16:35
0

You should be calling (iOS 8+) registerForRemoteNotifications, not isRegisteredForRemoteNotifications.

Otherwise, you'll never get "high priority" messages from the Firebase server, delivered natively using APNS.

Please note that when you're running in XCode, you are running in sandbox mode, so be sure to register with the proper parameters.

Try sending it in "high priority" and see if your payload is in the proper APNS format:

{
    "aps" : {
        "alert" : "You got your emails.",
        "badge" : 9,
        "sound" : "bingbong.aiff"
    },
}

Note that your payload may have = instead of :, which is acceptable.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • i used registerForRemoteNotifications and it worked but with warning Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called. how can we fix this – Hya Aug 30 '16 at 16:22
  • You'll need to implement `didReceiveRemoteNotification:`. That will get triggered when the app gets a native (APNS) notification in the background and the user taps on it or when the app receives the notification while it's in the foreground. – Marcus Adams Aug 30 '16 at 18:28
0

you need to create an App ID in your developer account that has the push notification entitlement enabled. Luckily, Xcode has a simple way to do this. Go to App Settings -> Capabilities and flip the switch for Push Notifications to On. After some loading, it should look like this: push notificationsenter image description here

Also check p12 certificate is set for Firebase as it uses .p12 certificate for push notifications.

Aadil Ali
  • 351
  • 1
  • 15
  • Please use this method : [application registerForRemoteNotifications]; after [application registerUserNotificationSettings:settings]; – Aadil Ali Aug 31 '16 at 12:33