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.
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 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.
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
}
}
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;
}
I'm using parse.com as my APNs provider for a test app I'm building. I've confirmed that push notifications are working as I have been able to successfully receive alerts when the app is in the foreground. Also, I have the remote-notification value for the UIBackgroundModes key specified in my plist.
In my app, I'm wanting to send a user's current location data back to my app-specific parse.com database when a push notification is received. I don't actually care about the notification payload itself, as the notification is just a means to getting a small piece of info. The app is constantly collecting data in the background and storing it in a persistent NSDictionary.
I've put the location sending code in the application:didReceiveRemoteNotification: method. If my app is in the foreground when I receive a notification, the method gets called. If my app is in the background, the method isn't called.
Am I doing something wrong?
Is it even possible to execute an API request in application:didReceiveRemoteNotification: when the app is in the background and the user hasn't interacted with the notification?**
EDIT: The problem persists even when I use application:didReceiveRemoteNotification:fetchCompletionHandler:.
Note: My NSDictionary full of location data isn't empty. Also, I am not attempting to do any UI manipulation in the background - just trying to perform an API request.
Check the following:
Notification payload includes "content-available"
{"alert":"",
"badge":"0",
"content-available":"1",
"sound":""}
The documentation for UIApplicationDelegate -application:didReceiveRemoteNotification is pretty clear on this method:
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.
You need to handle notifications in both methods to be able to respond when your app is in the background and foreground.
you can try to use this instead of
application:didReceiveRemoteNotification
method, since you need to fetch your push in background mode, thus, this would works when the app is in background mode. However, you might need to add in custom notification or UIAlertView when app is in foreground to display your message. Hope it helps and it's not too late.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"Remote Notification Received: %#", userInfo);
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"message to be displayed";
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
completionHandler(UIBackgroundFetchResultNewData);
}
Alex L's answer almost worked for me.
Just make sure the value of content-available is 1 (number), not a string "1":
{
"alert":"",
"content-available":1,
"sound":""
}
badge parameter is optional.
(Using Parse Server for APNs)