Firebase Cloud Messaging Send Message Via HTTP Protocol
How do I send a message to device using Restful API?
Setup Post Method
The Url:
1 | https://fcm.googleapis.com/fcm/send |
The Header:
1 | "Content-Type": "application/json", |
Replace
The BODY:
Check Http-Server-Ref and About FCM messages | Firebase official documentation for detail JSON object you can use.
There are three type of notifications.
- Notification Messages
- Data messages
- Notification With Data Payloads.
For Notification Messages, android display the heads-up notification depends on the JSON you send. (title, subtitle, icon, etc.). Will simply launch app when click on the message. If you want specific behaviour after click on message, you can choose Data Messages or Notification With Data Payloads
For Data Messages, app will call OnMessageReceived to handle the message as soon as it gets received. The method will be fired even if the app not opened. You will need to create heads-up message yourself under function OnMessageReceived
You can also build a push notification with both Notification Messages and Data Messages. I call it Notification With Data Payloads. In this case, android first display the heads-up notification depends on JSON in the notification section: (title, subtitle, icon) etc. If you have specify the ”click_action” in the notification section, it will launch the activity you specified there and put all the data payloads into the activity intent extras.
Notification Messages
For BODY of Notification Messages:
1 | { |
to: Device token
notification: The notification object.
title: Title for notification.
body: Body for notification
click_action: The click action for notification. Usually be some kinds of activity to launch. (OPTIONAL)
icon: The small icon for notification. String for drawable resource. (OPTIONAL). With out icon, a default one will be used if meta data is implemented in android manifest file.
Data Messages
For BODY of Data Messages:
1 | { |
to: Device token
“data”: Any self-defined data object.
The data object can be anything self-defined. Just make sure you implement the same key in the onNotificationReceived for your custom object extends FirebaseMessagingService
Notification With Data Payloads
For BODY of Notification With Data Payloads
1 | { |
For detail of each field, visit Notification Messages and Data Messages. This is just a combination of the two.
However, please notice that the “click_action” here becomes required. Because you will need it to launch the activity to handle the data payload. After the activity launched, get the payload data hash map by accessing getIntents().getExtras()
