1

Using FCM for push notifications in iOS 10:

This is the snipper that's getting called after pushing a notification via our own API:

- (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

    [[FIRMessaging  messaging] appDidReceiveMessage:userInfo];
    if (userInfo[kGCMMessageIDKey]) {
        NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
    }

    completionHandler(UIBackgroundFetchResultNewData);
    if(application.applicationState == UIApplicationStateBackground){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"Bacgkround"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];

    }
    else if(application.applicationState == UIApplicationStateActive){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"Active"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];
    }

    else if(application.applicationState == UIApplicationStateInactive){
        [[TWMessageBarManager sharedInstance] showMessageWithTitle:@"InActive"
                                                       description:@"Wassup"
                                                              type:TWMessageBarMessageTypeSuccess callback:^{


                                                              }];
    } 
}

When the app is active, above method gets called and UIApplicationStateActive case gets executed and I am showing a pop-up using a 3rd party library. When I press the Home button and push a notification, above method gets called and UIApplicationStateBackground gets executed but I am not sure how do I show a notification in the form of a banner?

Is this the method where I have to handle the notifications? If yes, how do I handle Background and Inactive states?

AL.
  • 36,815
  • 10
  • 142
  • 281
Vamshi Krishna
  • 979
  • 9
  • 19

2 Answers2

1

you just have to put notification body in payload you receive from server, if you don't add it, iOS consider it as silent push notification and will not show notification banner.e.g

{  
   "data":{  
  "title":"mytitle",
  "body":"mybody",
},
"notification":{  
  "title":"mytitle",
  "body":"mybody",
 },
}

when user clicks on notification banner in what ever state application is, you will get its call back method triggered as provided by firebase.e.g.

// [START receive_message]
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

etc.

NOTE: no need to use any third party library to show notification banners as all this is controlled by iOS.

amagain
  • 2,042
  • 2
  • 20
  • 36
0

I do not know much about TWMessageBarManager but i can tell you can not user any lib or anything you've made custom to show notification while your app is not in foreground. If your app is in background and you receive a push notification the notification banner will be shown itself by the iOS

Pratik Jamariya
  • 810
  • 1
  • 10
  • 35
  • But I need to write something in UIApplicationStateBackground case right? – Vamshi Krishna Apr 27 '17 at 05:44
  • i dont think so, i've never done anything for handling push notification for `UIApplicationStateBackground` iOS will handle it by itself – Pratik Jamariya Apr 27 '17 at 05:48
  • In that case, - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {} method is not getting called in my case! – Vamshi Krishna Apr 27 '17 at 05:49
  • it might happen if your app is suspended or its terminated, because of that none of your delegate/overridden method will be called, iOS will show a notification banner or alert based on permissions you've asked – Pratik Jamariya Apr 27 '17 at 05:52
  • I've tried using APNS testers available online and everything works fine but in FCM, I am not receiving notifications in background/ inactive. All the permissions are set correct. – Vamshi Krishna Apr 27 '17 at 05:54
  • can you post your notification payload from FCM? – Pratik Jamariya Apr 27 '17 at 05:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142788/discussion-between-pratik-jamariya-and-vamshi-krishna). – Pratik Jamariya Apr 27 '17 at 06:00