【发布时间】:2011-06-15 00:33:33
【问题描述】:
我们如何在应用图标中获得徽章通知,类似于标签栏项目中的徽章通知。?我需要这个来通知新消息。
【问题讨论】:
标签: iphone push-notification badge
我们如何在应用图标中获得徽章通知,类似于标签栏项目中的徽章通知。?我需要这个来通知新消息。
【问题讨论】:
标签: iphone push-notification badge
如果您想通过 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];
}
斯威夫特:
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()
}
【讨论】:
您可以像这样设置应用图标的徽章编号:
[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
【讨论】: