Apple push notification info is missing when we clear the notifications - ios

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;
}

Related

How a push notification behaves based on iOS application state

I want to understand what will happen if a push notification arrives to device.
App Not running - what will happen if notification arrives - state change?
App is in foreground - ?
App is background - ?
App is inactive state ?
I am assuming if App is not running we can invoke by sending a silent push notification in some situations.
Can some one explain me how a push notification works based on app state.
If the application is not running or in background state, if the user accepted to receive push notifications, the push notification will be sent to the device that will display it on screen.
From this displayed notification, you can launch or wake the target application.
The traditional launch callback:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
will be fired and you can access the received notification and its payload from its launchOptions dictionary, using UIApplicationLaunchOptionsRemoteNotificationKey key.
If the application is running and in foreground, the AppDelegate method
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo
will be fired, userInfo containing the push notification payload.

Remote Notification callback not being called during phone call

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.

Read all push notifications available in the notification bar in iOS

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
}
}

IOS DidReceiveRemoteNotification Issue

I am doing project in that push notification is the one of the key feature.It is working fine when i am in the app,I received notification and handle that notification.
But the issue is when my app is inactive state or else remove the instance of the app.In this scenario i have received the notification didReceiveRemoteNotification method is not called, and i didn't handle the push notification.
When the App is inactive and a push notification comes through the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method gets called when the app is resumed and the launchOptions dictionary has the push notification, which you can get with
launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
then you can process it as normal.

Receiving Push Notifications while in background

I know this is covered in a lot of places, but I cannot figure this out. I use Urban Airship for push notifications. Everything seems to be fine except that when my app is in the background - didReceiveRemoteNotification is not getting called. It works when in the foreground - I am able to properly process messages. And I am able to get to messages from launch options if message is tapped from notifications center. But when in the background, a message it send - iOS displays the alert - didReceiveRemoteNotification is not called. When I tap on my Application icon (not from within notification center) the app comes to the foreground and I have no idea the notification is present. Any ideas?
application:didReceiveRemoteNotification: will call in the background only when you have added content-available key with value 1 into the notification payload.
In case of the Urban Airship, you can send Test Push under the Setting tab.
Sample Payload for Push Notifications:
{
"aps": {
"alert": "aaaa",
"badge": "+1",
"content-available": "1"
},
"device_tokens": [
"86BA71E361B849E8312A7B943BA6B26A74AB436381CF3FEE3CD9EB436A12A292"
]
}
Apple has clearly mentioned in his documentation....
For a push notification to trigger a download operation, the
notification’s payload must include the content-available key with its
value set to 1. When that key is present, the system wakes the app in
the background (or launches it into the background) and calls the app
delegate’s
application:didReceiveRemoteNotification:fetchCompletionHandler:
method. Your implementation of that method should download the
relevant content and integrate it into your app.
https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
From the APNS programming guide :
Let’s review the possible scenarios when the operating delivers a
local notification or a remote notification for an application.
The notification is delivered when the application isn’t running in the
foreground. In this case, the system presents the notification,
displaying an alert, badging an icon, perhaps playing a sound.
As a result of the presented notification, the user taps the action button
of the alert or taps (or clicks) the application icon. 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.
I believe the last sentence describes your case, and explains why your application gets no information about the notification.
didReceiveRemoteNotification is calling ONLY if app in foreground or if app is just launched or is bought from background to foreground
link in Apple and some question
Method didFinishLaunchingWithOptions:(NSDictionary *)launchOptions parameter launchOptions one of the dictionary keys are UIApplicationLaunchOptionsRemoteNotificationKey which holds the pressed push notification info.
You can push received info after tour main root controller is initialised. I save it to some property and then push it after view is initialised.
if (launchOptions) {
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
self.notificationToMakeAction = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
}
}

Resources