Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push silent notification through GCM to Android/IOS

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

like image 617
Onza Avatar asked Apr 11 '16 17:04

Onza


People also ask

How do I send silent push notifications?

There are two ways users can receive silent push notifications on Android. Users can long press on a notification to get an option to display notifications silently. Users can also enable silent notifications by heading to Settings > App & Notifications > Search for app and choose> Notifications, and turn it off.

Will iOS awake my app when I receive silent push notification?

Yes. It will. When you click on that. Yeah its ok but being push notification is silent,you are not able to view any alert and cant click.

What is GCM push notification?

Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...


2 Answers

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

like image 155
tyczj Avatar answered Sep 28 '22 11:09

tyczj


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.

like image 20
Kristian Barrett Avatar answered Sep 28 '22 11:09

Kristian Barrett