0

Like WhatsApp, I am going to implement a web interface for an iPhone application. If a user logged in on both environments(iPhone & web) and receives a message from another user, a notification is sent to the iPhone and web. When the user reads that message from the web interface, notification sent to iPhone should be removed as WhatsApp does even when the iPhone app is killed.

How can we remove that iPhone sent notification from backend? As I know we don't have control on iPhone app when app is killed by User.

Thanks in advance.

atifali
  • 193
  • 16
  • See the below answer for how silent notification works. https://stackoverflow.com/questions/36694963/what-is-silent-push-notification-when-does-the-device-receive-it – jogshardik May 09 '19 at 12:45

3 Answers3

2

I don't know how whatsapp manages this but you can use silent notification to achieve the above feature.You need to turn on the background mode for silent notification.

When Silent notification comes it doesn't show up on screen like regular notification it will open your application even if your app in terminated mode. The system will provide some time to your app to perform certain task and you can do in that time frame only.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
       print(" Entire message \(userInfo)")
       print("Article avaialble for download: \(userInfo["articleId"]!)")
       //let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(scheduleLocalNotification), userInfo: nil, repeats: false)
       let state: UIApplication.State = application.applicationState
       switch state {
       case UIApplicationState.active:
           print("If needed notify user about the message")
       default:
           registerBackgroundTask()
           removeBadges()
    }
    completionHandler(UIBackgroundFetchResult.newData)
}
func registerBackgroundTask() {
       backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
       self?.endBackgroundTask()
   }
   assert(backgroundTask != UIBackgroundTaskIdentifier.invalid)
}

func endBackgroundTask() {
  print("Background task ended.")
  UIApplication.shared.endBackgroundTask(backgroundTask)
  backgroundTask = UIBackgroundTaskIdentifier.invalid
}
func removeBadge() {           
  UIApplication.shared.applicationIconBadgeNumber = 0           
  UIApplication.shared.cancelAllLocalNotifications()
}
jogshardik
  • 1,446
  • 1
  • 12
  • 23
1

What's app is a VoIP app and it's always (in a way) awake in the background. So one can perform api calls to check status of the application messages. If your app has a VoIP feature, you can benefit from it.

Shrey
  • 1,959
  • 2
  • 21
  • 44
1

Try using VoIP Pushes, silent push notification wont work if app was intentionally terminated by user.

archilo20
  • 41
  • 5