简体   繁体   中英

iOS doesn't receive Push notification when the app is being killed

I have found similar questions on Stack Overflow, but none of them have cleared the point.

I am using Firebase Cloud Messaging for sending push notifications in my app. I am storing the messages received in a local database. When my app is active or in the background, my app is able to receive the notifications (delegate methods called properly) but if the app is being forced quit or not in the memory then also the device is receiving the notifications and sat in notification center but the none of the delegate methods called when the app is being launched by an icon. If a user clicks on Message in the notification center, then the app gets launched, but only the message clicked on is being received and not all of them (in the case of multiple notices have been received). According to Apple - The system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again .

But even if the user launched the app still not receive the notifications which were received and sat in the notification center.

Here are the points followed by the app:

  • My app has no VoIP functionality.
  • Content-available has been set to 1.
  • Has enabled to receive background remote notifications.
  • All notifications sent has been received and displayed in the notification center.

{
    aps =     {
        alert =         {
            body = "Push Notification Test Message";
            title = Push Notification;
        };
        badge = 1;
        "content-available" = 1;
        sound = default;
    };
    "gcm.message_id" = "0:1499340350307980%361a2e5b361a2e5b";
    m = "Push Notification Test Message";
    tag = m;
}
  1. If the app is being forced quit or not in memory then your app will not receive silent notifications(ie Content-available set to 1). Only push notifications are received in above conditions.

  2. If user clicks on Message in notification center, then app gets launched and only that clicked the message will be received in delegate methods.You cannot access all the messages in the notification tray.

Your app should not rely on the delivery of push notifications. Delivery of push notifications is not guaranteed, as you have found.

Even if your app isn't terminated you may not receive all notifications; For example if the device is in airplane mode and multiple notifications are sent, only the last one is delivered when the device comes back online.

Your app should check with the server for new messages when it is launched or when a notification is received in order to "catch up".

You can also consider including a message payload in your silent notifications. This will be displayed to the user if your app is terminated to prompt them to open your app.

There is no way to get push notifications for the application if app is not running. This is restriction . You are only can get and clear local notifications. So the another way of resolving your problem is saving your notification on the backend when you send it. Then after launching app , you can get notifications list from the server , and match it with id . You can send any parameters that you want in notification playload.

You need to handle method for push notification when the application is open from a tap on any notification. As soon as you tap on notification application will awake and lunch it.

In didFinishLaunchingWithOptions, you need to put below condition:

 if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
    }

It also called below method when notification is tapped:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM