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
}
Related
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.
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.
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.
When my app is on background and I receive a remote notification, two things can happen:
I tap on the push notification banner, my apps comes to foreground and didReceiveRemoteNotification is called.
I tap on my app icon from the springboard, my app comes to foreground and didReceiveRemoteNotification IS NOT called.
So, in the scenario 1, I can update my counter of unread messages inside the app in response to didReceiveRemoteNotification.
In the scenario 2, I can't.
How can I solve this using Quickblox?
As one possible variant:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self handleRemoteNotifications:userInfo];
}
// Override point for customization after application launch.
return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self handleRemoteNotifications:userInfo];
}
#pragma mark - Remote notifications handling
-(void)handleRemoteNotifications:(NSDictionary *)userInfo {
// do your stuff
}
#end
When app is not running, in didFinishLaunchingWithOptions: you can use this code for get push's payload:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *myKey = [userInfo objectForKey:#"myKeyFromPayload"];
}
Remember to set permission in plist
For the remote push you can use in your appdelegate:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
The issue is probably that application:didReceiveRemoteNotification: is not called if the app is not running. To quote Apple documentation:
This document is outdated
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.
This is the new document
Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.
You have to enable Remote Notifications in the Background Modes.
To do so, automatically: (Xcode5)
- Go to your Project settings -> Capabilities -> Background Modes
- Tick "Remote Notifications"
To do so, manually:
- Open your %appname%-Info.plist
- Right click and tick "Show Raw Keys/Values"
- Right click and choose "Add Row"
- Type in "UIBackgroundModes" (Key)
- The key will be created, and the type is an Array
- Add new item in the array with the value of "remote-notification" (Value) and press enter
- Now you have 1 item in your array called: "Item 0", if you had any other items in there, just add this item (remote-notification) to the array.
Be sure to use these methods frankWhite used :)
Hope this helps ;)
This question already has answers here:
Will iOS launch my app into the background if it was force-quit by the user?
(7 answers)
Closed 9 years ago.
I have implemented silent push notification. Its not working if I killed app manually.
After getting silent push notification, I have called one function which will send data to server.
Here is my code for Silent Push notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
NSLog(#"didReceiveRemoteNotification fetchCompletionHandler...........");
[MyviewController SendDataFunction];
handler(UIBackgroundFetchResultNewData);
}
Please guide me.
Update:
-There is no way to launch app via silent notification or via background fetch if user has killed app manually in app switcher. To get notification user must start the app again. And still if anybody needs to activate app then PushKit + VOIP should be enabled for that app like whatsapp, refer the following link
https://zeropush.com/guide/guide-to-pushkit-and-voip
didReceiveRemoteNotification is only called app when running and app in background state, it not called app not running state, that time you get notifications like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...............
...............
UILocalNotification *localNotif =[launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSLog(#"load notifiation *******");
// Perform your actions
}
return YES;
}