Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 655
Nandkishor Chaudhari Avatar asked Jan 05 '15 10:01

Nandkishor Chaudhari


3 Answers

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.)

like image 191
Midhun MP Avatar answered Oct 22 '22 15:10

Midhun MP


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.

like image 31
kgaidis Avatar answered Oct 22 '22 16:10

kgaidis


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

like image 1
Cassio Seffrin Avatar answered Oct 22 '22 16:10

Cassio Seffrin