-
Notifications
You must be signed in to change notification settings - Fork 16
G.8 Understanding FCM Messages
With Firebase FCM service you can send different type of data to devices. Understanding the different between these messages can be very helpful to you when deciding how to build your application.
When you send a notification message, it means that all you want to do is to notify your users about a news or something and you don't wish to send any payload variables to them. When a notification message is sent, depending on the device's status, the FCM SDK will behave differently.
- If app is in foreground mode the
FcmEvents.MESSAGE
event will be dispatched so you know a notification message has been received. There will be no useful payload variables with it though. It's just to notify you! - If app is in background mode or killed, the device will build a notification which will be shown on the device's status bar. clicking on that notification will open your app. You should not expect to receive any payload variables on the InvokeEvent.INVOKE event because this is just a notification message.
A notification message you are sending from your server might look like the following:
{
"notification":
{
"title": "Title",
"text": "Your Text",
"badge": "4",
},
"priority": "high",
"registration_ids":
[
"cKXE9N6gZ_M:APA91bH04p4ZUFdyzqqKYUySPH3fTPYUWQGkYuT-yBGcpBcv_aW",
"cNAk9En1QfM:APA91bFWJ0YbgnsVfuJE2BxUDMmpnt6CeWQA0isZBTAxPHikxK6",
"cAMxajJxy2Y:APA91bHkJiRUhzsowbeHtFbsEAPv-ppaSgKpzeH08f12gppv9EU",
"dgNU5axXqfo:APA91bGgwGcVtDZKI81qMepH49oDUhphdwtqeyf-5TTSoktQ2AE",
"dSCy3H0D99I:APA91bEmylmTU9HJzPej4R1SChVjd5FskD61nX2wT8p4bwdzVpgv"
]
}
Data messages are the exact opposite of notifications in a sense that they never generate a notification alarm in your device by default. They will be received as a bunch of payload data. You may do different stuff in your app based on the received data. It totally depends on you. One of the things you can do with Data messages when they are delivered to your app is maybe to generate a custom local notification! When designing your application if you want to use Data messages, you must know how they work depending on the status of your app.
- If app is in foreground mode, the
FcmEvents.MESSAGE
event will be dispatched and it will give you access to the payload data through thee.msg
parameter. - If app is in background mode:
-
Android: Although the app is in background, the
FcmEvents.MESSAGE
event will be dispatched as soon as the Data message is received. -
iOS: unlike the Android side, the Data message will wait for the app to come to foreground mode first and then the
FcmEvents.MESSAGE
event will be called.
-
Android: Although the app is in background, the
- If app is killed, when a Data message is received, nothing will happen visually but these messages will pile-up each other and they will wait for when the application runs. As soon as the application starts and the FCM ANE is initialized, these messages will be dispatched through the
FcmEvents.MESSAGE
event.
A Data message you are sending from your server might look like the following:
{
"data":
{
"key": "value"
},
"priority": "high",
"registration_ids":
[
"cKXE9N6gZ_M:APA91bH04p4ZUFdyzqqKYUySPH3fTPYUWQGkYuT-yBGcpBcv_aW",
"cNAk9En1QfM:APA91bFWJ0YbgnsVfuJE2BxUDMmpnt6CeWQA0isZBTAxPHikxK6",
"cAMxajJxy2Y:APA91bHkJiRUhzsowbeHtFbsEAPv-ppaSgKpzeH08f12gppv9EU",
"dgNU5axXqfo:APA91bGgwGcVtDZKI81qMepH49oDUhphdwtqeyf-5TTSoktQ2AE",
"dSCy3H0D99I:APA91bEmylmTU9HJzPej4R1SChVjd5FskD61nX2wT8p4bwdzVpgv"
]
}
As the name suggests, these kind of messages are a mixture of Notification and Data messages. But you should keep in mind that this name "Mix Messages", is something that we have came up with to be able to explain the differences easier. The official Firebase documentation says that there are only two types of FCM messages. But I think it's a very good idea to consider a third type as we explain below.
NOTICE 1: Firebase documentation says that mix messages are considered as notification messages which can hold payload data with them.
NOTICE 2: If you are using the official Firebase console to send FCM messages, you should know that you are ALWAYS sending Notification messages which you may add payload data to it but if you wish to send Data messages, you have to send them from your own server. I can predict that Firebase console will be updated in future to support Data messages too, but as right now, (Oct 19th, 2016), their console can be used to send notification type messages only.
Mix Messages are used when you wish to notify users' devices while sending a payload data to the app. Depending on your app status, these messages work differently.
- If app is in foreground mode, the
FcmEvents.MESSAGE
event will be called and you will have access to the payload data through thee.msg
parameter. - If app is in background mode or killed, FCM SDK will build a notification in your device's status bar. When you click on that notification, your app will come to foreground (or will launch if it was killed before) and you can receive the payload data if your app is listening to the
InvokeEvent.INVOKE
event.
A Mix message you are sending from your server might look like the following:
{
"notification":
{
"title": "Your Title",
"text": "Your Text",
"badge": "8",
"click_action": "MY_INTENT"
},
"data":
{
"obj":
{
"Variable1":"value1",
"Variable2":"value2"
}
},
"priority": "high",
"registration_ids":
[
"cKXE9N6gZ_M:APA91bH04p4ZUFdyzqqKYUySPH3fTPYUWQGkYuT-yBGcpBcv_aW",
"cNAk9En1QfM:APA91bFWJ0YbgnsVfuJE2BxUDMmpnt6CeWQA0isZBTAxPHikxK6",
"cAMxajJxy2Y:APA91bHkJiRUhzsowbeHtFbsEAPv-ppaSgKpzeH08f12gppv9EU",
"dgNU5axXqfo:APA91bGgwGcVtDZKI81qMepH49oDUhphdwtqeyf-5TTSoktQ2AE",
"dSCy3H0D99I:APA91bEmylmTU9HJzPej4R1SChVjd5FskD61nX2wT8p4bwdzVpgv"
]
}
NOTICE 3: Make sure you are adding the "click_action": "MY_INTENT"
parameter in your notification
branch. Missing this parameter will prevent notifications from launching your app on the Android side.
we have written a very helpful post about how you can build your own FCM console for sending out FCM messages. You can find the article here.
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files