在iOS12这样设置会闪退
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
[content setValue:@(YES) forKeyPath:@"shouldAlwaysAlertWhileAppIsForeground"];
这样做的目的通常是为了在前台收到推送通知, 或者本地通知时也显示弹窗, 所以可以修改以下方法设置
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0)){
........
//当应用处于前台时提示设置,需要哪个可以设置哪一个
if (@available(iOS 10.0, *)) {
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
} else {
// Fallback on earlier versions
}
}
另外如果你是iOS12的系统, 在前台收到推送后没有走这个方法, 检查
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate=self; // 检查这个代理是否设置
}
}