Get push notification data when application is in background IOS - ios

Hi I am developing small IOS application in which I am using push notification. So in my case I am able get notifications and I am also able to access data for notification. Data from notification mean title, description etc. So in my case I am not saving my notification at my server side. I want to save those locally. For that what I want as soon as notification come I want to save that data locally. I am able to access my data when App is in foreground but I am not able to access my notification data when app is in background. I want to access the data of notification so that I can save it. For notification I did following things:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken {
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
for (NSString *key in [userInfo allKeys])
{
NSString *data = [userInfo objectForKey:key];
NSLog(#"inside did register for notification .... %# ---- > %#",key,data);
}
}
My requirement is simple I want access to notification when my app is in background. Any one is here who already did this? Need Help. Thank you.

In iOS, the app cannot access it's push notification until the user taps on the push notification from the notification center.
Once the push notification is tapped and the app loads/becomes active, only then will you be able to access the push notification.
FYI:
When the app is in background and a push notification is recieved.
After the user taps on the push notification:
the contents will be accessible in the -didReceiveRemoteNotification: method.
When the app is not open and a push notification is received.
After the user taps on the push notification
the contents will be accessible in the -didFinishLaunchingWithOptions: method in it's launchOptions parameter.
Also... push notifications aren't 100% reliable. They may or may not be delivered (although them not being delivered is pretty rare in my observation but worth pointing out none-the-less)

There is a way in iOS 7.0 and later where you can send notifications and access its content without user tapping it.
So, the notification payload which you send has a parameter called content-available. You can view the payload parmeters in the following link:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html
So when you set the content available to 1 and deliver the notification, iOS calls the below function even if the app is not in the background or foreground(must have remote notification enabled)
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
Then you can use the userInfo dictionary to fetch your notification data.

Related

What is UIApplicationLaunchOptionsRemoteNotificationKey used for?

if I understand correctly, the UIApplicationLaunchOptionsRemoteNotificationKey key is used on the -[UIApplicationDelegate application:didFinishLaunchingWithOptions:] method when
- the push was received when the application was not running (e.g. killed)
- the user clicked on the received push
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo) {
// app was not running and the user clicked on the push
}
}
but .. in this exact same case, the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is also called just after the previous one.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
// called when
// app was not running and the user clicked on the push
// app was running in background and user clicked on a push
// app was running in background and a silent push was received
// app is in foreground and a push is received
completionHandler(UIBackgroundFetchResultNewData);
}
So the question is, why should I use the UIApplicationLaunchOptionsRemoteNotificationKey if everything can be handled in the application:didReceiveRemoteNotification:fetchCompletionHandler delegate? Did I miss something?
cheers,
Jan
In case when the app is killed and user taps on push notification in notification center, launchingOptions dictionary contains UIApplicationLaunchOptionsRemoteNotificationKey so that you can adjust your app start logic.
In prior iOS version there wasn't application:didReceiveRemoteNotification: fetchCompletionHandler: and launchingOptions dictionary from application:didFinishLaunchingWithOptions: was the only place where you could handle remote notification on app start.
My guess is that application:didFinishLaunchingWithOptions: contains UIApplicationLaunchOptionsRemoteNotificationKey for compatibility reasons.
The presence of this key indicates that a remote notification is available for the app to process. The value of this key is an NSDictionary containing the payload of the remote notification.

Push notification data not getting when app launched directly by clicking app icon

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.

Silent Local Notification IOS

Can we have a silent local notification in IOS app. Which does some data processing in the background with out the user interacting with it.
What I want to do is create a silent local notification which fires every 8 am in morning and after the user receives it I want to do some data processing and recreate a new one which the user can see with the new data I processed after I saw the first silent local notification.
I am trying to avoid using push notification as much as I can.
You can receive silent notifications in the background on iOS, but you will need a server to actually send the notifications.
To do this you enable the Remote notifications background mode in your target's Capabilities tab:
Then you register for push notifications in application:didFinishLaunchingWithOptions: with
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeNone categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
Pending the user allowing your app to send push notifications, you will receive the device token:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;
If something goes wrong, the failure handler will be called:
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:
You send the deviceToken to your server and tell it to send a silent push notification to that deviceToken at the device's local time of 8AM.
That device will have the following app delegate method called:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler;
and you will be able to do your data processing.
Easy!
Don't forget to call the completion handler when you're done!

iOS: Detect app start via notification press

is there a way to detect if my App is started from a notification being displayed on the home screen?
When my App is going into background I schedule a UILocalNotification. When the user presses this notification at the home screen, the app is starting again. I want to get this information that my app was started again from pressing the notification.
I already tried out what was mentioned in Send notification to user from app via notification API but the app does not call the method:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Thanks for your help.
The delegate method you are implementing is for remote notifications. In order to get the user info for the local notifications scheduled by your app you have to implement:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
Apple Documentation has all the information you need on local notifications.
Add these lines to didFinishLaunchingWithOptions function in your app delegate
NSDictionary *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif)
{
//your code here
}

Usage of UrbanAirship's handleBackgroundNotification

I am trying to develop an iOS app using UrbanAirship. I receive push
notifications but cannot get any delegate to be called when the app is in
the background. I (wrongfully it seems) assumed that the
handleBackgroundNotification API from the UAPushNotificationDelegate
class would provide me the functionality to execute custom actions when
receiving the notification when app is in the background.
This is what their documentation says:
"handleBackgroundNotification:
Called when a push notification is received when the application is in the background
- (void)handleBackgroundNotification:(NSDictionary *)notification
Parameters
notification
the push notification
"
- see https://docs.urbanairship.com/ios-lib/Classes/UAPushNotificationHandler.html#//api/name/handleBackgroundNotification:
Doesn't seem to work that way - sure seems the OS is keeping the notification
for itself - which is inline with Apple's documentation.
I am questioning the purpose of the function if the OS doesn't allow it. I use didReceiveRemoteNotification for receiving remote push notifications which works just fine!
However, since this is an enterprise application (i.e. not App Store),
if there are private APÏs and frameworks that would allow me to do this,
I would appreciate any assistance. There is no way this app would ever
make it to the app store!
The custom actions I am trying to execute include, for example, a
notification receipt sent to a server that would "prove" the recipient
app did indeed receive the notification, play a custom sound at maximum
volume (bypassing silence and do not disturb mode). These are some
requirements from the client.
This is what I use and its working just fine:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if(!loggedIn) return;
NSLog(#"WOW: got notification! %#", userInfo);
// see this for fine tuning: http://fivelakesstudio.blogspot.com/2012/04/push-notifications-and-urban-airship.html
[[UAPush shared] handleNotification:userInfo applicationState:application.applicationState];
[[UAPush shared] resetBadge];
sharedApplication.applicationIconBadgeNumber = 0; // probably redundant
[self handlePushNotification:userInfo isBooting:NO]; // my common handler
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
UA_LINFO(#"Application received remote notification: %#", userInfo);
[[UAPush shared] appReceivedRemoteNotification:userInfo applicationState:application.applicationState fetchCompletionHandler:completionHandler];
NSDictionary *values = [userInfo objectForKey:#"aps"];
NSString *title = [values objectForKey:#"alert"];
}
process the notification received for all states in this block.
NB. you can monitor the application.applicationState value to note the state of the app when the notification is received.
Hope this helps

Resources