1

Within few minutes, I got two different local notifications at the same time. But the badge number of the app show "1" instead of "2"

my code:

var localNotification = UILocalNotification()

localNotification.userInfo = ["UUID": FirstReminderString, ]
localNotification.fireDate = SameDay
localNotification.alertBody = "Reminder !"
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19
Trombone0904
  • 4,132
  • 8
  • 51
  • 104
  • You might find useful additional information in [this related question](http://stackoverflow.com/questions/5962054/iphone-incrementing-the-application-badge-through-a-local-notification). – Michael Dautermann Jul 13 '15 at 09:44
  • okay, that means that before a new notification will register, i have to check how many notifications will be active on the same day and get this count number + 1 for the new one? how can ich check the number of notifications on a day? – Trombone0904 Jul 13 '15 at 09:57

2 Answers2

1

When you are scheduling the notifications, the current badge number is taken (and it is likely 0). Thus, the new badge number will be 1 for all notifications you are scheduling before the badge number increments.

tilo
  • 14,009
  • 6
  • 68
  • 85
1

If your app is in foreground mode: you can adjust badge number by following code:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1; // increase counter
}

This method will not be called if app is in background mode.

So we have to do something in server as follows:

func sendLocalNotif(notifCountAlreadySent:Int) {

    var localNotification = UILocalNotification()

    localNotification.userInfo = ["UUID": FirstReminderString, ]
    localNotification.fireDate = SameDay
    localNotification.alertBody = "Reminder !"
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.applicationIconBadgeNumber = notifCountAlreadySent + 1

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}

See if it helps. This function receives a tracking number. That is being used as badge number. You have to just keep track of how many notification already sent.

Md. Najmul Hasan
  • 605
  • 1
  • 6
  • 19