我最终根本没有使用应用程序徽章,同时我放弃了我在此发布的初始代码片段。由于仍然有人阅读和评论这个问题,我也将在此处添加我当前的工作解决方案。它确实包含对 iOS7 的检查,但我不使用回调方法。此外,此版本不再只是要求申请徽章许可。
解决方案
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
这是我的第一个解决方案
我保留它只是作为最初讨论的参考。此代码未维护。
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
您还可以通过执行以下操作将权限堆叠到一个请求中:
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
此外,从 iOS 8 开始,可以确定用户允许什么样的警报:
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