Can we call api on silent notification in background? - ios

Is that possible to call API to send data to the server while getting silent push-notification in iOS? Any help will be appreciated.
Thanks in advance

It is possible to make an API call after receiving a silent push.However, it does not work if the app is killed by the user. If that is a problem, please see this answer
If that is not the case, here is how to do it. You need to enable 'Background Fetch' and 'Remote Notifications' from Background Modes on Application Capabilities screen on XCode.
Then, add this application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method into your AppDelegate.You can make your API call inside this method.
Ex:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
apiCall(fetchCompletionHandler: completionHandler)
}
func apiCall(fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void){
//Make call here
completionHandler(UIBackgroundFetchResult.noData)
}

Related

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

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.

Change notification behavior swift

I need to call a notification so that the phone vibrates and rings for several minutes at a certain point in time, planned for earlier.
To send a notification, I use UNNotificationRequest. after searching, I realized that you can only change .sound, having muted the sound for 30 seconds. Which does not fit.
I thought to make several notifications at intervals of a second, but there is a limit on the number of notifications (64 pieces), and this is not enough. Then I tried to cause vibration with
func application (_ application: UIApplication, performFetchWithCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .background {
for _ in 1 ... 30 {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
sleep(1)
}
completionHandler(.newData)
}
}
But the problem is that I do not know, how to call this function at a certain point in time, and even if the application is killed.
+ There is a problem with the fact that when you open the application, the vibration does not pass.
+ performFetch more than anything else for network requests.
Question: I need to attract the user's attention with vibration + melody (if the mode allows it) + notification. How can this be done, and in which direction should I continue to dig?
Maybe all the same I somehow can wake the application up in time and start the vibration + melody. A notification will arrive from the system itself.
UPD 1: from the answer https://stackoverflow.com/a/43232737/13642906
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { }
don`t work on ios10 and above.
Now (ios 10 and above) use
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) { }
but its method dont call when not called when local notification arrives. Maybe, am I doing something wrong?
Is it possible to do what I want?
 

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.

Which method is called or How to handle when remote push notification arrives when app is killed manually

App remote notification work well when app is foreground or background state but not works when App is killed manually,
Added background fetch in plist.
Tried with:
NSLog("Do something")
under this method still not able to receive in swift :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
and
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void)
When you kill the app manually, the remoteNotifications cannot be handled before the app is launched again.
When a remote notification is present, and you open the app by tapping on that notification, the app launches in the usual manner with the
didFinishLaunchingWithOptions
method being called in the app delegate. But in this case, your notification data would be passed in the 'Options' parameter. You can then check for this parameter and perform the required tasks
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if (launchOptions != nil)
{
let dictionary:NSDictionary = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary
self.setNotification(dictionary)
}
return true
}
You cannot handle a push notifications while app is not running. You only can handle when a user open the app form notification.
There is good article about that: How to handle remote notification with background mode enabled

Resources