FCM, how to allow messages from API's and avoid from Console - android

FCM message can be pushed from console and using api
from console (https://console.firebase.google.com)
using api (https://fcm.googleapis.com/fcm/send)
In both case when app is in foreground, message is received in service that extends FirebaseMessagingService, in onMessageReceived we can filter requests by custom param in bundle, but when app is in background and message is sent from console, receiver is not called, and the push message is somehow added.
Is possible to handle this request?

When app is in background and you suppose to fire notification via console at that time notification will come in notification tray rather than going to your service.
That will mentioned in guidelines.
https://firebase.google.com/docs/cloud-messaging/android/receive
So if you want to still handle notifications then, you have to set proper payload for same. So that whenever user click on notification then it will react accordingly.
For disabling notification try following,
FirebaseInstanceId.getInstance().deleteInstanceId();

Firebase Cloud Messaging support two types of messages:
Notification messages, sometimes thought of as "display messages." [When the app is not active], these are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
The Firebase console always sends notification messages, which are (when the app is not active) handled by the system. There is no way to intercept these messages in that case.
If you want to always receive the message in your application code, you should use a data message, which can only be sent using the API.

Yes, it is possible to handle messages when app is in foreground/background. In both cases you can handle it by implementing onMessageReceived. The only difference is, You have to put data in your JSON Message. Here you can find more.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myKey = data.get("myKey");
}

Related

Firebase display automatic notification even when all overrided

I use Firebase Cloud Messaging in my app, and I'm able to receive remote messages, with the data and everything I need from it. But even when I do nothing with the remote message, a notification appears automatically, with the content of the message.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// This line is executed, with the full message
}
}
With this code, the Android notification is still triggered, and I don't want it. I did not setup a default channel, and even on Android 8 (where I need to create a channel to send notification), there is one by default.
How can I remove this notification?
I want to display a notification only on my own terms, not on Firebase's ones
Don't use Notification message use data message instead.
in the Notification message, firebase will always show a notification when your app is in the background and there is no way to prevent it.
so the only way is to use data message and handle the notification by yourself.
from: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Notification message: FCM automatically displays the message to
end-user devices on behalf of the client app. Notification messages
have a predefined set of user-visible keys and an optional data
payload of custom key-value pairs.
Data message: Client app is responsible for processing data messages.
Data messages have only custom key-value pairs.
in all of my apps, I only use Notification message for very simple promotions because of lack of control and customization.

How to make a GCM / FCM notification-type message non-collapsible

Google Cloud Messaging (GCM) support two types of push messages: "notification" messages and "data" messages. According to the documentation, notification messages are collapsible by default, while data messages are non-collapsible by default.
In order to make a data message collapsible, you need to specifiy a collapseKey. My question is: How can you make a notification message non-collapsible?
Note: The question applies to Firebase Cloud Messaging (FCM) as well.
The message concepts and options documentation states:
messages are non-collapsible by default except for notification messages, which are always collapsible
But then later on the same page, it goes on to say:
except for notification messages, all messages are non-collapsible by default
Which is somewhat ambiguous. However, in the payload section, it states:
[notification messages] may have optional data payload. Always collapsible
Therefore, it doesn't seem possible to make notification messages non-collapsible.
I'd suggest that this is by design, because when creating notifications in Android, they are automatically replaced when another notification with the same ID is posted (similarly to how collapsing messages works). If I remember correctly, FCM/GCM uses the same ID for all notification messages.
Possible solution
If you do want a non-collapsible notification message, I'd suggest sending a data-only payload (with no notification or collapseKey), and then overriding the onMessageReceived() from the FirebaseMessagingService to create your own notification.
There is an example of this available on the Android quickstart sample:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// ...
}
// ...
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
The last comment there points you to the example sendNotification() method.
For your scenario, you'll need to pass a unique ID to the notificationManager.notify() call so that Android creates a new notification and does not replace any existing notifications - therefore, making the message non-collapsible.
First off, just going to put the FCM docs reference here:
A non-collapsible message denotes that each individual message is delivered to the device. A non-collapsible message delivers some useful content, as opposed to a collapsible message like a content-free "ping" to the mobile app to contact the server to fetch data.
As per your question -- "How can you make a notification message non-collapsible?" -- have you specifying a different collapse_key for each notification message you send?
A collapsible message works as if each of the message has the same collapse_key. But if you specify a different one for each message, then it won't be able to replace the previous one. This would make a notification message behave like a non-collapsible message.
However, since you are using a collapse_key, it is technically still treated as a collapsible message. Which means it is still a subject to the limit of four collapse keys at a time:
FCM allows a maximum of four different collapse keys per Android device to be used by the app server at any given time. In other words, the FCM server can simultaneously store four different collapsible messages per device, each with a different collapse key. If you exceed this number, FCM only keeps four collapse keys, with no guarantees about which ones are kept.
With all that said, any reason why you're not just using a data message payload? We use it on our app and I find it more flexible than a notification message.

How to send message from Firebase to device when app is killed? [duplicate]

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.

what does gcm.notification.e=1 stand for, into push notification payload on Android?

I'm writing an Android application which receives downstream messages from a server using Google Cloud Messaging. When receiving a message with onMessageReceived I print the bundle and read this:
RECEIVED PUSH NOTIFICATION: Bundle[{gcm.notification.e=1, gcm.notification.badge=1, gcm.notification.sound=default, ..etc...
I don't understand the key gcm.notification.e in the notification payload. On Google documentation I didn't find anything like that.
Moreover, when sending the message, I usually specify both a notification payload and a data payload. Now I tried to remove the notification payload but I still receive gcm.notification.e=1
Someone can help me understand this key and find a way to remove it?
Thanks
EDIT: I found out that sending a message without the notification payload and with content_available=false (I set this to true when working with iOS) then the gcm.notification.e=1 is not received. I receive only the data payload :). However, I still want to know the meaning of this e
Maybe this e field stands for enable. If you set it to zero in your server you won't receive the notification payload on Android. For more details se my answer here.

Android PushNotification From Json URL

I have a Json URL, which contains data about Latest Job Postings, I am successfully parsing the Json URL and able to display the top job postings in my ListView.
But my requirement is to create a push notification, so that whenever a new job is posted, the user should be able to get a notification on device.
I have followed this: http://www.vogella.com/articles/AndroidNotifications/article.html
But I don't know how to get notifications in my case.
Could anyone help me?
Issue:
Give push notification to user's device about the updated data even when application is in background mode.
Solution:
Upon successful insertion of new data in your database (which is going to give updated set of data to your JSON request) , just call the file which send GCM push notification to all your users.
Reference:
GCM docs
GCM push-notification using php server
In context of implementation presented in demo app of 2nd link,
upon successful insertion,you can call send_message.php file,but make sure that $regId and $message should be retrieved from your database
You have created ActionBar Notifications for your app, but now you need to create the ability to receive notifications from a web client, instead of going to find them yourself from the URL.
To create a push notification you would need to have a constant thread (BroadcastReceiver) on the device that is waiting for the notification from the sever.
Google 'Cloud to Device Messaging' is the simplest way to do this.
This is a good link with lots of info on how to do this :
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/
If you require these notifications to be displayed on the device even when the application is not running (which seems to be the case from what you describe), you can use Google Cloud Messaging.
You would need a server that would poll the Json URL for updates, and send a GCM message to all the devices where your app is installed once such an update is detected.
Your app would have to register to Google Cloud Messaging and send the Registration ID received from Google to your server.
When your app receive a GCM message, you would create a notification and when the notification is tapped, you would start the activity that loads the data from the JSON URL.

Categories

Resources