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

查看:34
本文介绍了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];

但这样做的问题是徽章编号不会在后续(多个)本地通知中增加(这里有一个场景,但为了简单起见,我们只说徽章保持不变1 甚至在 2 次或更多后,背靠背,本地通知).
在这种情况下,...推送通知似乎是要走的路
(但请注意,推送通知并不总是可靠的...检查:链接)

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天全站免登陆