executing code in response to an iOS notification - ios

I need to execute code when my iOS app receives a notification, without involving any user interaction.
if the notification is a silent (i.e. non alert) notification DidReceiveRemoteNotification() does fire in both background and foreground modes.
However, silent notifications are relatively low priority and delivery is not even guaranteed, so this is not a viable option for us; we need to use alert notifications.
If the notification is an alert notification, DidReceiveRemoteNotification() does fire when the app is in foreground mode. However, it does not fire in background mode.
How can I execute code in response to an alert notification, in background mode, without involving user interaction. Is this even possible?
Thanks.

You can combine both silent notification and alert notification on one JSON.
by adding "content-available" : 1 as following :-
{
"aps" : {
"alert" : "Notification Title",
"content-available" : 1
}
}
This will show the alert notification as well as wake the APP to do work on background.

Related

Recive background notifcation iOS Swift 5 (Without body and title)

I am trying to receive a notification in the background to process information in my App when it is in Foreground, Background or Closed. I don't want to receive the Alert.
I have tried to do it without placing the Title and Body at the time of shipment, only Custom Data.
How could I receive this information without using an alert, regardless of the status of the App?
A silent push notification or some might call it background notification is a notification that does not trigger any alert or sound. It wakes up your app and allows you to perform any non-UI related operations in the background.
Sample payload for a background notification:
{ "aps" : { "content-available" : 1 }, "acme1" : "bar","acme2" : 42 }
You need to add “Background Modes” and “Push Notifications” capabilities in Xcode in order for your app to be able to receive a silent push notification.
Points to note:
You can only test push notifications in a real iOS device. iOS
simulator will not be able to receive any push notifications.
The background operation triggered by a silent push notification
will have roughly 30 seconds of execution time.
Silent push notifications will not work when the device is in Low
Data Mode.
Apple guideline for this topic to refer in more details.

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

Push-and-silent remote notification: Cancel alert when app running in background

I want to send a push-and-silent remote notification...meaning... I would
like to update data on the device BEFORE (and not at the same time) an alert message is displayed
to the user.
More Details
1 If the app is not running, I want to show the alert, which is why I added the alert key in the json.
2 If app is running in the background, I want to cancel the alert, and handle the notification silently: by first downloading content, and when ready trigger a local notification.
3 If app is foreground active state, alert is not shown, which is great, and I can handle the logic silently.
I have this
{
"aps" : {
"content-available" : 1,
"alert" : "This is my new notification",
}
}
But it's not working because
When the app is running in the background, the alert is not canceled, and is displayed at the same time as being handled silently.
So
Is there a way to cancel the alert if app is running in the background?
You need to remove alert from payload. Pass only badge and content-available property.
Enable Remote notification under your Application capabilities area. It should be under Background more section.

The purpose of "Content-available" in Push Notification Json?

The purpose is to send push notification with only badge value & nothing else (no banner).
I integrated parse sdk to test push notification & send this push notification
{
"alert" :"",
"badge" :"787",
"Content-available" : "1",
"sound" : ""
}
So the push notification got send when app is in background, foreground & when app is killed.
The purpose to wipe some data on arrival of push notification with badge valve 78 got succeeded.
I send same notification with "Content-available" : "1" removed but everything worked fine as earlier.
My understanding on "Content-available" was that putting it's value to 1 will allow push notification with no alert value.
So I am confused or I am missing something to know the meaning of "Content-available" in this push notification JSon.
Thanks
If you provide this key with a value of 1, (if user opens you app is in background or resumed) the application:didReceiveRemoteNotification:fetchCompletionHandler: will be called.
According to RemoteNotifications Programming content-available definition is
Provide this key with a value of 1 to indicate that new content is
available. Including this key and value means that when your app is
launched in the background or resumed,
application:didReceiveRemoteNotification:fetchCompletionHandler: is
called.
(Newsstand apps are guaranteed to be able to receive at least one push
with this key per 24-hour window.)
TL;DR:
"content-available" : 0: The default; your application won't be notified of the delivery of the notification unless the app is in the foreground.
"content-available" : 1: your application will be notified of the delivery of the notification if it's in the foreground or background (the app will be woken up).
The only time you need to use "content-available" : 1 is for background update notifications:
Background update notifications improve the user experience by giving you a way to wake up your app periodically so that it can refresh its data in the background. When apps do not run for extended periods of time, their data can become outdated. When the user finally launches the app again, the outdated data must be replaced, which can cause a delay in using the app. A background update notification can alert the user or it can occur silently.
However, this does NOT always mean that this notification will be invisible to the user:
If there are user-visible updates that go along with the background update, you can set the alert, sound, or badge keys in the aps dictionary, as appropriate.
By default, "content-available" is set to 0. These "regular" notifications do not immediately notify the app UNLESS the app is in foreground. Instead, these "regular" notifications notify the app when a user taps on them or has selected an option via a "Haptic Touch" on the notification.
Background update notifications are delivered to application(_:didReceiveRemoteNotification:fetchCompletionHandler:):
Unlike the application(_:didReceiveRemoteNotification:) method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background.
Note: there's a key distinction between "your application" and "the device:" the device will show the notification if the payload requests it to be shown, but this doesn't always mean that "your application" will be notified on the delivery of this notification (aka "your application" code will run). That's where "content-available": "1" comes in: "your application" will always be notified unless its been terminated.
Short answer: for me I just used "content_available" : "1", or "content_available" : true for resume the background/quit modes in iOS. Notice in my case it's underscore and not hyphen separated.
In my specific scenario my app was made in react-native and I have used https://rnfirebase.io for push notifications
Here a complete explanation of this:
https://rnfirebase.io/messaging/usage#data-only-messages
in IOS
content_available" : "1
equivalent in Android
priority: 'high',
In both cases the background message will invoke the onMessage() method when the app is resumed from background, so the program can run some specific code from there.
Here a sample of sending a push notification using CURL:
#curl -H "Content-type: application/json" -H "Authorization:key=#MyAuthHashCode#" -X POST -d '{ "to": "/topics/#thetopicnumber#","notification": { "title": "msg for topic", "body": "bodytext", "content_available": "true" }}' https://fcm.googleapis.com/fcm/send

Resources