如何使用scheduleLocalNotification设置applicationIconBadgeNumber?

发布于 2024-12-28 06:25:33 字数 674 浏览 0 评论 0 原文

我在为我的 ios 应用程序设置带有计划本地通知的图标徽章时遇到一些困难。

当我加载应用程序后单击主屏幕时,我能够在 10 秒后触发本地通知弹出。但是,应用程序图标徽章编号尚未设置。我正在使用以下代码。

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = @"New Message!";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

我不明白为什么会弹出本地通知但 applicationIconBadgeNumber 未设置。我可以通过执行以下代码来手动设置图标徽章编号。

[UIApplication sharedApplication].applicationIconBadgeNumber = 1;

I am having some difficulties setting the icon badge with a schedule local notification for my ios application.

I am able to trigger a local notification pop up after 10 seconds when I click the home screen after loading the application. However, the application icon badge number is not getting set. I am using the following code.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] addTimeInterval:10]; // adds 10 secs
localNotif.fireDate = fireTime;
localNotif.alertBody = @"New Message!";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

I don't understand why the local notification pops up but the applicationIconBadgeNumber doesn't get set. I am able to set the icon badge number manually by executing the following code instead.

[UIApplication sharedApplication].applicationIconBadgeNumber = 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

峩卟喜欢 2025-01-04 06:25:33

你在模拟器中调试吗?模拟器上有同样的问题,但你的代码在我的 iPhone 上运行良好。

Are you debugging in the Simulator? Same problem on simulator but your code works fine on my iPhone.

じее 2025-01-04 06:25:33

您是否正在注册所有徽章通知类型的应用程序?

[[UIApplication sharedApplication]registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge]; 

另外,请检查您的通知设置是否已禁用徽章。

are you registering your application for all the badge notification type?

[[UIApplication sharedApplication]registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge]; 

also, check your notifications settings have not disabled badges.

携君以终年 2025-01-04 06:25:33

这是 Swift 中 iOS 8+ 的代码...

let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert , UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)
application.registerUserNotificationSettings(notificationSettings)

Here's the code for iOS 8+ in Swift...

let notificationSettings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert , UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文