2017-05-24 77 views
0

如果您從通知中心點擊通知,您會在代碼中獲得通知信息,但如果用戶無法點擊該通知,並且只需點擊主屏幕上的應用程序圖標?當應用程序進入前臺時獲取待處理通知

例如,因爲通知有效載荷還包含徽章屬性。然後用戶可以點擊應用程序圖標,因爲徽章號碼閃亮,但在這種情況下,您如何管理代碼中的等待通知?

如果沒有此方法,那麼通知有效載荷中的badge屬性有點無用,不是。因爲如果用戶點擊帶有徽章號碼的圖標,他預計會發生某種情況,但應用程序無法對待處理的通知執行任何操作,因此無法執行任何操作。要麼?

+0

的[應用程序時,通過點擊應用程序圖標直接啓動沒有得到推送通知數據]可能的複製(https://stackoverflow.com/questions/33471064/push-notification-data-not-getting-when-app -launched-直接-通過單擊應用內-IC) – Bastian

回答

1

似乎getDeliveredNotifications方法允許這樣做。

- (void)applicationWillEnterForeground:(UIApplication *)application { 
    NSLog(@"msg applicationWillEnterForeground"); 
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
    [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) { 
     NSLog(@"msg getDeliveredNotificationsWithCompletionHandler count %lu", [notifications count]); 

     for (UNNotification* notification in notifications) { 
      // do something with object 
      NSLog(@"msg noti %@", notification.request); 
     } 

    }]; 
#endif 
} 
相關問題