18

我制作了一个非常简单的应用程序,它可以在计时器运行时在后台运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序徽章设置为 1。当我启动应用程序时,我总是将其清除。我注意到安装 Xcode 6 后,每次启动应用程序时都会收到此消息:

“尝试标记应用程序图标,但未获得用户授予应用程序标记的权限”

显然,该文本是由我的应用程序生成的,将徽章设置为 0 以清除它。我在哪里设置这些权限或请求它们?现在算是推送通知了吗?


问题已解决,答案贴在下方。最重要的是,对于任何类型的通知,您都需要从用户那里获得确认,而过去只有推送通知才适用。

4

1 回答 1

29

我最终根本没有使用应用程序徽章,同时我放弃了我在此发布的初始代码片段。由于仍然有人阅读和评论这个问题,我也将在此处添加我当前的工作解决方案。它确实包含对 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

于 2014-06-04T10:51:27.063 回答