当我收到PN.时,我正在尝试更新我的应用程序的徽章图标(已关闭)
我已经尝试将代码添加到中,但当我的应用程序关闭时,它不起作用。当应用程序在前台运行时,它可以工作。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
//Accept push notification when app is not open
if (remoteNotif) {
[application setApplicationIconBadgeNumber:100];
return YES;
}
}
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 30];
}
如果您的应用程序已关闭或处于后台,推送通知不会唤醒它。你需要在服务器端这样做,并在通知负载的图标上包括你想看到的号码:
{
"aps" : {
"alert" : "Your notification message",
"badge" : 1
}
}
查看推送通知编程指南
对于didFinishLaunchingWithOptions:
方法中的集合applicationIconBadgeNumber = 1
或0
,如下所示。。。
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
参见UILocalNotification
的另一个答案从此链接ios徽章号码实时更新
还有来自此链接的RemoteNotificationsPG指南的另一个链接
由于推送通知由iOS处理,而不是由您的应用程序处理,因此您无法在收到推送通知时更改应用程序徽章。
但是你可以在推送通知的有效负载中发送徽章号码,但你必须在服务器端进行计算。
有效载荷可能是这样的:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 1
}
}
现在,应用程序徽章图标将显示1。