简体   繁体   中英

Badge on App Icon in Iphone App

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.

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

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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