Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Push Notifications when App is Terminated

When my App is not running and receives a Push Notification, if I click on that notification, the App is launched - but then it doesn't prompt the user with the Alert-View I set up, asking them whether they want to view the Notification's contents or not. It just launches, and sits there.

The Push Notifications do work perfectly when the App is running - either as the Active app or while in the background - but nothing works correctly when the app is not running.

I tried logging-out the launchOptions NSDictionary in application: didFinishLaunchingWithOptions: to see what load its bringing - but it comes up as "(null)". So It basically contains nothing - which doesn't make sense cause shouldn't it contain the Notification's load?

Anybody have any ideas how to make Push Notifications work when they arrive while the App was NOT running?

I mean how to handle the Push notifications when the App is in not running state. What if, if you receive many notifications & you did not open the app, neither did you tap the system's notification panel. How are you preserving those push for a later retrieval.

like image 410
Hasmukh D. Avatar asked Jan 28 '16 10:01

Hasmukh D.


People also ask

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

Does an app need to be open for push notifications?

For app publishers, push notifications are a way to speak directly to the user. They don't get caught in spam filters or forgotten in an inbox: as a result, push click-through rates can be twice as high as email. They can remind users to use an app, even if it isn't open.


1 Answers

1) When application is running in background and When application is running in foreground application:didReceiveRemoteNotification: method will called as below.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {     if (application.applicationState == UIApplicationStateInactive)     {         // opened from a push notification when the app was on background         NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);     }     else if(application.applicationState == UIApplicationStateActive)     {         // a push notification when the app is running. So that you can display an alert and push in any view         NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);     } } 

2) When application is not launched (close) then application:didFinishedLaunchingWithOptions method will called.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     if (launchOptions != nil)     {         // opened from a push notification when the app is closed         NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];         if (userInfo != nil)         {              NSLog(@"userInfo->%@", [userInfo objectForKey:@"aps"]);         }     }     else     {         // opened app without a push notification.     } } 

3) There is no way to remove a specific notification as of. The way to remove all the notifications from your app so they don't show in the Notification Center when the user opens the app from one of them, is to set the app badge to 0.

like image 70
Rahul Patel Avatar answered Sep 28 '22 00:09

Rahul Patel