Thursday, April 27, 2017

Possible to overwrite existing push notification on iOS

Leave a Comment

In Android it is possible to overwrite an existing push notification if you keep using the same notification id.

Is the same possible for iOS in any way?

It seems hard to find any information about replacing an push notification, because a lot of answers are using silent push notifications and remove them manually.

I use Cordova so I have limited options for background processes when receiving push notifications.

On iOS I cannot run code to manually remove any push notifications when the app is in the background.

1 Answers

Answers 1

No, in iOS you can not overwrite already scheduled remote / local notification.

You have to schedule another push notification.

By maintaining some flag or checking on key / value. You have to remove previous notification while reviving new notification.

Hope this helps.

Updated

As an alternate, once you receive push notification, on based of information you received in push notification payload.

You can schedule local notification.

import UIKit import PushKit  @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {  var window: UIWindow? let notificationObject = UILocalNotification()  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {       return true }  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {        notificationObject.fireDate = NSDate(timeIntervalSinceNow: 1)     notificationObject.alertBody =  "Title"     notificationObject.alertAction = "Open"     notificationObject.soundName = "SoundFile.mp3"     notificationObject.category = ""     notificationObject.userInfo = "As per payload you receive"      UIApplication.sharedApplication().scheduleLocalNotification(notificationObjectCall)   } 

As you can see in above code, local notification object is declared globally. So that object will get overwrite again and again whenever you receive payload.

For remote notification, you can not make object and overwrite it.

I am not much aware of cordova, but in native iOS, this way, you can do overwrite using local notification.

Hope you understand what technique is been used and help you figure out solution.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment