Send Message - shaikhalvee/FCM GitHub Wiki

We use Advanced Rest Client Application.

Server Key

Settings > GLOUD MESSAGING > Server key : AAAAz3lrkJk:APA91...nHi

Send Message with Notification payload to single client (device)

Request URL:

https://fcm.googleapis.com/fcm/send

Header:

Content-Type:application/json
Authorization:key=AAAAz3lrkJk:APA91...nHi  // Server key

Payload

{
   "to": "fImrBgFR9vM:APA91bF...Vj0E"  // registration_id or cleint token, see
                                       // FirebaseInstanceId.getInstance().getToken()
   "notification": {
    "title": "Hello",
    "body": "This is test message."
   }
}

Message with Notification and Data payloads

Payload:

{
   "to": "fImrBgFR9vM:APA91bF...Vj0E"
   "notification":{
     "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
   }, 
   "data": {
    "title": "Hello2",
    "body": "This is test message."
   }
}

Setting ICON

Payload:

{
   "to": "fImrBgFR9vM:APA91bF...Vj0E"
   "notification":{
     "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "ic_directions_run_white_24dp"
   }
}

where ic_directions_run_white_24dp.xml is in res/drawable/

To replace existing notifications in the notification drawer

For this purpose we use tag

Payload:

{
   "to": "fImrBgFR9vM:APA91bF...Vj0E"
   "notification":{
     "body" : "3 : 1",
       "title" : "Portugal vs. Denmark",
       "tag":"1",
       "icon" : "myicon"
   }
} 

Start Activity by clicking on Notification

AndroidManifest

<activity android:name=".ActionActivity">
    <intent-filter>
        <action android:name="com.fcm.ACTION_ACTIVITY"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

Payload

{
   "to": "fImrBgFR9vM:APA91bF...Vj0E"
   "notification":{
     "body" : "4 : 2",
      "title" : "Italy vs. France",
      "icon" : "myicon",
      "click_action":"com.fcm.ACTION_ACTIVITY"
   }
}

Scheduling

Unfortunately, as of the moment, there is no API or even a parameter that you can use in order to schedule a notification.

Send to a single topic

Payload:

{
   "to": "/topics/news",
   "notification":{
     "body" : "We have a news for you",
      "title" : "Read this now!",
      "icon" : "myicon"
   }
}

Here topic is "news". To create a topic, we call in client:

FirebaseMessaging.getInstance().subscribeToTopic("news");

Send to devices subscribed to topics "news" or "radio"

Payload:

{
   "condition": "'news' in topics || 'radio' in topics",
   "notification":{
     "body" : "We have a news for you",
      "title" : "Read this now!",
      "icon" : "myicon"
   }
}

Creating a device group

https://android.googleapis.com/gcm/notification
Content-Type:application/json
Authorization:key=AAAAz3lrkJk:APA...XtnHi
project_id:891095322777   (Overview -> CLOUD MESSAGING -> Sender ID)
{
   "operation": "create",
   "notification_key_name": "appUser-Ievgen",
   "registration_ids": ["fheS3m4LmoM:APA9...v7K",
                        "d0QCxkOrfRE:APA91...QZTOd-"] 🐪
}

Response

{
"notification_key": "APA91bHcOB3n6Dnu6rUM0m8yu3sAtTd4r...CfE" 🐙
}

Sending a Message to Group

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AAAAz3lrkJk:APA...XtnHi
{
   "to": "APA91bHcOB3n6Dnu6rUM0m8yu3sAtTd4r...CfE", 🐙
   "notification": {
    "title": "Hello",
    "body": "This is test message."
   }
}

Response:

{
"success": 2,
"failure": 0
}

Removing a device from group

{
   "operation": "remove",
   "notification_key_name": "appUser-Ievgen",
   "notification_key": "APA91bHcOB3n6Dnu6rUM0m8yu3sAtTd4r...CfE", 🐙
   "registration_ids": ["d0QCxkOrfRE:APA91...QZTOd-"] 🐪
}

If you remove all existing registration tokens from a device group, FCM deletes the device group.

⚠️ **GitHub.com Fallback** ⚠️