Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 890
Sudy Avatar asked Jan 25 '11 07:01

Sudy


People also ask

What are app icon badges iPhone?

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.

Where is the badge app icon?

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.

How do I turn off app badges on my iPhone?

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.


2 Answers

You can set the app icon's badge number like this:

[UIApplication sharedApplication].applicationIconBadgeNumber = 3;
like image 151
Di Wu Avatar answered Sep 20 '22 21:09

Di Wu


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()
}
like image 25
gmogames Avatar answered Sep 20 '22 21:09

gmogames