4

Is there a way to intercept user's click on push notification or user's click on rich push notification action button, WITHOUT LAUNCHING THE APP? I have implemented all necessary settings to register for push notifications, to receive push notifications and to handle simple and rich push notifications, and user's click on some action button inside rich push notification. But, when user clicks on some action button, app launches, and I can handle it inside:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let notification = JSONParser.parseRichPushNotification(notification: response.notification.request.content.userInfo)
    if let responseIdentifier = 
    {
         print(response.actionIdentifier)
    }
//......
}

I would like to perform, for example some background action without launching the app, or to open web browser if there is a link in push notification data. Is it that possible in iOS?

I have found similar questions:

But I think that I didn't find the answer for my question. I have also read about silent notifications, but I am not sure if I can use them for this purpose, i.e. to perform some operation when user clicks on action button without automatic launching the app.

Btw, I'm usine OneSignal push notifications

Does anybody know how to perform this? Thanks in advance

Community
  • 1
  • 1
Vladimir88dev
  • 733
  • 1
  • 10
  • 19
  • 4
    Did you use the `foreground` option when creating `UNNotification​Action` objects? You should not. You app will be allowed to handle the action without coming to the foreground if you don't use `foreground` – Lou Franco Apr 04 '17 at 18:49
  • I am not using local notifications, but remote notifications. I receive action buttons via push notifications, so I think that I don't need that. Should I do that? – Vladimir88dev Apr 04 '17 at 18:53
  • 3
    If the user taps the notification then your app will be launched. That's it. If your app is launched from a notification then you could open a URL as soon as your app opens, but your app will appear first. – Paulw11 Apr 04 '17 at 19:04
  • 2
    @LouFranco Thanks for this. Changing the option from UNNotificationActionOptionForeground to UNNotificationActionOptionNone worked for me! – Ashutosh Shukla May 02 '19 at 04:52

1 Answers1

3

You can accomplish this by creating a Custom Interface that implements the UINotificationContentExtension protocol.

Antonio R.
  • 407
  • 3
  • 9
  • Tnx for the answer, I am trying to do something using notification extension. Do you know maybe how to prevent notification from presenting? I try to do this without: OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent) contentHandler(bestAttemptContent) But this prevents all next push notifications to present, and when I stop app in debugger, all notifications appear, even the notifications which I prevented from showing – Vladimir88dev Apr 06 '17 at 10:09