how to handle push payload when the app closed?

2019-05-03 23:07发布

问题:

i am sending push payload to my users with the follow content :

{"aps": {"alert": "Go To Google", "sound": "Default","url":"http://www.google.com"}}

everything goes well when the pp is running but in the background. if i am receiving the push and the app is closed i am open it and nothing happen. i am trying to redirect to this url in the payload. again when the app is running from the background it goes well.

this is the implementation so far AppDelegate.m:

-(void)Redirect:(NSString*)url{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 
    NSLog(@"%@",url);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    
    RedirectedUri = [[userInfo objectForKey:@"aps"] objectForKey:@"url"];
    NSLog(@"%@",RedirectedUri);
    [self Redirect:RedirectedUri];    
  }

need some help please.

回答1:

Additionally, add the following to your code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self applicationDidFinishLaunching:application];

    if (launchOptions != nil)
    {
        NSDictionary* dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (dictionary != nil)
        {
            RedirectedUri = [[dictionary objectForKey:@"aps"] objectForKey:@"url"];
            [self Redirect:RedirectedUri];
        }
    }
    return YES;
}