Remote Notification callback not being called during phone call - ios

I have created an app in which background fetch code is written on receiving push notification. I have enabled the background mode in .plist, content-available key is set to 1 in push notification payload, registered for push notification and using delegate
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
Now when my app is in background, I receive a call.
During call, I receive a push notification for my app.
On receiving the push notification during a call, push notification delegate is not getting called.

So to handle the push notification/Remote Notification during phone call here below the method is:
When the phone call is received the app is become inactive and when the phone call is disconnected then the app become active and the Method "applicationDidBecomeActive" in the AppDelegate is called.Hence you can call back the Remote Notification in the didReceiveRemoteNotification method in the applicationDidBecomeActive.
Even you can handle the push notification when the app is terminated.
such as move on the specific viewController, didFinishLaunchingWithOptions contains the dictionary which contains the payload of the notification when app is terminated and Push notification is received . This can be done as . `.
if (launchOptions != nil)
{
// opened from a push notification when the app is closed
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
}
}
else
{
// opened app without a push notification.
}`
hope this will work :)

I guess during phone call cellular chip is being used for voice transmission. Data transmission is extra work for the chip to do which could affect battery life dramatically. This is a more a conscious decision by Apple to make it more of a silent notification during phone calls.

Related

How a push notification behaves based on iOS application state

I want to understand what will happen if a push notification arrives to device.
App Not running - what will happen if notification arrives - state change?
App is in foreground - ?
App is background - ?
App is inactive state ?
I am assuming if App is not running we can invoke by sending a silent push notification in some situations.
Can some one explain me how a push notification works based on app state.
If the application is not running or in background state, if the user accepted to receive push notifications, the push notification will be sent to the device that will display it on screen.
From this displayed notification, you can launch or wake the target application.
The traditional launch callback:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will be fired and you can access the received notification and its payload from its launchOptions dictionary, using UIApplicationLaunchOptionsRemoteNotificationKey key.
If the application is running and in foreground, the AppDelegate method
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
will be fired, userInfo containing the push notification payload.

push notification Handling when app is not running / call a url if push receive the app

i have a question. I have configured my app for development push notifications and all was working fine.
All push notifications arrived the app on the device. But i want to handle the push notification in a special way. If my app is running or in background the function
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
will work fine. But is the app killed the function doesn't work. I need a idea to handle incomming push notifications whether if the app is running, in background or killed. If a push was received the app must call a callback URL. if there any way to realize this?
for example i send the following apn string:
{"aps":{"content-available":1,"alert":"This","badge":1},"callback":"https://www.xxxxxxxxx.com/sf/daniel_push/985270815/12323453534534/?device(id)=1111111111&s="}
At the end all what i want is to call the callback url if the app becomes a push notification.
Try this in didFinishLaunching:
NSDictionary *userData = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userData) {
// handle notification
}

Apple push notification info is missing when we clear the notifications

I have received push notification, if i open the push notification then didReceiveRemoteNotification is calling and i am getting the notification information, instead of if i clear the notifications and open the app then didReceiveRemoteNotification is not getting called. How to resolve this?
If you open the app from the launch icon (instead of opening it from the push notification), didReceiveRemoteNotification will not be called and you'll have no way to access the notification data. It doesn't matter if you clear the notifications or not.
If the action button is tapped (on a device running iOS), the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications).
If the application icon is tapped on a device running iOS, the application calls the same method, but furnishes no information about the notification.
As Eran pointed out, if You're opening the app from the Icon that callback will not be called. I don't know if the notification is totally removed when You delete it from the notification center list, but if the app is not running and has been launched by tapping on the notification, you can check for the notification in the launchOptions at the startup:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
NSDictionary *notif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (notif) {
//handle your notification here
}
return YES;
}

iOS remote push notification with app background

Looking on web and in many stackoverflow post i have seen that the only way to get remote push notification when the app is in background (and it is launched from icon and not from push message) is to call the server when the app is loaded and get the "last" messages.
I've made this test with instant messaging app (i dont tell the name of the app, but i think you understand):
From another device i've sent one message to my device
When my device has received the notification i have wait for push notification pop-up to disappear.
At this point i have take device offline (no internet connection)
I have then open the application and in the list the message was correctly added
So, if i'm in foreground, i can handle the notification on method didReceiveRemoteNotification.
If the app is in background i can handle the nofification in didFinishLaunchingWithOptions (if i launch it with push notification pop-up).
How it's possible to handle the notification when the application is in background and it is launched from icon and not from the push pop-Up?
Thanks
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive)
{
// write code here for handle push notification when app is in foreground
}
else
{
// write code here for handle push notification when app is in background
}
}

Apple Push Notification in Background Issue

I'm using parse.com as my APNs provider for a test app I'm building. I've confirmed that push notifications are working as I have been able to successfully receive alerts when the app is in the foreground. Also, I have the remote-notification value for the UIBackgroundModes key specified in my plist.
In my app, I'm wanting to send a user's current location data back to my app-specific parse.com database when a push notification is received. I don't actually care about the notification payload itself, as the notification is just a means to getting a small piece of info. The app is constantly collecting data in the background and storing it in a persistent NSDictionary.
I've put the location sending code in the application:didReceiveRemoteNotification: method. If my app is in the foreground when I receive a notification, the method gets called. If my app is in the background, the method isn't called.
Am I doing something wrong?
Is it even possible to execute an API request in application:didReceiveRemoteNotification: when the app is in the background and the user hasn't interacted with the notification?**
EDIT: The problem persists even when I use application:didReceiveRemoteNotification:fetchCompletionHandler:.
Note: My NSDictionary full of location data isn't empty. Also, I am not attempting to do any UI manipulation in the background - just trying to perform an API request.
Check the following:
Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
The documentation for UIApplicationDelegate -application:didReceiveRemoteNotification is pretty clear on this method:
If the app is not running when a push notification arrives, the method
launches the app and provides the appropriate information in the
launch options dictionary. The app does not call this method to handle
that push notification. Instead, your implementation of the
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method needs to get the
push notification payload data and respond appropriately.
You need to handle notifications in both methods to be able to respond when your app is in the background and foreground.
you can try to use this instead of
application:didReceiveRemoteNotification
method, since you need to fetch your push in background mode, thus, this would works when the app is in background mode. However, you might need to add in custom notification or UIAlertView when app is in foreground to display your message. Hope it helps and it's not too late.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Remote Notification Received: %#", userInfo);
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"message to be displayed";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
completionHandler(UIBackgroundFetchResultNewData);
}
Alex L's answer almost worked for me.
Just make sure the value of content-available is 1 (number), not a string "1":
{
"alert":"",
"content-available":1,
"sound":""
}
badge parameter is optional.
(Using Parse Server for APNs)

Resources