Iphone 应用程序中应用程序图标上的徽章

发布于 2024-10-14 17:28:29 字数 54 浏览 3 评论 0原文

我们如何在应用程序图标中获得徽章通知,类似于选项卡栏项目中的徽章通知。我需要这个来通知新消息。

How could we get a badge notification in the app icon , similar to badge notifications in tabbar item.? I need this for notifying new messages.

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

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

发布评论

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

评论(2

梅窗月明清似水 2024-10-21 17:28:29

您可以像这样设置应用程序图标的徽章编号:

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

You can set the app icon's badge number like this:

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
人事已非 2024-10-21 17:28:29

如果您想通过 PUSH 消息添加徽章编号,您可以将 PUSH 发送为:

{"aps":{"alert":"My Push Message","sound":"default","badge",3}}

然后在 AppDelegate 中添加以下内容:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

// This get's the number you sent in the push and update your app badge.
[UIApplication sharedApplication].applicationIconBadgeNumber = [[userInfo objectForKey:@"badge"] integerValue];

// Shows an alert in case the app is open, otherwise it won't notify anything
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Notification!"
                                              message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]  delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
[alertView show];    
}

swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

// This get's the number you sent in the push and update your app badge.
UIApplication.shared.applicationIconBadgeNumber = (userInfo["badge"] as? NSNumber)?.intValue ?? 0

// Shows an alert in case the app is open, otherwise it won't notify anything
let alertView = UIAlertView(title: "New Notification!", message: (userInfo["aps"] as? [AnyHashable : Any])?["alert"], delegate: self, cancelButtonTitle: "OK", otherButtonTitles: "")
alertView?.show()
}

If you're wanting to put the badge number through PUSH messages, you can send the PUSH as:

{"aps":{"alert":"My Push Message","sound":"default","badge",3}}

Then in your AppDelegate you add the following:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

// This get's the number you sent in the push and update your app badge.
[UIApplication sharedApplication].applicationIconBadgeNumber = [[userInfo objectForKey:@"badge"] integerValue];

// Shows an alert in case the app is open, otherwise it won't notify anything
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Notification!"
                                              message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]  delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
[alertView show];    
}

swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

// This get's the number you sent in the push and update your app badge.
UIApplication.shared.applicationIconBadgeNumber = (userInfo["badge"] as? NSNumber)?.intValue ?? 0

// Shows an alert in case the app is open, otherwise it won't notify anything
let alertView = UIAlertView(title: "New Notification!", message: (userInfo["aps"] as? [AnyHashable : Any])?["alert"], delegate: self, cancelButtonTitle: "OK", otherButtonTitles: "")
alertView?.show()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文