I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:
In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So, when I'm trying to access the extras in the intent, the keys of the notification are not found.
My code in MainActivity.java:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
Object value = getIntent().getExtras().get(key);
Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
}
}
The notification we are sending from the server:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Anyone has an idea of how I can retrieve the values inside the notification?
This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.
The data being referred to in (emphasis mine):
In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
is the data message payload (See Message Types).
You'll have to send a payload with both notification and data. Like so:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Also see my answer here.
All you have to do is update your notification push format.
From:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
To
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
Explanation:
There is a firebase service running by your system which works for all app and handle all push notification if it contains the "notification" tag. but if contain the "data" tag only it just handover the task to the service created by the app where you can do anything.In your case show user notification and save it to the local db. For more you read here Documentation : Handle Messages.
It's not a simple or clean solution, but you could use a NotificationListenerService to receive a callback for each notification. A further limitation is that it supports only API 18 and above.
Add this to manifest file
<intent-filter>
<action android:name=".MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Related
I am sending notification and handle notification FirebaseMessagingService.
But if app on background FirebaseMessagingService cannot handle notification and not show image.
How can I do this? (If the application is in the foreground, the picture is shown in the notification.)
And if I use onesignal image showing all times.
{
"to" :"4sQrChOF16uMYYEKeiRz6dzCnN3m9OCE9jwOyPBOD92IlzljWvQ_1quiYyluP",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data": {
"body" : "great match!",
"title" : "Portugal vs. Denmark2222",
"img_url" : "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRYJ_0NZFg0htNXwmKkyTv76bw05EacXaXNnqd4ZrPB7wVTXNxR"
}
}
Here are 2 ways you can show an image in your notification consistently:
[Recommended approach] - Using the newer HTTP V1 API of FCM:
Here you can give an image URL as part of the notification payload and firebase will handle downloading and displaying the image for you. Here's the documentation link for the same.
Using the older legacy API of FCM:
Send a data-only payload(Don't send the notification object at all). Include image URL as part of the data payload. This will give you control in the onMessageReceived method(in both foreground and background cases). Here you can generate and display the notification however you like it. Retrieve the image URL here and download the image and create a notification with the image or however you want it to be rendered.
My application is running in the background and I send a push notification through postman using the payload below.
Now when I send this, I get the notification and when I tap it, it launches my application's Main Activity. But I want to launch the web browser which opens the link that I send in the notification body. How to do this?? Can somebody please help.
{
"to" : "MY_TOKEN",
"collapse_key" : "type_a",
"notification" : {
"body" : "https://developer.android.com/training/notify-user/build-notification#click",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2",
"click_action" : "OPEN_WEB"
}
}
I'm working on an app in which i get from the server some push notifications when some events are triggered.
When the application is in foreground all works great, in the onMessageReceived() i get the remoteMessage and handle it based on its map key value.
The problem is in background or when the application is not running...I still get the notification in the device screen, and when i tap on it the activity starts.
I do the following verification after the activity starts:
Bundle mBundle = getIntent().getExtras();
if(mBundle != null)
{
//do stuff
}
But i always get the mMap = null in the bundle object which means i cannot handle properly the push notification.
Can something be done about this ?
There are two types of Notification Message
1. Notification message
Notification Message can be fetched when application is running, but if the application is not running(killed) then it will not work. It only shows that notification is arrived.
example,
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
2. Data message (also known as silent notification)
Whereas Data Messagegs works even if the application is not running.
example,
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
I'm using FCM. Notifications are working properly and onMessageReceived is called on Foreground, but when I receive a notification while the app is in background/killed that method is never called.
I'm using data messages as Firebase Documentation suggested but no succeed...
FCM has 3 kind of notifications
1. Notification messages
Json body will be only having notification tag
FCM will automatically show notification in client app if app is in background. onMessageReceived() will be called both in foreground . In background, onMessageReceived() will not be called.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
2. Data messages
Json body will be only having data tag
Developer have to show notification. onMessageRecieved will be called both in foreground as well as in background. The json should only have data tag in it
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
3. Notification and data messages
The json body will be having both notification and data tags. onMessageReceived() will be called only when app is in foreground. Notifications will be shown automatically shown if app is in background and onMessageReceived() will not be called.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
For more info please read https://firebase.google.com/docs/cloud-messaging/concept-options
Are you sure you add like this service in
AndroidManifest.xml
<service android:name=".services.fcm.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
this is the notification I'm sending:
payload = new JSONObject();
notification = new JSONObject();
notification.put("title", "Avisos");
notification.put("text", "Nuevos avisos disponibles");
payload.put("data", notification);
payload.put("to", "/topics/avisos");
payload.put("priority",10);
We are using the Google Cloud Messaging API to push Android notifications. We are executing the https://android.googleapis.com/gcm/send endpoint with a payload similar to:
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!"
}
}
The notification works and the phone shows our app logo, and the message from above. However, there is no title in the notification (above the message and to the right of the logo):
For reference, we are using Phonegap 3.3 and PhoneGap Build.
Well, a stab in the dark of adding "title" fixed it. I wasn't able to find this documented at http://developer.android.com/google/gcm/server.html#params
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!",
"title" : "Title"
}
}
Now in docs:
https://developers.google.com/cloud-messaging/server#payload
Have a look at the documentation here:
{
"registration_ids" : ["..."],
"notification" : {
"body" : "You received cash back!",
"title" : "Title",
"icon": "ic_notification" // replace with your own
}
}
Edit: cloud messaging is now deprecated and this example is no longer valid. The link to the updated documentation is at firebase
its working my side
$message = array("message" => $pushMessage,"title" => "My Notification");
$fields = array(
"registration_ids" => "device id to send notification",
"data" => $message
);