【发布时间】: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 版有什么要添加的吗?
【问题讨论】:
-
你调用什么函数来发送通知?
-
@KakshilShah 现在检查我的问题。
标签: objective-c xcode ios11