2

I am developing iOS app with both Push notification and Local Notification, I know how to remove 1 and all notification from center,

using

[[UIApplication sharedApplication]cancelAllLocalNotifications];

But my problem is if i have scheduled some local notifications ,

and a push notification is arrived so in didReceiveRemoteNotification I am write

[[UIApplication sharedApplication]cancelAllLocalNotifications]; for clear notification center,

but it is cleared all my LocalNotification also...

EDIT if there are total 3 notifiction in NC i.e. 1 is come from local notification and two from push( from server) in this case how can i handle it,? i am tap on 1st notification( comes from server) in NC. in this case what should do, my app badge should be 2.

then what should i do?

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121

3 Answers3

3

The cancelAllLocalNotifications will only cancel the local notifications, its even in the name! Not the push notifications as you can read in the documentation:

Cancels the delivery of all scheduled local notifications.

Since push notifications are server side there is noting to cancel in your app. To remove the push notification from the notification center just set the applicationBadegNumber to 0.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Thnaks for answer. so if there are total 3 notifiction in NC i.e. 1 is come from local notification and two from push( from server) in this case how can i handle it,? i am tap on 1st notification( comes from server) in NC. in this case what should do, my app badge should be 2.then? – Toseef Khilji Oct 07 '13 at 12:24
  • 1
    To remove the push notification you have to set the `applicationBadegNumber` to 0. After which you can set it to 2 fro you local notifications. – rckoenes Oct 07 '13 at 12:26
  • if i make `applicationBadegNumber=0` is it remove all notifications from NC? or remove only Push noti. ? – Toseef Khilji Oct 07 '13 at 12:31
2

There is an option ,each notification contains a dictionary inside it, so when you creates any local notification add any key in dictionary which specifies that this notification is for local notification.so you can check that if it is not my local notification then i will remove it.

 -(void)scheduleLocalNotification{
    [self cancelAlarm]; //clear any previous alarms
     UILocalNotification *alarm = [[UILocalNotification alloc] init];
    alarm.alertBody = @"alert msg";
     alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime]; 
    alarm.soundName = UILocalNotificationDefaultSoundName; 
   NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"localNotification"  forKey:@"localNotification"];
   alarm.userInfo = userInfo;
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

}

 -(void)cancelNotification{
for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){
    NSDictionary *userInfo = notification.userInfo;
    if (![self.key isEqualToString:[userInfo objectForKey:localNotification]]){
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}

}

PJR
  • 13,052
  • 13
  • 64
  • 104
  • Yes I am cleared about to handle Local notification, but suppose if there are total 3 notifiction in NC i.e. 1 is come from local notification and two from push( from server) in this case how can i handle it,? i am tap on 1st notification( comes from server) in NC. in this case what should do, my app badge should be 2.n in NC how can i show only remaining notifications. – Toseef Khilji Oct 07 '13 at 12:53
  • i am not getting what you are saying.. what you want to do actually. remove any notification or badge ? – PJR Oct 07 '13 at 12:58
  • yes remove only notification that are come from server (push) not local ones. – Toseef Khilji Oct 07 '13 at 13:00
  • so look at my second method. cancelNotification it is removing notifications which are not local. – PJR Oct 07 '13 at 13:00
  • is this method to call in didReceiveRemoteNotification method? – Toseef Khilji Oct 07 '13 at 13:24
  • yes currently i am checking with localnotification you can do it with remoteNotification. – PJR Oct 07 '13 at 13:25
0

The applicationbadgenumber is managed by you. Deleting scheduled notifications will not change that number. You must manage that yourself. I gave a presentation a while back that might help. http://www.youtube.com/watch?v=ixQqZWtn0pg Start watching at 11:50.

ZappyCode
  • 75
  • 1
  • 8