How to Catch All iOS Push Notifications with Different User Actions Including Tap on App Icon

How to catch all iOS Push Notifications with different user actions including tap on app icon

You can't, you will only receive information about the notification that was used to open your app.

So if a user opens your app, and your app has notifications, you will not be able to retrieve them from with in your app.

A work around could be to also keep track of notification on a server and handle this with in the app. Thus the server keeps track on which notification has been read. This is how Facebook does it.

Get a push PAYLOAD, after user tap-on-notification, actually in userNotificationCenter?

Answer kindly provided by the crew at npm-apns2

For data like

let bn = new BasicNotification(deviceToken, 'Teste', {
data: {
name: 'jack',
street: 'jones'
} })

so ...

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler:
@escaping () -> Void) {
print("notification tapped, app opens")
let userInfo = response.notification.request.content.userInfo

let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'jones'
print("> \(test3)") // nil

completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("notification arrived while app in foreground (user has
not 'tapped on notification' - you get this immediately)")

let userInfo = notification.request.content.userInfo // sic

let name: String? = userInfo["name"] as! String?
let street: String? = userInfo["street"] as! String?
let test3: String? = userInfo["typo"] as! String?
print("> \(name)") // optional 'jack'
print("> \(street)") // optional 'main'
print("> \(test3)") // nil

completionHandler(.alert)
}

and that's it.

iOS push notification: how to detect if the user tapped on notification when the app is in background?

OK I finally figured out.

In the target settings ➝ Capabilities tab ➝ Background Modes, if you check "Remote Notifications", application:didReceiveRemoteNotification: will get triggered as soon as notification arrives (as long as the app is in the background), and in that case there is no way to tell whether the user will tap on the notification.

If you uncheck that box, application:didReceiveRemoteNotification: will be triggered only when you tap on the notification.

It's a little strange that checking this box will change how one of the app delegate methods behaves. It would be nicer if that box is checked, Apple uses two different delegate methods for notification receive and notification tap. I think most of the developers always want to know if a notification is tapped on or not.

Hopefully this will be helpful for anyone else who run into this issue. Apple also didn't document it clearly here so it took me a while to figure out.

Sample Image

Unable to get push (remote) notification when app icon tapped

You need to continue reading. The remainder of the section you quoted reads -

If the app icon is clicked on a computer running OS X, the app calls
the delegate’s applicationDidFinishLaunching: method in which the
delegate can obtain the remote-notification payload. If the app icon
is tapped on a device running iOS, the app calls the same method, but
furnishes no information about the notification.

Note the section I bolded. The short answer is that if the application is launched from the app icon then no information is provided about any notifications that may have been received.

Handling push notifications iOS when app is force-quit by user (Alternatives?)

You should never assume that state has remained consistent between the time the notification has been delivered and the time the user has launched the app. Nor, even, that it is the same device. I'll frequently get a "Hey! Do something!" notification on my phone and, if my iPad is handy, respond to it on my nice big iPad screen.

Instead, you should round trip to the server and grab the most up to date state for that user at the time of app launch or activation.

Is there a way to get iOS System alarm notifications?

Unfortunately not. You have to stay within the confines of your app. The iOS Clock app has special privileges because...well, it's made by Apple.

Is the notification not enough? I would think that would alert the user adequately.



Related Topics



Leave a reply



Submit