加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 调试输出提示我应该申请应用程序徽章权限

发布时间:2020-12-15 02:00:41 所属栏目:百科 来源:网络整理
导读:我做了一个非常简单的应用程序,可以在后台运行一个计时器运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序徽章设置为1.当我启动应用程序时,我总是清除它。我注意到安装Xcode 6后,每次启动该应用程序时都会收到此消息: “尝试标
我做了一个非常简单的应用程序,可以在后台运行一个计时器运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序徽章设置为1.当我启动应用程序时,我总是清除它。我注意到安装Xcode 6后,每次启动该应用程序时都会收到此消息:

“尝试标记应用程序图标但尚未获得用户对应用程序的许可”

很明显,文字是由我的应用程式产生,将徽章设为0,以将其清除。我在哪里设置这些权限或要求他们?现在被认为是推送通知吗?

问题已经解决,答案在下面发布。最重要的是,您需要从用户那里获得任何通知的确认,而以前只有推送通知才是真实的。

解决方法

我最终没有使用应用程序徽章,我放弃了我在这里平均发布的初始代码段。由于仍然有人阅读和评论这个问题,我将在这里添加我的工作当前解决方案。它包含对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/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读