22

I'm seeking to send an state update to an application, which can (or not), be silent.

For example, if the user gets a new message from one of their friends I want to send such message and be able to display an alert with sound.

But in the silent case, I want the user not to notice such notification. it must be gathered by the application; for example if my user has two devices and he changes his name in one of them, I want to send the other device (which is sleeping) a silent name update, that should trigger a change (be dispatched to the application) in background.

According to what I've read this is actually possible, but I'm quite confused regarding to how it actually is, or how it should be done, and there are some contradictions at times. As a backend developer I can do anything but I need to make sure that it works for the frontend guys.

Here some relevant topics:

Silent background push on iOS and Android

Push Notifications without alert

iPhone push notification without alert

Android Silent Push

Community
  • 1
  • 1
Onza
  • 1,710
  • 4
  • 18
  • 31

2 Answers2

55

Android:

UPDATE: New Documentation for FCM: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

As long as you do not include a notification tag in your payload and put a data tag in it you get a silent notification

this example would show a notification

{ "notification": {
    "title": "Portugal vs. Denmark",
    "text": "5 to 1"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

this would not show a notification

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

go here to read more https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages

iOS

add the tag contentAvailable: 1 to your json payload and you get a silent notification

its that simple

Dhara Bhavsar
  • 345
  • 4
  • 11
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • 5
    hi thanks for your answer. in android user can disable push notifications. What happens to silent push if user disables push notifications for the app from settings? still can receive daha notifications? – savepopulation Feb 06 '17 at 08:27
  • 2
    the proper tag for iOS is "content-available", and you must make sure that you don't include a "sound", "badge", or "alert" key to the "aps" dictionary. You also have to configure your app to have the "Remote notifications" Background Mode. https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH10-SW8 – Alec Mar 17 '17 at 18:51
  • My experience is that it does not work like this. If you send a "data" attribute than the notification in tray is still displayed with default application name. If "data" contains "title" and "body" then these two are displayed. Applies to Android, haven't tested on iPhone yet. – Jiri Matejka Nov 19 '21 at 20:18
9

The accepted answer is pointing to a deprecated documentation.

You should use this instead: Firebase documentation

Data message looks like this:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data":{
      "Nick" : "Mario",
      "body" : "great match!",
      "Room" : "PortugalVSDenmark"
    }
  }
}

And the notification message:

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}

Hopefully this will make some other people avoid the headache I had from the accepted answer.

Kristian Barrett
  • 3,574
  • 2
  • 26
  • 40