【问题标题】:Push notification not received when App is in Background in iOS 11应用程序在 iOS 11 中处于后台时未收到推送通知
【发布时间】:2018-04-11 07:05:50
【问题描述】:

我正在使用 UNUserNotificationCenter 在 iOS 中发送推送通知。

当应用程序处于前台状态时,我能够收到通知。但是当App处于后台状态时,并没有收到通知。每当应用程序进入前台状态时,才会收到通知。

注册远程通知:

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
        if( !error ){
            dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });
        }
        else{
            NSLog( @"Push registration FAILED" );
            NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
            NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
        }
    }];
}
else {
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

当应用处于前台模式时,调用该方法:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
   willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

但此方法在后台模式下不起作用。 我提到了一些 StackOverflow 问题,但无法解决问题。 iOS 11 版有什么要添加的吗?

【问题讨论】:

标签: objective-c xcode ios11


【解决方案1】:

如果是远程通知,则在应用委托中调用此方法:

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Handle notification here!
    }

【讨论】:

  • 确保后台模式已启用远程通知
  • 我正在使用 UNUserNotificationCenter 委托方法来接收远程通知:- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
  • @Kakshil Shah 只有当通知到达并且表明有数据要获取时才会触发该委托。 developer.apple.com/documentation/uikit/uiapplicationdelegate/…
猜你喜欢
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多