简体   繁体   中英

iOS: Handling Remote (push) Notifications

I'm trying to handle all possible cases with remote notifications. I'm ok when app in foreground - didReceiveRemoteNotification is called. The problem is when app in background state, and I receive push notification. Nothing is called. how to let user know that he has new remote notification when app come back to foreground ?

The only way for you to intercept a push notification is when the user tap the notify in the notification center (or when slide the app icon from the lock screen).

In this case before the app go in foreground the didFinishLaunchingWithOptions method in the app delegate is called. You should use the NSDictionary launchOptions to determine if the app has been launched from notification center or by tapping the icon (the normal use)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSDictionary *pushDic = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
    if (pushDic != nil) {
        NSLog(@"Notification");
    }
    else {

    }
}

Just have a look into the programming guide:

If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate's application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).

Of course, if your app is in background, there will be nothing called...

If your app is not launched (not even suspended in background), the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

will contain the notification payload (key UIApplicationLaunchOptionsRemoteNotificationKey):

NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

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