i experience pretty weird thing in my android app. In some unknown for me cases sometimes blank notification apperas in notification bar, without any title, body and with default gray color. The only filled thing is icon, which is my default application icon(from manifest).
There is only one place in project when i create notification manually - intent service that sends data via rest api. Notification have his own icon (different than default), color, text, progressbar and works fine when service is running. I have not configured any Cloud Messaging or push notification in this project.
I spent a lot of time and i have no idea why blank notification described above appears. I will be grateful for any hint how to prevent it.
Yes, I am also facing this issue, when the app is closed that time notification will appear like this. I am resolved this issue in server side.
There are two types of messages in FCM (Firebase Cloud Messaging):
Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground
Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed
Firebase team have not developed a UI to send data-messages to your devices, yet.
refer this link to achieve
https://fcm.googleapis.com/fcm/send
And the following headers:
Key: Content-Type, Value: application/json
Key: Authorization, Value: key=<your-server-key>
Body using topics:
{
"to": "/topics/my_topic",
"data": {
"my_custom_key" : "my_custom_value",
"other_key" : true
}
}
Or to send it to specific devices:
{
"data": {
"my_custom_key" : "my_custom_value",
"other_key" : true
},
"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}
NOTE: Be sure you're not adding JSON key notification
NOTE: To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
Related
I upgraded from GCM to FCM and now I'm testing the experience of upgrading users
I have the following steps
install the app version that supports GCM
Upgrade to a new version that supports FCM
Send push message on Firebase console
The problem is that I see the message received on the notification area in the device, but it doesn't invoke the code CloudMessagingListenerService extends FirebaseMessagingService
I have a debug log at the beginning of CloudMessagingListenerService#onMessageReceived
After I go to the app info and select force stop and reopen the app my code inside CloudMessagingListenerService#onMessageReceived is called properly.
Any idea what causes this and what can be done to avoid the force kill (my users won't do it voluntarily)
in both scenarios the app is in the background
Update:
The problem is w/ receiving messages from the Firebase console.
I added logs in CloudMessagingListenerService#onCreate and CloudMessagingListenerService#onDestroy and I can see that they are called w/ a 100ms time diff between them - while CloudMessagingListenerService#onMessageReceived isn't called so it seems FirebaseMessagingService decides to handle the notification but not the data portion
Regarding messages sent from the server:
this is the json I send from the server - the data part is propagated correctly to my code, but the notification is ignored, when I check what is the content of remoteMessage.getNotification() I get null
{
"to": "my token",
"collapse_key": "my app",
"notification": {
"title": "server title",
"body": "server text"
},
"data": {
"tag": "debug",
"action": "custom-push",
...
}
}
Firebase Console sends the message in the notification key, which means it is only delivered to the onMessageReceived() if the app is running in foreground/open. If the app is running in the background the notification will show up on the notification drawer directly.
To receive the payload in onMessageReceived() of your class extending FirebaseMessagingService make sure the the notification content is passed in the data key of your request to the made to the FCM server. This will ensure that the payload is delivered to the onMessageReceived() irrespective of whether the app is in foreground/background.
I'm new to Cordova and JS stuff, and currently making a Cordova mobile app. The purpose is to send notifications with the help of firebase (I'm focusing on android for now).
The notification has to be send with a phone, to an other phone.
I use JQuery Mobile 1.4.5, JQuery 2.1.0 and Cordova CLI 6.1.1
My project is a Visual Studio project (techno restriction)
I'm using this Cordova FCM plugin: https://github.com/ostownsville/cordova-plugin-fcm
My code to receive the notifications works pretty well: No errors, token generated successfully, notification received ! (notifications sended by firebase console, with the token, etc.).
==> My problem here is that I don't know how to proceed to send notifications to an other phone !
I already have a payload example, that can be use to send the notification. but I don't know how to use it. I'm sure I have to put it in firebase, somewhere, but i don't know where.
Plus, I can't figure how to obtain the token of the phone that I want to send a notification. Is it possible to know the receiver token ?
here is my payload code:
var fcm_server_key = "aiz******************************";
method: "post",
datatype: 'json',
headers: { 'content-type': 'application/json', 'authorization': 'key=' + fcm_server_key },
url: "https://fcm.googleapis.com/fcm/send",
data: json.stringify(
{
"notification": {
"title": "title ", //any value
"body": "body ", //any value
"sound": "default", //if you want notification sound
"click_action": "fcm_plugin_activity", //must be present for android
"icon": "fcm_push_icon" //white icon android resource
},
"data": {
"param1": "value1", //any data to be retrieved in the notification callback
"param2": "value2"
},
"to": "<USER_TOKEN>", //topic or device ==> maybe the place to put the token !?
"priority": "high", //if not set, notification won't be delivered on completely closed ios app
"restricted_package_name": "" //optional. set for application filtering
}
)
==> Is it possible to send notifications with a phone to an other phone, with firebase ? how then :) ?
Finally, I use the the Cordova Contact plugin to list all the contacts in the phone. Is there an other way to show to the user his contacts and then send the notification to the right person ?
Thanks a lot for your help. I've already checked the stackoverflow forum and the plugin issues section in order to solve that problem but I haven't find the solution yet.
(Sorry for bad english, this is my first post...)
To answer to your question there are more levels
My problem here is that I don't know how to proceed to send
notifications to an other phone !
Push notification can be sent through firebase console.
Is it possible to know the receiver token ?
Yes if you Initialize firebase plugin, you will get the firebase token in your javascript function inside FCMPlugin.getToken()
Is it possible to send notifications with a phone to an other phone,
with firebase ?
You can send only from one receiver to another receiver directly using firebase push data. But I wont recommend you that. Because it is not safe to exchange data directly between receivers without a server
Note:
Before starting firebase notification I would like to give you a note. There are 2 types of payloads in firebase.
1.Notification payload : This has a few limitation in receiving notification that, the app will get notification only when app is in background. So notification will not be shown to receiver if he is using the app. It will be handled by your FCMPlugin
1.Data payload : If you need to show the notification even if the app is in foreground then go for it. But you need to trigger the notification manually in your java code in android way
I am using Firebase Cloud Messaging HTTP Protocol to send push notifications to my application using postman for testing.
I am using the following code to send the push.
{
"notification":{
"title":"Title",
"body":"this is a notification to a specific topic",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
},
"data":{
"action":"ping"
},
"to":"/topics/Topic_1",
"priority":"high"
}
and I am using this code to handle the notification on my app:
FCMPlugin.onNotification(function(data){
console.log(data);
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('notification tapped'+ JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert('application is open'+ JSON.stringify(data) );
}
});
it is all working except i cannot get the title and the body to use them in my application, all I am getting is the following:
Object {wasTapped: false, action: "ping"}
I cannot find a way to get the notification title and body.
I know I can copy them to the data section but that's not logical its a dirty workaround
so any idea how to get the notification data?
thank you.
Basing from the behavior shown, I'm presuming that the client platform is Android. If so, then this is working as expected.
When sending a combination of both notification and data in your message payload, the Android System tray will be the one to handle the values in notification. The dirty workaround you mentioned is the only workaround so far -- a workaround that I actually suggested on one of my answers as well.
We think that Android’s push notifications by default are silent notifications and must be the developer who programmatically raise the notification on the screen. Or the push notifications aren’t silent by default?
Today we work using this JSON structure which our app receives from the GCM server.
{
"data":
{
“Type” : “2”,
“_dId” : “3718829”,
“_mId” : “9924012”,
“_msg” : “HOLA JVE”,
“collapse_key” : “9924012”
},
"to" : "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Is this structure valid for silent notifications or there is a different one ?
Truly we are pretty lost on this topic. Following this question, anyone have a json structure of an Android push notification with message and title tags ?
Other question : When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
Or the push notifications aren’t silent by default?
There are two ways that a notification is received in Android, either it's Notification Tray, or you handle it yourself in onMessageReceived() depending on which payload you use (Notification or Data).
Basing from your inquiry (silent push notification), I'm guessing you'd prefer the latter, since you'll be able to handle it yourself. As per the GCM Payload docs:
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app,..
So what you are currently using right now, (a data payload) should be fine. However, if you have both notification and data in your payload, you will have to consider your app's status. Referring to this FCM docs, for data payload:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
..anyone have a json structure of an Android push notification with message and title tags ?
As also mentioned in the docs I linked above:
Data messages have only custom key/value pairs.
So I think it's safe for you to just use keys so long as it's not a reserved word, as mentioned in this docs:
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
I think what I mentioned above pretty much covers this part (see the table).
On Android, differently from iOS, the app is responsible for creating and showing the push notification. So yes, you can think of them as silent by default, although you don't exactly have this concept on Android.
What I normally do is, if the notification should not be shown, add a silent field. For example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "Will not show this message",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
"silent": "true"
}
}
And then you can use the silent field to show or not show the notification according to an if statement.
This question already has answers here:
Android app not receiving Firebase Notification when app is stopped from multi-task tray
(6 answers)
Closed 6 years ago.
I am trying to get familiar with Firebase Notifications. It works fine, but I am stuck with receiving messages from the notification console when the app is not turned on.
I know that documentation says that:
if your app in foreground or background you can receive message in onMessageReceived method, otherwise user will receive notification in tray... click on it will open main activity with data inside intent
But is there are any way to catch every message from the notification console even if the application is closed?
==== ANSWER ====
Find answer here
There is no way to send data message from the notification console.
But there are other ways to send notification to devices and they will be caught inside onMessageReceived!
You can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
onMessageReceived() method will not be called if the app is in background or killed only when the message is sent through Firebase Console.
When app is not running you will anyway receive notification from firebase console. But if you want to intercept data via onMessageReceived() then you will have to create a custom app server which will send only data payload to fcm endpoint. In short you will have to create an app server if you want to actually utilize FCM more efficiently in this case .
You can have a look at these two questions which discuss more regarding same and in second question I also discuss briefly over using node.js and java servlet to do the same:
Handle the data payload without user tapping on the notification?
How to push notification to client when Firebase has a new entry?
Do let me know if this provides you with some info.