Uday Babariya
Uday Babariya

Reputation: 1021

Stop Present Push Notification while App is Inactive or in Background

I want to stop notification to present when user has been logged out from my App. I have tried below.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    print("\n>>willPresent notification")

    if sharedManager.instance.isLoggedIn {
        completionHandler([.alert, .badge, .sound])
    }else{
         completionHandler([])
    }
}

This is working perfect when app is active. But the problem is when the app is in background or inactive, This code is not work.

I checked for background mode, at that time the willPresent notification and didReceiveRemoteNotification methods are not get called.

Which is the appropriate method get called while receiving push notification in background or Inactive Mode??

Upvotes: 1

Views: 966

Answers (1)

Bhavin Kansagara
Bhavin Kansagara

Reputation: 2916

There is no direct way to handle this condition.

You can ask your API developer to handle this at server side,

  1. When user clicks on logout button, call API to logout and set the status, isLoggedIn to false

  2. For every API, that will send the push notification to user, at server it first check for the user's logged in status and can send the notification accordingly.

  3. On, Login again set isLoggedIn to true in db at server and then it will start receiving the notifications again.

Hope it helps.

Upvotes: 1

Related Questions