Get push notification as background app on iOS - ios

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
}

Related

iOS doesn't receive Push notification when the app is being killed

I have found similar questions on Stack Overflow, but none of them have cleared the point.
I am using Firebase Cloud Messaging for sending push notifications in my app. I am storing the messages received in a local database. When my app is active or in the background, my app is able to receive the notifications (delegate methods called properly) but if the app is being forced quit or not in the memory then also the device is receiving the notifications and sat in notification center but the none of the delegate methods called when the app is being launched by an icon. If a user clicks on Message in the notification center, then the app gets launched, but only the message clicked on is being received and not all of them (in the case of multiple notices have been received).
According to Apple - 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.
But even if the user launched the app still not receive the notifications which were received and sat in the notification center.
Here are the points followed by the app:
My app has no VoIP functionality.
Content-available has been set to 1.
Has enabled to receive background remote notifications.
All notifications sent has been received and displayed in the notification center.
{
aps = {
alert = {
body = "Push Notification Test Message";
title = Push Notification;
};
badge = 1;
"content-available" = 1;
sound = default;
};
"gcm.message_id" = "0:1499340350307980%361a2e5b361a2e5b";
m = "Push Notification Test Message";
tag = m;
}
If the app is being forced quit or not in memory then your app will not receive silent notifications(i.e. Content-available set to 1).
Only push notifications are received in above conditions.
If user clicks on Message in notification center, then app gets launched and only that clicked the message will be received in delegate methods.You cannot access all the messages in the notification tray.
There is no way to get push notifications for the application if app is not running. This is restriction . You are only can get and clear local notifications. So the another way of resolving your problem is saving your notification on the backend when you send it. Then after launching app , you can get notifications list from the server , and match it with id . You can send any parameters that you want in notification playload.
Your app should not rely on the delivery of push notifications. Delivery of push notifications is not guaranteed, as you have found.
Even if your app isn't terminated you may not receive all notifications; For example if the device is in airplane mode and multiple notifications are sent, only the last one is delivered when the device comes back online.
Your app should check with the server for new messages when it is launched or when a notification is received in order to "catch up".
You can also consider including a message payload in your silent notifications. This will be displayed to the user if your app is terminated to prompt them to open your app.
You need to handle method for push notification when the application is open from a tap on any notification. As soon as you tap on notification application will awake and lunch it.
In didFinishLaunchingWithOptions, you need to put below condition:
if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
It also called below method when notification is tapped:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
}

Handling push notifications iOS when app is force-quit by user (Alternatives?)

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.

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

Detect if the app was launched/opened from a push notification and then redirect it to particular view controller

What I am doing is when push notification received I am navigating to particular view controller depending on user info I get from that notification. This navigation and user info is received from method
didReceiveRemoteNotification
This is working perfectly fine when app is in background and receive notification.
But when my app is force stopped then I get notification and after click on that my app launches first time so then didReceiveRemoteNotification method is not getting called and I am not able to redirect my app to particular view controller . It just opening first page of my app.
How can i detect that my app is launch from push notification and get info from that notification same which i get from didReceiveRemoteNotification and now I need to redirect it to particular page? My app support is from iOS 7 and above.
Whats up is doing this type of senario , but can't figure out how to do that.
You can detect the notificatin from didFinishLaunchwithOptions:(lauchoptions) method.
In this method, use lauchoptions to read the notification dictionary like
`
if (launchOptions) { //launchOptions is not nil
userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
apsInfo = [userInfo objectForKey:#"aps"];
}`
and then set your window.rootviewcontroller based on notification key.

Taking action on app start up based on Push Notification

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
}

Resources