I have an app in which I'm trying to receive and handle SILENT push notifications.
I'm registering for APNs as follows:
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) {(granted, error) in
if granted {
application.registerForRemoteNotifications()
}
}
Implemented:
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
// Handle notification
}
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
// Handle notification
}
The app has UIBackgroundModes
fetch
and remote-notification
The APN content looks like:
{
"aps":{
"content-available":1,
"badge":0
},
"action":"shareDelete",
"shareId":633
}
When the app is in the foreground, userNotificationCenter(centre:withCompletionHandler:willPresentNotification)
is fired when the notification is received, but when the app is backgrounded, nothing happens.
I can see the APN is received by changing the badge number, but nothing is triggered in app.
What am I missing?
(Also asked on Apple dev forums)
Yes. It will. When you click on that. Yeah its ok but being push notification is silent,you are not able to view any alert and cant click.
the app doesn't wake up. We use this mechanism to send device location to our server.
There are two ways users can receive silent push notifications on Android. Users can long press on a notification to get an option to display notifications silently. Users can also enable silent notifications by heading to Settings > App & Notifications > Search for app and choose> Notifications, and turn it off.
They arrive on the mobile device and sit in the notification tray until read or dismissed. Users can choose to mute their notifications and receive silent push notifications instead of being alerted to every new push that arrives. They can activate this on iOS or Android from their individual settings pages.
Method
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
// Handle notification
}
called when user taps on notification or check it on Apple Watch:
Called to let your app know which action was selected by the user for a given notification.
You need to implement the old method
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With