didReceiveRemoteNotification:fetchCompletionHandler not being called when app is in background and inactive - ios

In the application active state, the didReceiveRemoteNotification method is called and when the application is in the background and inactive state the didReceiveRemoteNotification method is not called.
Code:-
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void){
NSLog("Push Received: \(userInfo)")
completionHandler(UIBackgroundFetchResult.newData)
}

If it calms you down, I had the same problem and I did nothing to fix this, because after a few hours my application started to receive notification in the background. But if it doesn't help, you should try sending notifications manually using ARC, and check notification status.

If you want below method to call
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
in background mode using remote notification. you need to send content-available : 1 in aps payload as silent notification then only it will awake your app from background mode. And if you want you can configure local alert notification using same payload.

Related

Silent push notification (APNS) can not received

I have set up the silent push notification for my app:
1. I configured the push notification from all places, i.e., XCode, Apple Developer portal with proper certificate
2. I enabled background capability
3. I included "content-available" in the json payload.
However, my App can not receive silent push sometimes.
To be more specific, neither
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
......
}
nor
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (_: UIBackgroundFetchResult) -> Void) {
......
}
has been invoked when the push message arrives.
How can I get the information in the push notification in such state?
I'm waiting online.
You question is twofold:
1. why the two delegate callbacks can not be invoked
2. what should you do to receive the data.
I did some hand test and here is the result:
1, when an app is in killed state, the two callback can not be invoked indeed
2. however, when you open the app next time,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
......
}
can be invoked and you can get the data from there.
I hope this is useful.

Silent push notification with Firebase in iOS10

I can't call method in background using silent push notifications.
Checked "Background Modes" > "Background fetch" and "Remote notification".
Add method to send data to Firebase.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.viewController.sendData()
}
Send push notification with content_available: true.
When app is in foreground, sendData() is called. But when in background, it isn't called.
Try to set time_to_live = 0. It works for me.

remote notification issue swift 3

I have an app that receives push notification but I have a doubt:
when app is in foreground and comes a remote notification I handle it but this method in AppDelegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
but I don't understand how do I have to handle the remote notification when comes a remote notification when app is in background and the user tap on rectangle of notification and then app opens.

Make a web request after receiving a push notification

I have a Swift 2 iOS8+ app where I need to make a request to fetch JSON data when my app receives a push notification.
When the user clicks on the notification the app will go and fetch the data but I really need the data to be fetched as soon as the notification is received. This looks to be possible, is this the case?
I've implemented:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)){
and checking the launch options in:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
I've also enabled:
background fetch
remote notifications
None of this seems to help. If this is possible I'd be grateful for any pointers/tutorials on this.
As of iOS 9, instead of a normal push, you can use silent push notifications. When your app receives a silent push, the user is not notified, but your app can perform actions based on this notification. Then, when your background actions is finished, you create a local notification for the user.
Check out this tutorial for info on how to use silent notifications:
https://www.raywenderlich.com/123862/push-notifications-tutorial

Swift View Push Notifications In App

I've got push notifications going to the application I'm working on, but I can't seem to manage them in the application. If I can't manage the notifications in the application, how else can I reduce the incrementing value of the badge icon for the application app-icon?
You can change badge count in push data, also if you need manage received push notification data, use this function:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
It's a snap to change the badge count from anywhere in the app:
UIApplication.sharedApplication().applicationIconBadgeNumber = 4

Resources