2

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.

2 Answers 2

2

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:

  1. 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.
  2. 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)

Sign up to request clarification or add additional context in comments.

6 Comments

Ohhh Is that really true? Dam bad. But why they are not allowing. Is there any other bypass for this ?
@nilkash : [1] yes, that's true (afaik) and [2] i think they're not allowed due to, maybe, performance issues because if multiple apps get push notifications and they each do their own thing then the device performance will take a major hit. Also maybe security reasons... who knows. [3] i don't know of any way to bypass this.
hi staticVoidMan thank you for your quick replay. I just tested what you said. only thing is that when I click on my notification it launched my application and also execute -didReceiveRemoteNotification method. Then why we have to access it in didFinishLaunchingWithOptions.
I am not accepting your answer because I am waiting for my problem answer but your answer is also gives good idea.Thank you.
@nilkash : two scenarios: first... your app is closed (like totally closed i.e not in your currently running apps list) then the push notification is accessible in the -didFinishLaunchingWithOptions: method. second... your app is running (but in background). Now the push notification is accessible in the -didReceiveRemoteNotification: method. Do one thing: quit your app, send a push notification and try to access it.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.