Is it possible to send data payload through Firebase Console that we can receive in onMessageReceived() while our app is in background/killed?
It doesn't seem like the Firebase Console has the option to send "data" type messages. If you set the custom data fields in the Firebase Console, those will be delivered but only if your app is in the foreground. You need to implement your own FCM sender or use a 3rd party to send data type messages.
See this answer for more details about the two types of Firebase messages
Yes! but :)
When your application is in the background notification messages (which is what is sent from the console) are automatically displayed in the system tray. When the user taps on the notification the custom data is made available in the intent of the Activity that is launched.
Note that if the user never taps the notification there is no way to get at that data, so this data should not be critical for your application to function but should be used to enhance the user's experience once they have tapped the notification.
As you may have realised if your app is in the foreground, onMessageReceived is called and you can retrieve the custom data there.
Related
I have an android app which has a separate activity for notifications. I integrated firebase cloud messaging to my app for sending notifications to users. When I send a notification from the console, by default a notification pops up from the app. I want disable that notification and save the message from notification to a database. Later when user opens the app it gets the notifications data from the database and show them to the user. That means I don't want to show popup notifications to user, I want to show them only if user opens the app. My question is: How can I disable the default notification which pops up when a message is received?
If you want to send a message without a notification, you should not use the Firebase console to send that message. Instead, you will need to write some code on your desktop or backend, usually with the Firebase Admin SDK, to send a message with a data payload. The app will silently receive that message, and you can decide what to do with it from the callback you receive. Start with the instructions in the documentation to set up the client app. Be sure you are sending messages with only a data payload. It must not contain a notification payload.
i am developping an android application based on a wordpress blog where users can post their own articles, i work with WP REST API to get posts , comments and users ...
I would like to send a notification automatically to the user when his article got a new comment , i have installed FCM correctly and tested the simple forme of notifications but i need to :
Trigger FCM Notification on new comment sent ( Or just on a button click )
Specify receiver so the notification is sent to logged in user with the right email/username
I thought of
working with setting User Property for every user logged like this :
mFirebaseAnalytics.setUserProperty("user_name_for_notification", "the_username");
Sending notification for all devices then handle it in onMessageReceived
but i don't think those are good ideas
So , any ideas where to start
thank you
FCM can send notifications with three different targeting methods:
to a specific FCM token/instance ID token, which identifies an installation of a specific application on a specific device.
to a group of such device/instance IDs.
to a specific topic, which FCM clients can subscribe to.
Firebase Cloud Messaging does not have the concept of a user, therefor it cannot target users directly. If you want to target users, you will have to map them to one of the targeting methods outline above.
The most common ways that I know of:
Store the token(s) for a user in a database under their UID, and then send to the user's token(s) when you need to target the user.
Give each user their own topic, based on their UID. Have the app subscribe to that topic, and send message to the user's topic when you need to target them.
Also see:
How to send one to one message using Firebase Messaging
Sending message to custom user using firebase
Send Firebase Cloud Messaging notification to users by user property
how to send data message to user segment From Rest API
Needed a suggestion/idea.
I have to send the push notification to the android app when the data is changed/updated in the server.
I have got a news app, so whenever the news is being updated in the server, the user should get the push notification and when they click the notification, the app should open the link of the news in the app.
What would be the best approach to do so?
I know a bit about FCM, and have played sometime with it, learned about sending the notification from the FCM console, to all the devices using the topic.
Just wondering how can I implement it, rather than using the console, but by just updating the data in the server, and notification should be sent to all the devices which have got the app installed.
You can use your server to send FCM messages to your app without having to use the firebase console manually.
Just a simple API call from the server will push notifications (or messages) to the required devices.
For more information read the documentation on FCM Server.
There are multiple alternatives for opening activity of your choice after clicking the notification.
IMO, a good approach would be to send 'data' field from the FCM server instead of 'notification' field so that the method onMessageReceived will always get called. Inside that method, you can build your custom notification and set the intent to activity of your choice.
Another approach would be to check for extras in the launcher activity and if there is something coming from notification, open the activity of your choice and perhaps finish the launcher activity.
I am creating mobile application using latest version of Ionic 3. And I am at the point where I need to implement push notifications. I am using FCM. I have managed to done it for Android phones using this plugin: https://github.com/phonegap/phonegap-plugin-push. Reading documentation for this plugin, it says that for Android I should send only Data Messages and that that is the best practice. It also says that send Notification and Data payload would not work properly:
When your app is in the foreground any on('notification') handlers you have registered will be called. If your app is in the background, the notification will show up in the system tray. Clicking on the notification in the system tray will start the app and your on('notification') handler will not be called as messages that have notification payloads will not cause the plugins onMessageReceived method to be called.
But for IOS I need to send notification message with data payload. This would not be a problem if I target directly devices, however I need to send Topic Messages. So the way I see is that on my server I need to implement this logic:
Let's say that the name of topic is FOOD-AND-DRINKS:
function onSomeEvent($data) {
// send push notification to topic ANDROID~FOOD-AND-DRIKS
// send push notification to topic IOS~FOOD-AND-DRINKS
}
Also, since in the future my plan will include multiple cities, and not all people would like to listen for notifications for cities other the one where that person lives, so then my topic would be PLATFORM~CITY~CATEGORY.
So my question would be, is there any better plugin for handling notifications, or some service between my server and Google FCM and that service would take care of that, or this is quite OK what I have proposed?
You can use cloud functions to be able to send notifications since you are using FCM also.
More on this here: https://firebase.google.com/docs/functions/
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.
Using cloud functions you can use database triggers like onWrite() and it will be able to send notifications if you have data payload and/or notification.
But if you use data payload alone then you can send if android device is in background also.
Regarding Topics:
If you have multiple cities in your application, then you can let the user register to each topic. You can do in the database like this:
city-category
id_here
cityname: nameX
//other details
id_here1
cityname: nameY
//other details
I can't get data payload when app is in background.
Unable to put custom notification on Status Bar. Please Help?
If you are using the Firebase console then no. If you send a message from the Firebase console and your app is in the background then any custom data will only be available through the notification. If the user never taps on the notification that accompanying data is lost.
If you want to send data to your application that is critical to its operation then you should send data messages, which you can do via the FCM REST API.
See more on the different types of messages in the docs.