3

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

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • see this for example https://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10/39383027#39383027 – Anbu.Karthik Jul 13 '17 at 09:22

2 Answers2

1

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)


    }
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
0

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.

atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • my question is about when app is in foreground, And i want to prevent particular notification not all – Jaydeep Vyas Jul 13 '17 at 10:08
  • In that case, you can check the notification payload and then do not call the handler if you want to hide a particular notification. – atulkhatri Jul 13 '17 at 11:02