“ getDeliveredNotificationsWithCompletionHandler”使通知托盘计数为零:iOS12.1

时间:2018-11-28 17:43:46

标签: objective-c push-notification nsnotificationcenter unnotificationrequest

遵循以下步骤

  
      
  1. 打开应用
  2.   
  3. 使用锁定按钮锁定iPad
  4.   
  5. 然后从后端发送通知-设备在通知托盘中接收通知
  6.   
  7. 然后使用锁定按钮解锁iPad由于我们尚未按下主屏幕按钮,因此该应用程序仍处于打开状态。
  8.   
  9. 现在当我们执行“ getDeliveredNotificationsWithCompletionHandler”时会发出通知   托盘计数为零-它应该给出1个通知为   收到。 [[当我将我的应用程序放到背景上然后又让我感到困惑时,它也不会给我正确的计数]
  10.   

这是我的代码:

- (void)applicationDidBecomeActive:(UIApplication *)application {

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {

        NSLog(@"getDeliveredNotificationsWithCompletionHandler count %lu", [notifications count]);

        for (UNNotification* notification in notifications) {
            // do something with object
            [self handlePushNotification:notification.request.content.userInfo];
        }

        if(notifications.count) {
            [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];
        }

    }];
}

1 个答案:

答案 0 :(得分:0)

此问题来自服务器端的通知负载-如下所示

{
    aps =     {
        alert =         {
            body = "";
            title = "";
        };
        badge = 1;
    };

}
  

**但是,当我将徽章的值设置为“ 0”时,我从getDeliveredNotificationsWithCompletionHandler **获得了计数

  {
        aps =     {
            alert =         {
                body = "";
                title = "";
            };
            badge = 0;
        };

    }