应用程序未运行时不会收到远程通知

时间:2014-04-09 12:41:20

标签: ios objective-c push-notification parse-platform

我的应用在未运行时未收到推送通知。 我正在尝试处理作为JSON发送的远程通知,并使用来自给定JSON的数据更新我的应用程序中的数据。 当应用处于活动状态或后台时,一切进展顺利。 但是,当应用程序未运行时,应用程序仅在我通过点击通知打开我的应用程序时处理通知,但不是在我通过点击图标打开应用程序时。 以下是appDelegate类的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [Parse setApplicationId:appId
              clientKey:clKey];

   if (application.applicationState != UIApplicationStateBackground) {

     BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)];
     BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)];
     BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
     if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) {
        [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
     }
   }
   [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
                                                UIRemoteNotificationTypeAlert|
                                                UIRemoteNotificationTypeSound|
                        UIRemoteNotificationTypeNewsstandContentAvailability];
   NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

   [self processPushNotification:notificationPayload foreground:YES];
   return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  TFLog(@"didRegisterForRemoteNotificationsWithDeviceToken");
// Store the deviceToken in the current installation and save it to Parse.
  PFInstallation *currentInstallation = [PFInstallation currentInstallation];
  [currentInstallation setDeviceTokenFromData:deviceToken];
  [currentInstallation saveInBackground];

  TFLog(@"deviceToken: %@, currentInstallation.badge: %ld", currentInstallation.deviceToken, (long)currentInstallation.badge);

  TFLog(@"deviceToken: %@, deviceType: %@", currentInstallation.deviceToken, currentInstallation.deviceType);
  TFLog(@"installationId: %@", currentInstallation.installationId);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  TFLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
  if (error.code == 3010) {
    TFLog(@"Push notifications are not supported in the iOS Simulator.");
  } else {
    // show some alert or otherwise handle the failure to register.
    TFLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);
  }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
  TFLog(@"%@", userInfo);
  [PFPush handlePush:userInfo];
  [self processPushNotification:userInfo foreground:YES];
  [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  TFLog(@"didReceiveRemoteNotification2");
  [self processPushNotification:userInfo foreground:YES];
  [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];
}

结果,app在所有状态下都会收到远程通知,但不运行时除外。 我错过了什么?

2 个答案:

答案 0 :(得分:2)

你错过了Local and Push Notification Programming Guide所说的位 -

  

如果点击操作按钮(在运行iOS的设备上),则系统   启动应用程序,应用程序调用其代理人   application:didFinishLaunchingWithOptions:方法(如果实现);它   传递通知有效载荷(用于远程通知)或   本地通知对象(用于本地通知)。

     

如果在运行iOS的设备上点击应用程序图标,则   应用程序调用相同的方法,但不提供任何信息   通知

此外,Apple的这条说明 -

  

重要提示:提交通知是“尽力而为”,而不是   保证。它不是为了向您的应用提供数据,而只是为了   通知用户有新数据可用。

如果您的应用程序是从应用程序图标而不是通知启动的,则需要检查更新的内容,而不考虑可能已收到的任何推送通知。这使应用程序在从通知打开时以及从应用程序图标打开时的行为方式不同。

例如,Facebook应用程序在从通知警报启动时直接打开通知中的项目,但不会从应用程序图标启动时打开 - 这是正确的"从用户的角度来看行为。如果我与通知进行交互,那么我对其内容感兴趣。如果我从图标启动应用程序,那么我只想使用该应用程序 - 如果需要,我可以在应用程序中访问通知。

答案 1 :(得分:0)

当您通过明确点按应用图标启动应用时,无法获得有关推送通知JSON有效负载的信息。

这符合Apple的设计。当您从任何推送通知操作打开应用程序时,您可以在application: didFinishLaunchingWithOptions:委托方法中检索推送通知。通常,您在launchOptions字典中查找UIApplicationLaunchOptionsRemoteNotificationKey键。

但是,当您通过明确点击应用程序图标打开应用程序时,虽然仍然会调用application: didFinishLaunchingWithOptions:委托,但UIApplicationLaunchOptionsRemoteNotificationKey键将返回nil

相关问题