Handling Push Notifications when App is Terminated

后端 未结 7 628
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 03:55

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

7条回答
  •  一个人的身影
    2020-11-30 04:27

    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.

提交回复
热议问题