簡體   English   中英

應用程序未運行時未收到遠程通知

[英]App doesn't receive remote notification when it is not running

我的應用程序在未運行時不會收到推送通知。 我正在嘗試處理以 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];
}

結果,應用程序在所有狀態下都會收到遠程通知,除非它沒有運行。 我錯過了什么?

您錯過了本地和推送通知編程指南中的部分內容 -

如果操作按鈕被點擊(在運行 iOS 的設備上),系統啟動應用程序,應用程序調用其委托的 application:didFinishLaunchingWithOptions: 方法(如果已實現); 它傳入通知負載(用於遠程通知)或本地通知對象(用於本地通知)。

如果在運行 iOS 的設備上點擊應用程序圖標,應用程序調用相同的方法,但不提供有關通知的信息

此外,Apple 的這篇筆記 -

重要提示:通知的傳遞是“盡力而為”,不能保證。 它不是為了向您的應用程序提供數據,只是為了通知用戶有新數據可用。

如果您的應用程序是從應用程序圖標而不是通知啟動的,則您需要獨立於可能已收到的任何推送通知檢查更新的內容。 這使應用程序在從通知打開時和從應用程序圖標打開時的行為不同。

例如,當從通知警報啟動時,Facebook 應用程序直接打開通知中的項目,而不是從應用程序圖標啟動時 - 從用戶的角度來看,這是“正確”的行為。 如果我與通知交互,那么我對它的內容感興趣。 如果我從圖標啟動應用程序,那么我只想使用該應用程序 - 如果需要,我可以訪問應用程序中的通知。

當您通過顯式點擊應用程序圖標啟動應用程序時,您無法獲取有關推送通知 JSON 有效負載的信息。

這是按照蘋果的設計。 當您從任何推送通知操作打開應用程序時,您可以在application: didFinishLaunchingWithOptions:委托方法中檢索推送通知。 通常,您會在 launchOptions 字典中查找UIApplicationLaunchOptionsRemoteNotificationKey鍵。

但是,當您通過顯式點擊應用程序圖標打開應用程序時,盡管仍會調用application: didFinishLaunchingWithOptions:委托,但UIApplicationLaunchOptionsRemoteNotificationKey鍵將返回nil

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM