[英]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.