当应用程序在后台时接收推送通知

时间:2020-09-03 12:34:21

标签: ios notifications apple-push-notifications ios13

我知道这个问题在SO中已经问了很多遍了,我已经看到了所有这些。当我的应用程序位于预期的前台时,它会收到推送通知,但是当它推送到后台时,什么也没有发生。我尝试了其他问题中提到的所有方法,但均无济于事。我正在iOS 13上对此进行测试。这是我在代码中所做的。请求权限

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        
        switch (settings.authorizationStatus)
        {
            // User hasn't accepted or rejected permissions yet. This block shows the allow/deny dialog
            case UNAuthorizationStatusNotDetermined:
            {
                center.delegate = self;
                [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error)
                 {
                     if(granted)
                     {
                         dispatch_async(dispatch_get_main_queue(), ^{
                             [[UIApplication sharedApplication] registerForRemoteNotifications];
                         });
                     }
                 }
           }
     }
}

收到通知后

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    os_log(OS_LOG_DEFAULT, "Received Notification");
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [self application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result){}];
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    
    os_log(OS_LOG_DEFAULT, "Received Notification willPresentNotification");
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center :(UNNotification *)notification{
    
    os_log(OS_LOG_DEFAULT, "Received Notification openSettingsForNotification");
}

-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
    
    os_log(OS_LOG_DEFAULT, "Received Notification didReceiveNotificationResponse");
}

当应用程序处于前台状态时,一切正常,但是一旦将其推送到后台,这些日志都不会在控制台上打印。这是我到目前为止尝试过的。

  1. 我已在“功能”的“背景模式”下设置了“远程通知”。
  2. 我尝试使用“ content-available”:1,因为“ aps”中的唯一值是“ apns-push-type”设置为“ background”。当应用程序也处于前台状态时,甚至都不会收到此类通知。
  3. 尝试设置UNUserNotificationCenter的委托,但从未调用过任何回调。
  4. 仅当“ aps”中具有“ alert”键并且仅在应用程序处于前台时,才会收到通知。

我注意到的另一件事是,当应用程序被杀死并且我从通知中心点击通知时,调用了didFinishLaunchingWithOptions,而launchOptions字典为nil。

有人有过这样的经历吗?我想知道如何解决这个问题。任何帮助将不胜感激。

欢呼

0 个答案:

没有答案