I have push notifications enabled for my app and I detect whether the app is opened from a push notification or not on didFinishLaunchingWithOptions
I want to take action based on the push notification that the user has opened the app from. So for example if the notification says "Someone has added you to their friends", opening the app from that notification would take the user to that other user's profile.
How can I achieve this?
on application:didFinishLaunchingWithOptions: method;
NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
// do something with notification
}
Related
I am making a certain application.
Which has these functions.
1 This application has push notification.
2 This application can stock the push notification log when it is active.
3 This application can stock the push notification log when it is not active.
1,2 is OK for me. 1 is normal, 2 is accomplished by push notification callback.
However 3...?
I guess on Android background job works and get the notification.
but is it impossible on iOS?
In addition to the previous answer: about didFinishLaunchingWithOptions.
This only works when the program is launched by clicking on the notification/push message window. But if you run the app directly, by clicking on the app icon, even when the notification/push message arrives, you will not receive any data in launchOptions about push message.
So about the paragraph 3: you can know it only if user tap on push message window/bar.
When the application is not active, you can handle the push notification using didFinishLaunchingWithOptions delegate method:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
NSString *json = [localNotif valueForKey:#"data"];
// Parse your string to dictionary
}
I have a problem with push notifications in iOS, in a similar scenary to this and this and another one.
Also, this post resume all possible situations.
In my case:
app is NOT RUNNING
content-available:1
UIBackgroundModes contains 'remote-notifications' and 'fetch'
If the user force-quit the app and receives a push notification, then it could open app from alert or from icon.
When the user tap on the notification the app will be opened and the following method will be executed:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle for notifications when app is closed
if (launchOptions) {
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
if (apsInfo) {
// handle notification
}
}
No problem up to here, I have the payload to doing something with that info (for example to fetch new data from server).
But if the user open the app from the icon I don't have any way to handle the payload (Although didFinishLaunchingWithOptions is execute, I don't have the aps info, according to docs here).
So, the question is, there are any way to solve that?
For example, I made the test with WhatsApp, and they handle that situation, (probably they are using VOIP notifications instead of Remote Notifications)
Regards
You should never assume that state has remained consistent between the time the notification has been delivered and the time the user has launched the app. Nor, even, that it is the same device. I'll frequently get a "Hey! Do something!" notification on my phone and, if my iPad is handy, respond to it on my nice big iPad screen.
Instead, you should round trip to the server and grab the most up to date state for that user at the time of app launch or activation.
The user force killed the application and then receives 3 push notifications from the server.
When clicking on the push notification, the application will relaunch.
Is it possible to read user info dictionary of all push notifications available in the notification bar for that particular application?
No, it is not possible. You need to implement a web service.
The web service will provide all unread notifications.
If your application isn't running then only the notification that the user actually tapped on will be delivered to your application in didFinishLaunchingWithOptions. The content of the other notifications is not available.
If the user launches your application from the app icon rather than from the notification then no notification information is available.
Your app should retrieve any updates whenever it is launched, regardless of the availability of notification data in didFinishLaunchingWithOptions. The presence of notification data should act as a hint as to the behaviour the user is expecting from your app (for example, if they tapped a notification that they had received a message from a specific user they would probably expect the app to open to that message).
Yes it is possible to read the push notification by clicking it from the navigation controller.
First of all when application is open it comes in didFinishLaunching method. And if you trying to open application from the navigation push notification click then you can get the notification in launchOptions variable. It contains the dictionary regarding the Pushnotification.
You can find it by below code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions valueForKey:#"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:#"aps"];
if(apsInfo) {
//there is some pending push notification, so do something
}
}
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) {
NSLog(#"app recieved notification from remote%#",notification);
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}else{
NSLog(#"app did not recieve notification");
}
here I am getting app did not receive notification method why ?
It's always a good idea to consult Apple's documentation first when it comes to their SDK.
Having a look here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/
You will see this extract:
When a remote notification arrives, the system calls the
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Notifications usually signal the availability of new
information. In your app delegate method, you might begin downloading
new data from a server so that you can update your app’s data
structures. You might also use the notification to update your user
interface.
If you're not using Push notifications, but local notifications, then you need this method: application:didReceiveLocalNotification:
The reason the method does not get executed is due to not receiving a remote notification.
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;
}