iOS 10请求通知权限会触发两次
当我在iOS 10上启动我的应用程序时,我获得了两次请求通知权限 。 第一个短暂出现并立即消失而不允许我做任何动作,然后我得到第二个弹出窗口,其中有一个正常的行为,等待用户“允许”或“拒绝” 。
这是我的代码在iOS 10之前运行良好。
在AppDelegate的方法didFinishLaunchingWithOptions中 :
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { #ifdef __IPHONE_8_0 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil]; [application registerUserNotificationSettings:settings]; #endif } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; }
我应该为iOS 10实现一些东西来修复这个双重请求权限吗?
对于iOS 10,我们需要在appDelegate didFinishLaunchingWithOptions方法中调用UNUserNotificationCenter。
首先,我们必须导入UserNotifications框架并在Appdelegate中添加UNUserNotificationCenterDelegate
AppDelegate.h
#import #import @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if([[[UIDevice currentDevice]systemVersion]floatValue]<10.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { if( !error ) { [[UIApplication sharedApplication] registerForRemoteNotifications]; NSLog( @"Push registration success." ); } else { NSLog( @"Push registration FAILED" ); NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription ); NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion ); } }]; } return YES; }
更多细节
- 将十进制坐标转换为度,分,秒,方向
- 在SLComposeServiceViewController中更改预览图像
- Firebase:针对同一个项目中的每个应用程序进行自定义validation
- AVCaptureOutput didOutputSampleBuffer停止调用
- 如何从iPhone上的在线url获取audio
- 找不到Xcode CoreFoundation / CFUserNotification.h'文件
- MPMoviePlayerViewController,删除quicktime符号/添加背景图像?
- UIRefreshControl闪烁
- UIActivityViewController问题iOS 7和iOS 8?