33

Heloo, I am building an app where I am using push notifications via Firebase Console. I want to know what is a difference between simply push-notification and cloud message? Is it that messages from cloud messaging are data messages(have key and value) and notifications are just text without key and value?Am I right?

1 Answers1

65

Firebase API has two types of messages, they call them:

  • notification
  • data

Explanation:

  1. notification - messages that goes directly to Android's Notification tray only if your application is in background/killed or gets delivered to onMessageReceived() method if your app is in foreground.

Sample:

{
    "notification" : { "body" : "Hi"}
}
  1. data payload - Doesn't matter whether your application is in forground or background or killed, these messages will always be delivered to onMessageReceived() method.

Sample:

{
    "data" : { "message" : "Hi", "whatever_key": "value"}
 }

Reference link

IMPORTANT : You can't send data payload messages from Firebase Console, Console only delivers notification message. However using API you can send both type of messages.

To send a data payload message you have to make a curl request:

HTTP POST Request

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}

You can get the server key (AIzaSyZ-1u...0GBYzPu7Udno5aA), from firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

Hisham Muneer
  • 8,558
  • 10
  • 54
  • 79
  • Thanks mate, but I think that from some day ago, it is possible to send data messages from Firebase console. Your project -> Notifications -> New message -> Advanced options -> Key and Value options....Please check and give me reply –  Jun 22 '16 at 07:04
  • @Atenica Nope, its still not possible to send data payload through Console. – Hisham Muneer Aug 18 '16 at 11:17
  • @HishamMuneer can you show how we can send send data to firebase realtime database.Because by using the technique in your answer we need to get either device ids of all the android devices or we need to subscribe at android device end.So i just want to push data to firebase server so that i can work independent of device ids or subscription of android devices.. – Dragon Jul 11 '17 at 20:14
  • @Dragon, Maybe I didn't get your question here, but to do anything in the realtime database, you need to use FirebaseDatabase api for that. Can you check this if it help you: https://firebase.google.com/docs/database/android/read-and-write – Hisham Muneer Jul 14 '17 at 07:17