Firebase notifications in the foreground - android

I am having problem with FireBase push notifications. When my app is in the background, notifications are coming, but when my app is in the foreground i don't receive notifications, but in console that notification is displayed, that means that notification is here but it doesn't show in notification bar . Could you help me?
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("OnMessage", "Received");
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From " + remoteMessage.getFrom());
Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
String aa=remoteMessage.toString();
Intent intent=new Intent(MyFirebaseMessagingService.this, MainActivity.class);
intent.putExtra("msg", aa);
sendNotification(remoteMessage);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentText(remoteMessage.getNotification().getBody());
// remoteMessage.getNotification().getBody();
}
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

EDIT:
I just noticed that onMessageReceived() of FirebaseMessagingService is not invoked(as of now) when you send notifications from console and your app is in foreground. One solution is you can use Firebase APIs to send push notifications and it will invoke onMessageReceived() regardless of app being in foreground or background.
If you use "notification" body to send push notification i.e.
notification: {
title: "notification title",
icon: "ic_launcher",
color: "#4E2AA5",
body: "notification body"
}
To retrieve this in onMessageReceived(), use something like this:
String color = remoteMessage.getNotification().getColor();
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String icon = remoteMessage.getNotification().getIcon();
If your app is in background, Firebase will display a notification corresponding to the data you set in push and will also invoke onMessageReceived(). Thus you will see 2 notifications if you have also written code to show custom notification in onMessageReceived().
To work around this, you can use some "data" key like this:
data: {
title: "notification title",
body: "notification body"
}
and to retrieve it in onMessageReceived(), use something like this:
Map<String, String> map = remoteMessage.getData();
for (Map.Entry<String, String> entry : map.entrySet()) {
Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}
You can then build your own custom notification and display it from onMessageReceived() only that notification will show up.
P.S. Please vote up if it helps. I am new to stackoverflow.

Like you said you are receiving the message since you are seeing the log messages in the console so you have handled the FCM part properly and the issue is likely with your creation of the notification.
If you look at your notification creation code you are missing a couple of the required elements to create a notification on Android. From the docs:
A Notification object must contain the following:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
So I think if you add those items to your notification creation you should see the notification in the notification area.

Intent intent = new Intent(this, "your class");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
}
builder = builder
.setSmallIcon(R.mipmap.ic_app_icon)
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.picker_button_background_innactive))
.setContentTitle((messageBody))
.setTicker(title)
.setContentText(message_body)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
notificationManager.notify(1, builder.build());

Related

How to display a FCM Firebase notification sent with a server

Firebase offers two ways to seding a push notifications, using it's firebase console and using a server like in GCM was did before. Now we are translating our servers from GCM to Firebase, and we need to receive and show FCM notifications in the app.
I have the implementation of the official firebase website done, with the sample codes done, but I'm stuck on this part. When our server sends the push notification to Firebase, and firebase send's to my app, I'm receiving the notification in onMessageReceived method with a RemoteMessage parameter. I know that the message is inside RemoteMessage parameter, but I can't find any guide or sample code in the Firebase official documentation about how to deal with this to show the notification text on the android device notification bar.
As you can see on the official guides, it is not explained how to extract the message without problems from the RemoteMessage parameter. I know that you can search inside and find the String but I want to find a official guide about how to correctly and safely deal with this.
In the official documentation you have only this:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// Handle message within 10 seconds
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// 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.
}
And this (which is not being called on the guide):
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_main)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
I have done something like this I am using addNotifcation() in place of sendNotification()
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getData().get("body"));
addNotification(remoteMessage.getData().get("body"), remoteMessage.getData().get("Nick"));
}
private void addNotification(String message, String title) {
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher1)
.setContentTitle(title)
.setContentText(message);
Intent notificationIntent = new Intent(this, app.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
You will recieve a json onMessageRecieve from server then you need to parse that json and get values out of it.Once you have parsed value you can send it with sendNotification() Method and show what you require
Check below method, its working fine, I am getting json data from server in notification.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
AppLog.Log(TAG, "From: " + remoteMessage.getFrom());
JSONObject json = null;
if (remoteMessage.getData().size() > 0) {
Log.v(TAG, "Message data " + remoteMessage.getData());
json = new JSONObject(remoteMessage.getData());
AppLog.Log(TAG, "Json object " + json);
}
if (json != null) {
try {
int icon = R.mipmap.ic_launcher;
int mNotificationId = 001;
// MainDrawerActivity is the class which open on the click of notification in status bar
Intent intent = new Intent(this, MainDrawerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//FLAG_UPDATE_CURRENT is important
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this);
Notification notification = mBuilder.setSmallIcon(icon).setTicker("your title").setWhen(0)
.setAutoCancel(true)
.setContentTitle("your title")
.setStyle(new NotificationCompat.BigTextStyle().bigText("message"))
.setContentIntent(pendingIntent)
.setContentText("message").build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

android FCM push, no sound, no vibration, no head up

I'm using FCM to make a push message.
I implemented all the push mechanism by copying and pasting the codes on FCM website.
After doing this, I have been testing several times to check if it works well.
I checked with Log.d and I found that both onMessageReceived and sendPushNotification methods are triggered when a push message is received.
However, when the message is received, my phone doesn't make any sound, no vibration, no head up.
The message is just displayed on the screen when I turn the screen on.
Can anyone let me know how to fix these problems?
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// sendPushNotification(remoteMessage.getData().get("message"));
if(remoteMessage.getData().size() > 0){
sendPushNotification(remoteMessage.getNotification().getBody());
}
}
private void sendPushNotification(String message) {
System.out.println("received message : " + message);
Intent intent = new Intent(this, Activity_Login_Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 //Request code
, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.backicon).setLargeIcon(BitmapFactory.decodeResource( getResources(),R.mipmap.ic_launcher) )
.setContentTitle("Push Title ")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wakelock.acquire(5000);
notificationManager.notify(0 // ID of notification
, notificationBuilder.build());
}
}
what is your notification payload you are sending, if you app is in background, as per FCM document it should have a sound key, your code will works only when your app is in Foreground.

Firebase onMessageReceived(RemoteMessage remoteMessage), is not called when the app is in background [duplicate]

This question already has answers here:
Firebase onMessageReceived not called when app in background
(30 answers)
Closed 6 years ago.
In Firebase onMessageReceived(RemoteMessage remoteMessage), this method will be called when your app is in Foreground. So, on clicking the notification you can open a different Activity, say NotiicationActivity.
But what if your app is in background, then this method will not be called, and on clicking the notification only a Launcher Activity will be opened.
So how to open the NotificationActivity on clicking the notification even if our app is in background.
My Code is this :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification().getBody());
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("key", messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Bitmap icon2 = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM Sample")
.setContentText(messageBody)
.setAutoCancel(true)
.setLargeIcon(icon2)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt() /* ID of notification */, notificationBuilder.build());
}
}
onMessageReceived is only triggered when the application is in foreground.
If the app is backgrounded, you may still be able to receive the notification but onMessageReceived will not be triggered.
So my suggestion,
on the receiving activity, you can still get the data from the notification by using:
getIntent().getExtras();
This should work accordingly. Hope this helps :)
try and follow below link :
Firebase Cloud Messaging for Android
In your sendNotification() method :
private void showNotification(String message){
Intent intent=new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("key", messageBody);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM Sample")
.setContentText(message)
.setLargeIcon(icon2)
.setContentIntent(pendingIntent);
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}

Notifications in Android with firebase. Not stacked

AT this moment I achive to get Push Notifications (Firebase) to my client.
I followed the instructions onf firebase.
Now, as the app is a message client, I'm receiving a lot of pushes (meanwhile the app is ni background and foreground).
What I tried (and found here: https://developer.android.com/training/wearables/notifications/stacks.html)
So my code, right now is:
public class FCMessagingService extends FirebaseMessagingService {
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Logger.d("Notification Body:" + remoteMessage.getNotification().getBody()
+ "\n DATA:" + remoteMessage.getData().toString());
String jid = remoteMessage.getData().get("jid");
sendNotification(remoteMessage.getNotification().getBody(), jid);
}
private void sendNotification(String messageBody, String jid) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_chv)
.setContentTitle(getString(R.string.notification_title))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setGroup(jid);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
AS you can see, I receive in data bundle, the parameter "jid", that identifies the user who send the message. So I use it to try to stak all theis messages, and at the end , have 1 (stacked)notification(s) for every jid.
But, the notifications don't stack when the app is in Background, even more..it launches the notications that where stacked before.
IS there any work around to solve it?
Log of what I receive from notification push:
23304-17215 D/ChatListRealmAdapter: ║ Notification Body:Ana ha enviado un mensaje
23304-17215 D/ChatListRealmAdapter: ║ DATA:{'jid':'ana1234#domain.com'}

Notification sending from firebase console but its showing failed status but sending to all device is mark as completed

I have implemented push by firebase.
I'm sending notifications but I am getting status of "Failed". When I send notification to all devices it is marking as completed but I am still not getting messages in device.
Even when I send messages to single devices it will also show failed and will not receive notification on device.
The code is
private static final String TAG = "StartingAndroid";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//It is optional
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
private void sendNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
Your sender ID mismatched or you have entered incorrect project ID in your Application.
Looking at logs, I think you are sending to specific devices via registration id but we see an error with mismatched sender id.Please check that these registration ids are valid and apply to the correct app.

Categories

Resources