- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我制作了一个非常简单的应用程序,它可以在计时器运行时在后台运行。如果应用程序仍在后台并且计时器结束,它将发送本地通知并将应用程序标记设置为 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/
我们可以使用GCM通知作为APNS徽章图标在iPhone应用程序上显示徽章编号吗 由于我无法使用GCM进行设置。 我推荐this link这说 Parameter Platform
注册推送通知时,我没有启用角标(Badge)警报... - (void)registerForPushNotifications { UIRemoteNotificationType noti
我向用户推送了一个没有角标(Badge)键或角标(Badge) = 0 的通知负载,通知无法从通知中心删除。这是我的有效负载: { "aps" : {"alert" : "bala bala
我想在用户每次打开应用程序时清除应用程序图标角标(Badge)。在我的应用程序中,我使用以下代码清除角标(Badge): [UIApplication sharedApplication].appli
在第二版中我可以使用 badge badge-important 我发现 .badge 元素不再具有上下文(-success、-primary 等)类。 如何在版本 3 中实现同样的效果? 例如。我希
Yii2 Twitter Bootstrap 徽章 颜色不变。 In browser source i cant find badge-success, badge-danger e.t.cclass
我目前正在设计一个教育网站。 为此,我们需要根据用户的事件(例如stackoverflow)为他们分配一些徽章。 我正在为此寻找框架。任何人都有任何想法,如何做到这一点? table 设计等 我可以想
我正在制作一个应用程序,该程序显示自从两个人建立关系以来的天数。我想在应用程序图标徽章上显示天数。我知道一旦用户离开应用程序该怎么做,但是我想每天更新图标徽章,即使该应用程序未在后台打开或在后台运行也
我是 iPhone开发的新手。我只是创建像聊天应用程序之类的应用程序。很简单,我使用JSON解析方法,并通过PHP从服务器调用数据并将其发送回给他们。 所以我有一个UIButton我想在此上设置徽章U
我想在控制SplitView中弹出框的UIBarButtonItem上添加徽章 这段代码什么都不做: - (void)splitViewController: (UISplitViewControll
也许这是一个简单的问题... 我有一个带有3个ViewController的3个选项卡栏项的UITabBarController。 我能够将Badge(通过使用setBadgeValue)添加到选项卡
我正在开发 react-native iOS 中的聊天应用程序。我想在收到通知、应用程序被杀死或用户强制退出时在主应用程序图标上进行标记增量。当应用程序处于后台模式时,它运行良好。但是 didRece
当我收到消息时,我会将用户名和未读消息计数写入 CoreData。并借助此功能: func unreadMessagesCountBadge(cell: onlineUserCell, forCoun
这是我的代码,我将日期放在应用程序角标(Badge)中。我想知道如何每天重新加载这个号码而不进入应用程序重新加载它。提前致谢! NSDate *today = [NSDate date]; NSCal
我有一个带有标签栏 Controller 的应用程序。这些 View 之一是 TableView 。有一种方法可以在标签栏中设置此 View 的角标(Badge)。这有效......但只有当用户触摸此
在我的数据库中,我将传递的消息计数存储在一个表字段中。对于我发送到特定设备的每条消息,我都会增加这个值。 当设备收到带有角标(Badge)设置的 payloa 时,它会在应用程序图标上显示红色圆圈。
我的应用程序包含许多嵌入式练习,这些练习会在应用程序使用期间显示给用户。我计划发布我的应用程序的定期更新,其中包含额外的练习。每次(标准应用程序商店应用程序)更新后,我想在应用程序图标上添加一个角标(
我试图了解当用户的网络连接特别差或没有网络连接时,iOS 上远程通知的最佳实践。 场景是这样的: 用户会收到一些远程通知,并在将来的某个时候打开应用程序。此时,我不想让服务器尽快知道角标(Badge)
我有一个函数,可以在单元格上添加角标(Badge): unreadMessagesCountBadge(cell, forCount: messagesCount) 但我不知道,如何检测我需要添加此角
我的问题的根源是我无法在真实设备上测试推送消息,因为这个问题似乎是 Xcode bug,但这不是现在的主题。我只是添加这个来回答为什么我不能正常测试我的实现。 因此,我使用 OneSignal SDK
我是一名优秀的程序员,十分优秀!