How do I tell if my iPhone app is running when a Push Notification is received?

前端 未结 5 492
栀梦
栀梦 2020-12-04 14:39

I am sending Push Notifications to my iPhone app, and I\'d like a different set of instructions to execute depending on whether the app is already launched or not. I\'m new

5条回答
  •  Happy的楠姐
    2020-12-04 15:06

    Here's the more appropriate way of handling active/inactive state of the app.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {   
            // check for the app state
            UIApplicationState state = [application applicationState];
    
            if (state == UIApplicationStateActive) {
                //the app is in the foreground, so here you do your stuff since the OS does not do it for you
                //navigate the "aps" dictionary looking for "loc-args" and "loc-key", for example, or your personal payload)
            }
    
        application.applicationIconBadgeNumber = 0;
    }
    

    didReceiveRemoteNotification: is called when the app is running, yes, but when it is suspended, the iOS takes care of putting up the badge, etc. If the app is in the foreground, the OS does nothing, and just calls your didReceiveRemoteNotification:.

提交回复
热议问题