How to respond to push notification view if app is already running in the background

后端 未结 2 609
北恋
北恋 2020-12-04 07:42

I have something fairly simple I want to do. I attach a custom piece of data to some push notifications that I handle in

-(BOOL)application:(UIApplication *)         


        
相关标签:
2条回答
  • 2020-12-04 08:11

    Check out application:didReceiveRemoteNotification:fetchCompletionHandler: in iOS 7 and later.


    The method application:didReceiveRemoteNotification: is called if your app is running in the foreground. It also is called if your app is running in the background and the user engages with your push notification (thus making your app active).

    So, the real question is how to determine if the app was in the foreground or if it was made active by the user engaging with your push notification.

    It looks like this answer to the question didReceiveRemoteNotification when in background has the key:

    You can tell whether your app was just brought to the foreground or not in application:didReceiveRemoteNotification: using this bit of code:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    {
        if ( application.applicationState == UIApplicationStateActive )
            // app was already in the foreground
        else
            // app was just brought from background to foreground
        ...
    }
    
    0 讨论(0)
  • 2020-12-04 08:14

    To detect if the app was activated by remote notication, try this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (userInfo == NULL)
        {
            NSLog(@"didFinishLaunchingWithOptions user startup userinfo: %@", userInfo);
        }
        else
        {
            NSLog(@"didFinishLaunchingWithOptions notification startup userinfo: %@", userInfo);
        }
    }
    
    0 讨论(0)
提交回复
热议问题