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.
They're the small icons on your smartphone screen typically made from an app's logo, the icons you click to launch an app. An app icon badge is the little red dot that appears in the corner of an app icon. Inside the red circle, you'll generally find a number printed in white text. That's called the badge count.
Turn on App icon badges from Settings. Navigate back to the main Settings screen, tap Notifications, and then tap Advanced settings. Tap the switch next to App icon badges to turn them on.
Tap the ellipsis (three dots) button next to a Focus mode, then select Settings in the dropdown. Under "Customization," tap Options. Tap the switch next to Hide Notification Badges to enable the option.
You can set the app icon's badge number like this:
[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()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With