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
}
}
Related
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.
I have a scenario in which app will get push notification and need to show that messages in home screen of my app, for that i saved the message array into user defaults from my app delegate and everything works well, but in following conditions it's not working
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
Then i searched for solutions and came to know about silent push notifications (for background mode) and nothing else so i need to know how to handle all scenarios by push notifications and my appdelegete is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
[self handlePushMessage:remoteNotif];
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[self handlePushMessage:userInfo];
}
-(void)handlePushMessage :(NSDictionary*)userInfo{
//method to handle push message
}
Thanks in advance
This is a common issue: if the user does not open your app by means of the displayed notification, there is no* way of getting the related information.
* A possible solution employed by many apps is to check with a remote server for unread notifications (e.g. check for an unset read-date field).
In scenario 1. NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; here remoteNotif return nil as you enter into the app through triggering app icon.
In scenario 2. You can get push notification info through the following method
-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if (userInfo) {
[self handlePushMessage:userInfo];
}
}
if app is in killed state and a notification came and user triggeres the app through the app icon (not from push notification )
If the app is in killed state then the push notification payload can only be handed over to the app when the user taps the push notification itself. If the user launches the app from app icon then the notification payload will not be passed to the application
if app is in background and notification came and user enters to app through app icon (not from push message ) in this case also
if you are targeting iOS7 and up then you need to enable background mode for remote notifications. Please check below link in order to get notification payload even when app is in background
didReceiveRemoteNotification not working in the background
The above mentioned app delegate method gets called when the app is in foreground,Background and suspended state.
There is no way to get the notification payload when app is killed and when the app icon is directly clicked instead of push notification in notification center.
Enable "Remote Notifications" under background modes in capabilities in target settings. This will fetch the notification data even when the app is in the background. Also, make sure to implement:
-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler;
in your app delegate.
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.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(#"PUSH NOTIFICATION %#",userInfo);
if([userInfo[#"aps"][#"content-available"] intValue] == 1){
NSLog(#"SILENT PUSH NOTIFICATION");
//Here I'm inserting a flag value to my DB.
completionHandler(UIBackgroundFetchResultNewData);
}
else{
NSLog(#" GENERAL PUSH NOTIFICATION ");
completionHandler(UIBackgroundFetchResultNoData);
}
}
My silent notification payload is {
aps = {
"content-available" = 1;
sound = "";
};
}
To support I have added a key in info.plist in "Required background modes"
item 0 = App downloads content in response to push notifications
Also in capabilities section my Background Modes are ON with remote notification check mark is selected.
Now my question is when I run my app then I'm able to receive silent as well as general push notification and code is executing successfully in foreground mode but when I press home button (not forcefully quitting by swiping it away and also device's passcode lock is open) my app is going to background mode and then my code is not executing even I'm able to receive push notifications those are having alert and sound, but app is not launching in background mode.
I'm thinking that for foreground and for background mode whenever my app is receiving any push notification don't matter is it silent or general push notification my first log in the delegate method should have to print on the console i.e NSLog(#"PUSH NOTIFICATION %#",userInfo);
Please help I'm struggling with this since last 2-3 days.
My info.plist
I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".
#Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.
I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.
My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.
Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.
I think you need to test once with 'content-available' outside 'aps', like the following:
aps = {
alert = "This is test alert";
badge = 1;
};
"content-available" = 1;
From my understanding, when app is running or in the foreground and a push notification is received, the app should NOT show any alert but the app delegate will call the didReceiveRemoteNotification delegate method and I should handle the push notification in that callback.
The push notification should ONLY display alerts/banners when the app is in the background.
However, our app gets push notification alert with an OK button when the app is running or in the foreground sometime, not all of the time. I'm wondering if it is something new in iOS 7 (I have never heard of this) or is it because I'm using UrbanAirship for push notification for our iOS app using alias of the user. The app will display the push alert when running and run the callback in didReceiveRemoteNotification.
Scratching my head over this. Does anyone know why?
When the App is in foreground, it should not display anything.
If you see alertView, it means you provided code for it.
Something along the following lines:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
//app is in foreground
//the push is in your control
} else {
//app is in background:
//iOS is responsible for displaying push alerts, banner etc..
}
}
If you have implemented pushNotificationDelegate
[UAPush shared].pushNotificationDelegate = self;
then override, and leave it blank
- (void)displayNotificationAlert:(NSString *)alertMessage
{
//do nothing
}
You are most likely seeing the additional push notification because of the way Urban Airship is configured in your code. From Urban Airship's docs:
The sample UI includes an implementation of UAPushNotificationDelegate that handles alerts, sounds, and badges. However, if you wish to customize this behavior, you can provide your own implementation:
[UAPush shared].pushNotificationDelegate = customPushDelegate;
There is more information on the proper way to handle push notifications with Urban Airship in their support articles. Since you are using UA, I would recommend that you use their delegates etc. to handle incoming push notifications while in the foreground rather than implementing your own code in the didReceiveRemoteNotification app delegate method.
Hopefully that helps...if not, please post your code so that we can decipher what is going on. This would be very odd behavior indeed!
This answer is for swift users looking for the same thing
let state = application.applicationState
if (state == .active) {
//app is in foreground
//the push is in your control
} else {
//app is in background:
//iOS is responsible for displaying push alerts, banner etc..
}
Please refer to the checked answer if you guys have any more doubts.
Apple Documentation on handling Notifications