繁体   English   中英

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

[英]Badge on App Icon in Iphone App

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

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

如果您想通过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()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM