简体   繁体   中英

IOS:call did receive remote notification delegate when app is in background

My app contains push notification functionality and i have done the part of coding to receive push notification in my application.When my app enters to background i am receiving push notifications and when i click push notification button app come to foreground and calling did receive remote notification delegate method.I need to call the same delegate method when my app receives push notification in background but without opening the app by clicking push notification button on the top of the screen.I have written code as below.

   if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
 {
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
                                                    UIUserNotificationTypeBadge |
                                                    UIUserNotificationTypeSound);

   UIUserNotificationSettings *settings = [UIUserNotificationSettings 
  settingsForTypes:userNotificationTypes

   categories:nil];
    [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
}
else
{
    // Register for Push Notifications, if running iOS version < 8
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                     UIRemoteNotificationTypeAlert |
                                                     UIRemoteNotificationTypeSound)];
}

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    [application registerUserNotificationSettings:[UIUserNotificationSettings  



     settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge
   |UIUserNotificati
 onTypeSound categories:nil]];
  }

My final intension is to call did receive remote notification when app is in background also.

The didReceiveRemoteNotification delegate won't be invoked when the app is closed or in background.When you tap on the notification,it launches the app and the method will be invoked.You can do required action at this moment like decrementing the icon badge. This is the working flow of push notification in iOS .One more thing is when the app is closed and you are taping on notification,didReceiveRemoteNotification won't be invoked. ApplicationDidFinishLaunching will be called and you can identify there using launchoption dictionary whether its launched from notification

If App receives Notification in background & user will open app without tapping on notification than that notification will not be received at all.

To achieve this you need to try different thing.

Here is an idea that might help you:

When Notification pushed from the server & received successfully in App you can send message through Api that it received.

Now if in above case where notification not received in the app server will not get the successful receive message. So in this case it can be saved at server in pending notifications table.

Now once you open up the device 1 api will check if any pending notifications on the server & if found than will get that in response & after that it will be removed from the server.

Note: Its an idea that is tried earlier & working. You can improve it as per your requirement.

Hope it will work for you.

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