With following code I get (2) in the badge icon immediately after app compiling:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.badge = 2
installation.saveInBackground()
}
I did try the next variant: Initialized a new var badgeCount = 0 and later:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
badgeCount++
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.badge = badgeCount
installation.saveInBackground()
}
But when I get new notifications it doesn't update to +1. Is anyone know how to fix it?
Whenever code is compiled it shows the badge icon which is previously store in your app. If you don't set the badge icon = 0 in your app it will show the badge icon number in your app every time you compile it or enter in background state.
Now for your problem, use badge icon as
var badgeCount = 0
UIApplication.sharedApplication().applicationIconBadgeNumber = ++badgeCount
Also whenever you are done with your task make badge icon as 0 otherwise it will show a badge icon in your app
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
It won't update the badge number with this method unless the app is actually open. If you want to update the badge number upon receiving a notification then you need to set the Badge property of the json push notification to the desired number.
If you, if you are sending a normal message (not using json) there is a toggle to increment the badge number, just tick that. If you're using Json then use this:
{
"aps": {
"alert": "Test Push Notification",
"sound": "yourSound.aiff",
"Badge": "desiredNumber"
}
}
Please note, if you do not wish to send this from the server, you can also send it from one device to another utilising Parse's client push, go into your settings in the app on Parse.com and enable "client push", you can then send the above Json to another user's device.
In Swift 5, you can update de application's badge whenever you want, using this code:
UIApplication.shared.applicationIconBadgeNumber = 0 // YOUR NUMBER DESIRED
I have worked on similar scenario and the final solution I found to increment and reset the badge numbers.
Increment Badge number
I always save the badge number count in the memory (NSUserDefaults)
Every time i have to set the notification, I get the current badge number increment that and set that number on .applicationIconBadgeNumber and update the count in memory.
Reset Badge Number
In my case, I have to reset all the badge count once the application is opened. So I have set UIApplication.sharedApplication().applicationIconBadgeNumber = 0 in didFinishLaunchingWithOptions of AppDelegate. Also I reset the count in the memory.
None of these answers are valid anymore.
You need to be looking at your Push code, not your AppDelegate
From the Parse docs:
badge: (iOS/OS X only)
the value indicated in the top right corner of the app icon.
This can be set to a value or to Increment in order to increment the current value by 1.
Related
Within AppDelegate I simply update applicationIconBadgeNumber:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
application.applicationIconBadgeNumber += 1
}
Everything works as expected when the app is connected to Xcode and is in debugger mode. But just after I plug it out from Xcode, notification arrives but badge is not updated. App is in background mode.
Why? What is wrong with my approach? Please, give me an advice🏆
Push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
You can send the badge number in the payload of the push notification,
Payload could look like this:
{
"aps" : {
"alert" : "Notification REceived",
"badge" : 1
}
}
I found this post very helpful for managing notifications in the background, basically you have to use the call with completion handler.
didReceiveRemoteNotification when in background
I think the best way to manage badge count is on the server.
A push notification will automatically update the badge on the app icon.
Payload formate.
{
"aps" : {
"alert" : "Weather report udapted",
"badge" : 5
}
}
Your messagingManager is an optional. You should check, that it isn't nil. If it is nil, the appDidReciveMessage() function would not be triggered
I integrated the Push Notification to the CloudKit so that every change in the iCloud will pop up a notification on my iPhone and the badge app icon number will add one correspondingly. However, when I used the code:
application.applicationIconBadgeNumber = 0
to reset that number in the applicationDidBecomeActive(_ application: UIApplication), I noticed that the badge app icon number truly disappeared but if another new notification came again, the number won't start from one again as supposed but just add one to the original total number before the reset. Therefore the number is getting bigger and bigger. I wonder how to solve this problem?
The problem is your apns payload, it contains badge count more than 1, you need to reset the payload as well.
When you set application.applicationIconBadgeNumber = 0 it just resets the badge count locally, not in the server.
Solution would be reset the badge count for the user in the server too.
Update: Apns Payload
{
"aps" : {
"alert" : {
"title" : "Push",
"body" : "Hello User"
},
"badge" : 5
}
}
The app shows the badge count same as in the apns payload above, you need to reset the badge value in the payload above from server.
Hope it helps.
Cheers.
I find that I should not only set the application side like:
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
but I should also set the iCloud side in CKContainer. Therefore, the complete code is like below:
let operation = CKModifyBadgeOperation(badgeValue: 0)
operation.modifyBadgeCompletionBlock = {(error) in
if let error = error{
print("\(error)")
return
}
application.applicationIconBadgeNumber = 0
}
CKContainer.default().add(operation)
In application we have mechanism like native Reminder app in iOS with firing notifications when user enter or exit in some region.
But two devices behave differently (5 and 5s) in same time. All devices have enable notifications, and allow use locations.
Two devices have a some "travel" and in the route created 10 points. First device (5) when came to finish received only 6 notifications, (5s) don't receive any notification.
But my question is how I can know when my app is restart in background or continue working. Because, all log in app I redirect into a file, and after download container and analyze what happened in app in travel time.
I noticed app restart in same times when device is enter to region and my log marks fired in the file but notifications don't receive. This is happended when app try to get some information from web service in didFinishLaunchingWithOptions
And maybe this is problem. How to know distinguish restart app or continue working. Thx.
Are you checking UIApplicationLaunchOptionsLocationKey in didFinishLaunchingWithOptions similar to (sorry, Swift is what I have now):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil {
// app was launched in response to incoming location event
}
}
Additionally, if you're not already doing this you may need to create notifications differently if app is in background:
// Show an alert if application is active
if UIApplication.sharedApplication().applicationState == .Active {
if let message = notefromRegionIdentifier(region.identifier) {
if let viewController = window?.rootViewController {
showSimpleAlertWithTitle(nil, message: message, viewController: viewController)
}
}
}
else {
// Otherwise present a local notification:
let notification = UILocalNotification()
notification.alertBody = notefromRegionIdentifier(region.identifier)
notification.soundName = "Default";
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
}
I am having troubles increasing the badge number using the Parse iOS Framework.
When I call this code, the other user gets the Push notification, but his badge number is not increasing on the icon.
let push = PFPush()
let data = ["badge": "Increment"]
push.setData(data)
push.setChannel("channel_\(userId)")
push.setMessage(message)
var err: NSError?
do {
try push.sendPush()
} catch var error as NSError {
err = error
} catch {
fatalError()
}
Thanks!
Try with this in swift for increase
let currentCountStr = UIApplication.sharedApplication().applicationIconBadgeNumber.description
let currentCount = Int(currentCountStr)
if(currentCount > 0) {
UIApplication.sharedApplication().applicationIconBadgeNumber = currentCount! + 1
} else {
UIApplication.sharedApplication().applicationIconBadgeNumber = 1
}
You should check your database and/or code if you successfully updated 'Installation' table. The way it works is, that they store badge number in that table (perhaps refresh after each application is awaken) so later server can send incremented number inside notification payload.
Parse had a blog entry on this : http://blog.parse.com/announcements/badge-management-for-ios/
Just to add note how it works in more generic environment :
You should send actual new badge number from the server, it is not just incremented for you.
There is good deal of information on another question :
Increment the Push notification Badge iPhone
Is there a way to clear the remote notification from the notification banner when swiping down from the top of the iPhone screen. I tried setting the badge number to zero:
application.applicationIconBadgeNumber = 0
in delegate didFinishLaunchingWithOptions, and didReceiveRemoteNotification, but it did not clear the notifications. Thanks.
In iOS 10, above all solutions are depreciated
'cancelAllLocalNotifications()' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]
Use the below code to cancel notification and reset Badge count
For iOS 10, Swift 3.0
cancelAllLocalNotifications deprecated from iOS 10.
#available(iOS, introduced: 4.0, deprecated: 10.0, message: "Use UserNotifications Framework's -[UNUserNotificationCenter removeAllPendingNotificationRequests]")
open func cancelAllLocalNotifications()
You will have to add this import statement,
import UserNotifications
Get notification center. And perform the operation like below
application.applicationIconBadgeNumber = 0 // For Clear Badge Counts
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications() // To remove all delivered notifications
center.removeAllPendingNotificationRequests() // To remove all pending notifications which are not delivered yet but scheduled.
If you want to remove single or multiple specific notifications, you can achieve it by below method.
center.removeDeliveredNotifications(withIdentifiers: ["your notification identifier"])
Hope it helps..!!
You need to set the IconBadgeNumber to 0 and cancel the current notifications. I never did in swift but I think the code for it would be as bellow:
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 5
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
I have to increment then decrement badge count in order for it to work:
application.applicationIconBadgeNumber = 1
application.applicationIconBadgeNumber = 0
application.cancelAllLocalNotifications()
Swift 3
In your AppDelegate.swift file under didFinishLaunchingWithOptions add:
application.applicationIconBadgeNumber = 0
On the launch of your app this will remove the iOS badge (red circle at the top right corner of the app icon).
any one looking for swift 4 and above code
application.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
This for scenario when the app is forcefully terminated by the user :
First of all send nonzero badge when you want to send Birthday reminders notifications to the users via push notifications , For example :
{
"aps": {
"alert": {
"title": "Hey! Urgent Reminder",
"body": "Do not forget my wife SURPRISE BIRTHDAY PARTY"
},
"badge": 1
}
}
After that , When there is no need of showing notifications in the device , you can send silent notification with zero badge that will clear badge and notifications even if app is forcefully terminated by the user but didReceiveRemoteNotification will not called because app is terminated.
payload for silent push notification :
{
"aps" : {
"content-available" : 1,
"badge" : 0,
"Priority" : 10
}
}
After send that payload will automatically clear badge and delete push notification from Notification Center.
Not. If badge was zero before sending silent notification , will not clear notifications.
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
In Nov, 2019 below is the working solution for me with Swift 4. First you have to check the device version to clear all notifications but do not need to check to reset badge count.
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]? ) -> Bool {
//--------
application.applicationIconBadgeNumber = 0
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
center.removeAllPendingNotificationRequests()
} else {
application.cancelAllLocalNotifications()
}
//------
}
Swift 4 & 5
import UserNotifications
...
...
...
UNUserNotificationCenter.current().removeAllDeliveredNotifications()