debugging输出提到我应该要求应用程序徽章权限
我做了一个非常简单的应用程序,可以在计时器运行时在后台运行。 如果应用程序仍然在后台,计时器结束,它将发送本地通知,并将应用程序徽章设置为1.当我启动应用程序,我总是清除它。 我注意到,安装Xcode 6后,每次启动应用程序时都会收到以下消息:
“尝试标记应用程序图标,但尚未获得用户的许可来标记应用程序”
很明显,我的应用程序将徽章设置为0来清除文本。 我在哪里设置这些权限或请求他们? 现在是否被视为推送通知?
问题已经解决,答案在下面发布。 底线是,你需要从用户那里得到任何forms的通知的确认,而过去只有推送通知才是真实的。
我最终没有使用应用程序标志,而放弃了我在此处发布的初始代码片段。 既然现在还有人在阅读和评论这个问题,我也会在这里添加我的工作电stream解决scheme。 它包含对iOS7的检查,但是我不使用callback方法。 此外,这个版本不只是要求应用程序徽章的权限了。
解
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
这是我现在使用的
.h文件
#import <Foundation/Foundation.h> @interface NotificationPermissionHandler : NSObject + (void)checkPermissions; + (bool)canSendNotifications; @end
.m文件:
#import "NotificationPermissionHandler.h" @implementation NotificationPermissionHandler static const UIUserNotificationType USER_NOTIFICATION_TYPES_REQUIRED = UIUserNotificationTypeAlert | UIUserNotificationTypeSound; static const UIRemoteNotificationType REMOTE_NOTIFICATION_TYPES_REQUIRED = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; + (void)checkPermissions; { bool isIOS8OrGreater = [[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]; if (!isIOS8OrGreater) { [NotificationPermissionHandler iOS7AndBelowPermissions]; return; } [NotificationPermissionHandler iOS8AndAbovePermissions]; } + (void)iOS7AndBelowPermissions { [[UIApplication sharedApplication] registerForRemoteNotificationTypes:REMOTE_NOTIFICATION_TYPES_REQUIRED]; } + (void)iOS8AndAbovePermissions; { if ([NotificationPermissionHandler canSendNotifications]) { return; } UIUserNotificationSettings* requestedSettings = [UIUserNotificationSettings settingsForTypes:USER_NOTIFICATION_TYPES_REQUIRED categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:requestedSettings]; } + (bool)canSendNotifications; { UIApplication *application = [UIApplication sharedApplication]; bool isIOS8OrGreater = [application respondsToSelector:@selector(currentUserNotificationSettings)]; if (!isIOS8OrGreater) { // We actually just don't know if we can, no way to tell programmatically before iOS8 return true; } UIUserNotificationSettings* notificationSettings = [application currentUserNotificationSettings]; bool canSendNotifications = notificationSettings.types == USER_NOTIFICATION_TYPES_REQUIRED; return canSendNotifications; } @end
这是我的第一个解决scheme
我只是把它作为初步讨论的参考。 此代码不被维护。
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
您也可以通过执行以下操作将权限堆叠到一个请求中:
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
另外从iOS 8开始,可以找出用户允许的警报types:
UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; if (notificationSettings.types == UIUserNotificationTypeBadge) { // change the badge }
我结束了使用这个代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (![defaults objectForKey:@"first_run"]) { [self setDefaults]; } [self askAlertPermissions]; if ([self canChangeBadge]) { [self setBadge:0]; } return YES; } - (void)setDefaults; { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:[NSNumber numberWithBool:NO] forKey:@"alerts_allowed"]; [defaults setObject:[NSDate date] forKey:@"first_run"]; // More defaults if needed [defaults synchronize]; } - (void)askAlertPermissions; { UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; } // This will be called only after confirming your settings - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings; { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // There is also a built in method to find out if the user has appropriate settings, you might want to use that instead if you just want to know what the setting is [defaults setObject:[NSNumber numberWithBool:YES] forKey:@"alerts_allowed"]; } - (bool)canChangeBadge; { UIUserNotificationSettings* notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]; return notificationSettings.types == UIUserNotificationTypeBadge; }
更多阅读:
https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html
https://developer.apple.com/documentation/uikit/uiapplication
- NSAttributedString高度由width和numberOfLines限制
- IOS警告:无法分配渲染缓冲区存储
- 如何使用AVCaptureDeviceFormat从iPhone录制timelapsevideo?
- 使用@autoreleasepool降低峰值内存使用量
- 如何过滤“updateSearchResultsForSearchController”中的字典数组来search带有Swift的UITableView
- Objective-C:何时调用self.myObject vs只调用myObject
- 自下而上的UITableView以不同的项目高度滚动结束
- 在Swift中使用Map来更改自定义结构属性
- 如何在self.view属性上设置lldb观察点?