gpt4 book ai didi

ios - 调试输出提到我应该请求应用程序角标(Badge)权限

转载 作者:IT王子 更新时间:2023-10-29 08:11:02 25 4
gpt4 key购买 nike

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

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

很明显,该文本是由我的应用程序将角标(Badge)设置为 0 以清除它生成的。我在哪里设置或请求这些权限?它现在是否被视为推送通知?


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

最佳答案

我最终完全没有使用 Application Badge,并且我放弃了在此期间发布的初始代码片段。由于仍有人阅读和评论这个问题,我也会在此处添加我的当前工作解决方案。它确实包含对 iOS7 的检查,但我不使用回调方法。此外,此版本不再只是请求应用程序角标(Badge)权限。

解决方案

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

关于ios - 调试输出提到我应该请求应用程序角标(Badge)权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24028172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com