打开应用程序时APNS问题

时间:2014-05-29 06:02:39

标签: iphone objective-c apple-push-notifications

当应用关闭(不在后台运行)且手机收到APNS时,didReceiveRemoteNotification不会被调用。

如果应用未在手机的后台运行,则会收到一条APNS消息,单击横幅或弹出通知会打开该应用,但不会调用didReceiveRemoteNotification代理

感谢。

1 个答案:

答案 0 :(得分:1)

didReceiveRemoteNotification 仅在您使用应用程序时(当应用程序运行时)被调用,并且如果您想在用户点击横幅通知时执行某些特定操作,那么在Appdelegate&#39; s < strong> - (BOOL)应用程序:application didFinishLaunchingWithOptions:launchOptions 检查它是否通过通知

// Handle notification when app is quit
NSDictionary *notification = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];

if (notification != nil) {

    [self handleNotificationsForApplication:application notification:notification];
}

并阻止方法 handleNotificationsForApplication:应用程序通知:通知

- (void)handleNotificationsForApplication:(UIApplication *)application notification:(NSDictionary *)userInfo {

NSDictionary *data = [[userInfo objectForKey:@"aps"] objectForKey:@"data"];
NSString *type = [data objectForKey:@"type"];

if ([type isEqualToString:@"app_update"]) {

   // Show alert to do the update
}
else if ([type isEqualToString:@"new_chat"]) {

    imageData = data;

    if (application.applicationState != UIApplicationStateActive) {

        // Open chat view
    }
    else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:APP_NAME
                                                        message:[[userInfo valueForKey:@"aps"] valueForKey:@"alert"]
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        [alert setTag:2];
        [alert show];
    }
}
}