Jaydeep Vyas
Jaydeep Vyas

Reputation: 4470

iOS 10.0 * Handle Push notification in foreground

If i implement the method to present push notification 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 present all the notification , So, my question is how can i prevent to show particular push notification like (login at another deveice ) i just handle code for that particular push only

Upvotes: 3

Views: 1444

Answers (2)

Jaydeep Vyas
Jaydeep Vyas

Reputation: 4470

My push data is

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

I have used id because i can separate each push by its id, and based on id i can decide weather to show notification in foreground or not..

Thanks @Anbu.Karthik For reference as per my question we can handle push even user did't tap on 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 user tap on notification Following method is called...

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

        print(userInfo)


    }

Upvotes: 1

atulkhatri
atulkhatri

Reputation: 11333

As per the official documentation:

// The method will be called on the delegate only 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 to have the notification presented as a sound, badge, alert 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 being shown, either do not implement this method or do not call the handler.

Hope it helps.

Upvotes: 0

Related Questions