Track remote push notifications when app in background - ios

I use Urban Airship to handle remote push notifications for my iOS app. It works as expected, and I get notifications both in foreground and in background. I would however like to track all incoming remote push notifications (even if they are not executed). So if a remote push arrives, I would like to invoke some code to record what kind of notification it was. I seem to only be able to handle when a user taps a notification, and not when it is received. Is this possible?
I have set Background mode to "Remote notifications", implemented both didReceiveRemoteNotification in appdelegate and a both receivedBackgroundNotification for urban airship. I have also set backgroundPushNotificationsEnabled to yes.
Any ideas if this is even possible?
Here is an example of a push notification:
{
"_" = "Q-Pr3MeyLaEeS8csDiudL0-A";
aps = {
alert = "A simple message";
badge = 53;
};
id = ea591946c21d437f8fc1c851e1c1e728;
pid = 6a26766232e14d971dc1417a0c217bfc;
type = c1;
}

Related

How handle silent push notification when application is InActive state in ios 9?

I have implement silent push notification.So "didReceiveRemoteNotification" method called when application is inactive state in ios 9.
There are some case when application is inactive state.
1.When user tab on particular notification.
2.When call or message receive.
3.When notification center and control center open.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive) //Inactive state
{
[self RedirectScreenBasedOnNotification:self.userInfoDic];//Screen Redirection code
}
}
So how can i handle silent notification when app is inactive state?
I have face problem is when notification center open at that time if any notification come then redirection will do,but i want to stop that.
Notification payload:-
aps = {
alert = "Test Dev 5 startd following you ";
"content-available" = 1;
"link_url" = "https://raywenderlich.com";
message = {
friend = {
email = "abc#gmail.com";
name = "Test Dev 5";
photo = "";
"user_id" = 27;
};
id = 3;
"is_business_sent" = 0;
message = "Test Dev 5 startd following you ";
};
sound = default;
}
Thanks in advance
Silent push notifications do not trigger user interactions. When a silent notification payload includes keys for user interaction things go wrong - iOS can't reason about wether the intent is to present something to the user, or to keep the notification silent and handled without user interaction. Sometimes the silent notification may work, other times it may be presented like a normal notification with user interaction. It can be one or the other, not both.
If the silent push key content-available is present in the aps payload the keys alert, sound, or badge should not be.
You can use my Push Notification Payload Validation Tool to check the content of your notification. The payload you posted in your question has several problems - the aps key should only contain Apple keys defined in Generating Push Notifications. All of your custom keys and values should be outside the aps object.
application:didReceiveRemoteNotification:fetchCompletionHandler: will only be called for silent push notifications. If the notification payload contains both content-available and one or more of alert, sound, or badge iOS will not know which method to call and you may see inconsistent behavior.
If you are just trying to show a non-silent notification you do not need to implement application:didReceiveRemoteNotification:fetchCompletionHandler:. Instead implement application:didReceiveRemoteNotification: for iOS 9 and userNotificationCenter:willPresentNotification:withCompletionHandler: for iOS 10 and later.
As far as silent notifications and the inactive application state, there is nothing special to be done here. Silent notifications are intended to 'hint' to the application that it should refresh content. When a silent notification is received the application is required to process the content update within 30 seconds and then call the fetch completion handler. When iOS executes the fetch completion handler it take a new restoration snapshot of the updated UI. This happens even when the application is inactive.
You can add your code in this If condition.
if (UIApplication.sharedApplication.applicationState != UIApplicationStateInactive) {
//Write your code her, this will get executed when your app is not in Inactive state.
}

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

iOS - Detect Push Notifications when the App is terminated by the user

I have a problem. When my App is Terminated by the user, push notifications are not detected by the application. The push notification is sent with content_available = true.
What should I do?
This is default system behaviour. If you Application is terminated by the user (from the App switcher), Silent Push Notifications (content_available = true) will not wake the Application, i.e. Application:didreceiveremotenotification will not be called.
If you want the user to be notified, do not send a Silent Push Notification. Send a normal push notification which will show up in the user's notification tray.
That is the way how it works on iOS.
If you app is not running at all, your app receive no push notifications at all. Only if the user swipe over one of your push notifications on the lock screen or the notification center your app will be started and you will be notified that your app was started because of the push message.
If you app is in the background, you actually can handle push notifications by enabling "run in background" support.
I'm talking iOS9 and earlier here. Not sure if the behaviour has been changed in iOS10. But if you are coming from Android then you have to accept that push notification handling works completely different on iOS than on Android.
Sending the notification with the content_available as disabled. content_available = 0
The content_available field is used for sending silent push notifications to process in the background and will not display as a notification.
See documentation : https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW6
Hope this helps,
DT
Make sure that in your push notification payload you are adding priority:"high". It will ensure that your app will receive a Push Notification in background or closed mode.
{
"to" : "/topics/{userId}"
"content_available":true,
"priority":"high"
"notification" : {
"title": "",
"body":""
},
"data" : {
//custom key value pairs
}
}

Not receiving push notifications in background, only foreground (no alerts)

I've set up my app for push notifications, and for the past few days of development I've received zero notifications when the app is in the background. No alert, badges, sounds -- nada. Nothing shows up in the notifications area when you swipe down the screen from above.
But when the app is in the foreground, application:didReceiveRemoteNotification: does indeed fire off. When I first started messing around with notifications, I was getting them, but at some point it just stopped working. I've verified that the phone has notifications enabled for the app.
I'm using Quickblox to send cross-platform push notifications, and they have a control panel on their website where you can send notifications. When I sent one through there, I receive it in the background. On the foreground, this is what I see as the log output for the ones sent from the control panel:
2015-07-11 00:11:26.755 Viewerapp[6671:2177549] didReceiveRemoteNotification userInfo={
aps = {
alert = "Testing push notification";
sound = default;
};
}
And this is what I receive when the client app uses the Quickblox API to send a push notification:
2015-07-11 00:14:14.907 Viewerapp[6671:2177549] didReceiveRemoteNotification userInfo={
QBUserID = 2574021;
aps = {
alert = "";
};
messageOS = Android;
messageText = "Push notification test";
messageType = "simpleMessage";
userName = John;
}
Is the problem that the latter lacks data in aps? Does it seem like it's a Quickblox issue?

Execute few lines of code when received silent push notification and app is running in background, ios

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(#"PUSH NOTIFICATION %#",userInfo);
if([userInfo[#"aps"][#"content-available"] intValue] == 1){
NSLog(#"SILENT PUSH NOTIFICATION");
//Here I'm inserting a flag value to my DB.
completionHandler(UIBackgroundFetchResultNewData);
}
else{
NSLog(#" GENERAL PUSH NOTIFICATION ");
completionHandler(UIBackgroundFetchResultNoData);
}
}
My silent notification payload is {
aps = {
"content-available" = 1;
sound = "";
};
}
To support I have added a key in info.plist in "Required background modes"
item 0 = App downloads content in response to push notifications
Also in capabilities section my Background Modes are ON with remote notification check mark is selected.
Now my question is when I run my app then I'm able to receive silent as well as general push notification and code is executing successfully in foreground mode but when I press home button (not forcefully quitting by swiping it away and also device's passcode lock is open) my app is going to background mode and then my code is not executing even I'm able to receive push notifications those are having alert and sound, but app is not launching in background mode.
I'm thinking that for foreground and for background mode whenever my app is receiving any push notification don't matter is it silent or general push notification my first log in the delegate method should have to print on the console i.e NSLog(#"PUSH NOTIFICATION %#",userInfo);
Please help I'm struggling with this since last 2-3 days.
My info.plist
I am doing almost exactly the same thing as you except I use the silent push notifications to present a local notification when the app is in the background and use it to update some state when the app is in the foreground. I am NOT registering for background fetch, only "remote-notification".
#Paulw11 is correct that you need to call the completionHandler each time didReceiveRemoteNotification is called.
I would check your plist to make sure your background modes are configured correctly. Maybe you could post the contents of your plist.
My notification payload looks exactly the same as yours so I don't think that is the problem. I initially had issues because the payload wasn't exactly right.
Keep in mind that if you kill the app (swipe it away), you will not get silent push notifications until the app is restarted.
I think you need to test once with 'content-available' outside 'aps', like the following:
aps = {
alert = "This is test alert";
badge = 1;
};
"content-available" = 1;

Resources