应用关闭后如何处理推送有效负载?

时间:2012-06-11 11:31:55

标签: iphone ios push-notification

我正在向我的用户发送推送有效负载,其中包含以下内容:

{"aps": {"alert": "Go To Google", "sound": "Default","url":"http://www.google.com"}}
当pp正在运行但在后台时,一切顺利。 如果我收到推送并且应用程序已关闭,我打开它并且没有任何反应。 我正在尝试重定向到有效负载中的此URL。 当应用程序从后台运行时它再次运行良好。

这是到目前为止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];    
  }

需要一些帮助。

1 个答案:

答案 0 :(得分:10)

此外,将以下内容添加到您的代码中:

- (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;
}