ios-关闭应用程序时,本地通知不更新徽章编号 [英] ios - local notification not updating badge number when application is closed

查看:82
本文介绍了ios-关闭应用程序时,本地通知不更新徽章编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当在ios设备中接收到本地通知时,该通知会显示在通知中心中,但是在关闭应用程序后,应用程序徽章编号不会更新.

I have noticed that when a local notification is being received in an ios device, the notification appears in the Notification Center but the app badge number is not updated when the app is closed.

我需要在通知中心中触摸通知,以将本地推送消息传输到应用程序.

I need to touch the notification in the Notification Center for the local push message to be transferred to the app.

这是正常现象吗?可以通过使用远程推送通知来解决此问题吗?

Is this the normal behavior? Can this be solved by using remote push notifications?

推荐答案

您可以在UILocalNotification对象中利用applicationIconBadgeNumber参数.

You can utilize the applicationIconBadgeNumber parameter in a UILocalNotification object.

基本上:

localNotificationObject.applicationIconBadgeNumber++;


示例:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];
localNotification.alertBody = @"Some Alert";

//the following line is important to set badge number
localNotification.applicationIconBadgeNumber++;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

但是问题是,在随后的(多个)本地通知中,徽章编号不会增加(这里有一种情况,但是为了简单起见,我们只说徽章保留了即使在2或更多之后,也会有1则,是背对背的本地通知).
在这种情况下, ...推送通知似乎是可行的方式
(,但请注意,推送通知并不总是可靠的...请检查:链接 )

But the issue with this is that the badge number doesn't increment on subsequent (multiple) local notifications (there's a scenario here but for simplicity sake, lets just say the badge stays 1 even after 2 or more, back to back, local notifications).
In this case, Yes... Push Notification seems to be the way to go
(but be aware that Push Notifications aren't always reliable... check: link)

好吧...要使用推送通知进行正确的徽章编号更新,您应该知道可以在推送通知的有效负载中发送徽章计数.
收到此推送通知后,iOS会将徽章计数更改为在推送通知中指定的徽章计数(&无需为此应用打开).

Well... to use Push Notifications for proper badge number updates, you should know that you can send a badge count in the Push Notification's payload.
When this push notification is received, the badge count is changed by iOS to the badge count specified in the Push Notification (& the app need not be open for this).

applicationIconBadgeNumber设置为0,因为它在某些情况下会有所帮助(可选)

Set applicationIconBadgeNumber to 0 as it helps in certain scenarios (optional)

- (void)applicationWillResignActive:(UIApplication *)application {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}


额外:

在终止/关闭或退出应用程序时,您也可以手动设置徽章编号.
通常...采用以下任何一种或所有方法:


Extra:

You can also manually set the badge number when you terminate/close or resign the application.
Generally... in any or all of the following methods:

  • -applicationWillResignActive
  • -applicationDidEnterBackground
  • -applicationWillTerminate(在应用关闭时设置badgeNumber)
  • -applicationWillResignActive
  • -applicationDidEnterBackground
  • -applicationWillTerminate (set badgeNumber when app closes)

示例:

- (void)applicationWillResignActive:(UIApplication *)application {
    //Called when the application is about to move from active to inactive state.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
    //...
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
    //...
}

这篇关于ios-关闭应用程序时,本地通知不更新徽章编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆