IOS 10.0 * Handle Push Notification Foreground

If I implement a push notification presentation method in ios 10.0

@available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler(UNNotificationPresentationOptions.alert)

    }

      

then it represents all notifications, So my question is how can I prevent a certain push notification from being shown, for example (login in another view). I am just processing the code for that specific click only

+3


source to share


2 answers


My push data

{
    "aps" : {
        "alert" : {
            "id" : 2091
        },
        "sound" : "chime.aiff"
    },
    "acme" : "foo"
}

      

I used id because I can separate each click by its ID, and based on the id I can decide whether the weather is showing the notification in the foreground or not.

Thanks @ Anbu.Karthik For help on my question, we can handle the click, even the user didn't click on the notification in



   @available(iOS 10.0, *)

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        print("Handle push from foreground")
        let userInfo:[AnyHashable:Any] =  notification.request.content.userInfo
        let aps:NSDictionary = (userInfo[AnyHashable("aps")] as? NSDictionary)!
        let alert:NSDictionary = (aps["alert"] as? NSDictionary)!
        let id = Int(alert["id"] as! Int)
        if id == your id
        {    
         //Handle push without prompting alert 
        }

        else
        {
          //Display alert
         completionHandler(UNNotificationPresentationOptions.alert)
        }
    }

      

And when the user clicks on the notification, the following method is called ...

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

        print(userInfo)


    }

      

+1


source


According to the official documentation:

// This method will only be called by the delegate if the application is in the foreground. If the method is not implemented or the handler is not called in a timely manner, then the notification will not be presented. The application can choose the notification presented as sound, icon, warning and / or in the notification list. This decision should be based on whether the information in the notification is otherwise visible to the user.

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);

      



So, to answer your question, if you want to prevent the notification from showing, either don't implement this method or don't call the handler.

Hope it helps.

0


source







All Articles