Firebase Notification contains both Title and Message - android

I am using this tutorial to send push notification using Firebase
How can I show Title and Message both in Notification Barenter code here (Still I am getting message as *Title* content in Notification Bar)…
As you can see in this screenshot (due to this, I am unable to deliver complete *message along with Title* to users, because in Notification Bar title has limit maximum 1 line)
I am using Advanced Rest Client (Messaging API) to deliver Notification(s) like this:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIza************adrTY
{ "data": {
"image": "https://ibin.co/2t1lLdpfS06F.png",
"message": "Firebase Push Message Using API"
"AnotherActivity": "True"
},
"to" : "f25gYF3***********************HLI"
}
Code
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
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)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.firebase_icon)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

Use
setContentTitle("title") to set the title and of notification
setContentText("text") to set the text.
A example from Android Developers:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
In your case you will have to change your code to:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)
.setSmallIcon(R.drawable.firebase_icon)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setContentTitle(messageTitle) // here
.setContentText(messageBody); // and here
I recommend that you take some time studying NotificationChannel as well, if you wanna to support the new Android O notifications
NotificationChannel example

Related

no sound of firebase notifications on marshmallows, but works on oreo

I don't know why when I send a notification to marshmallow, there is no sound, but when I send it to oreo, there is a sound notification.
My firebase code is below
Intent intent = new Intent(this, MainActivity.class);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("idata", idata);
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)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
} else {
Context c = getApplicationContext();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
final String CHANNEL_ID = "default";
// The user-visible name of the channel.
final String CHANNEL_NAME = "Default";
NotificationChannel defaultChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(defaultChannel);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("idata", idata);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setWhen(System.currentTimeMillis())
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent);
notificationManager.notify("myapp", 0, notificationBuilder.build());
}
I searched all over the internet, but there were no results for this, I really needed this, please help me, any help would really be appreciated. thanks
I had the same problem, but the channel id is only used by oreo & higer android versions. If no sound playing in less than oreo, then make sure you have added "sound": "default"//or any custom filename you have in ur res/ folder.
I accidently missed it in my firebase payload.
Addding sound:"default" solved my issue.
Example:
const notificationContent = {
notification: {
icon: "default",
priority:"high",
sound: "default",
android_channel_id: "myChannelID",
title: "new update",
},
data: {//Used on onMessage() function
priority:"high",
android_channel_id: "myChannelID",
click_action: "FLUTTER_NOTIFICATION_CLICK",
title: "new update",
},
};

Display FCM large notification icon

I am new to android. I have created FCM large notification. Now what I want to do, how can I make notification like image Large Imag I trying to make it large icon but I filed. Thank you for help.
FcmMessagingService : java
private void ManualNotification(String title, String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("message", messageBody);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher);
Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
bigpicture.bigPicture(bmp);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notifcation)
.setContentTitle(title)
//.setContentText(messageBody)
.setLargeIcon(bmp)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentText(messageBody).setLights(Color.YELLOW, 300, 300)
.setVibrate(new long[]{100, 250})
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
you can use this link for notification icon size:
notification icon size in android
or if you want to create your custom notification view then you can use RemoteView for that. See sample:
android custom notification tutorial
Try with Notification class:
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(Context context, String channelId);
Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(bigPictureStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();

how to make heads-up notifications in android studio?

As I've searched how to reveal heads-up, I found a solution that is to set priority high but it still doesn't show heads-ups. I've got a notfication massenger service. Its function is as below. Is there anything I missed in settings?
Heading
mycode:
private void sendNotification(String body, String title) {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
remoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification);
remoteViews.setTextViewText(R.id.title, "Firebase");
remoteViews.setTextViewText(R.id.body, body);
remoteViews.setImageViewResource(R.id.icon, R.mipmap.ic_launcher);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivities(this, 0, new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.ic_launcher)
.setCustomContentView(remoteViews)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setCustomHeadsUpContentView(remoteViews)
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setTicker(body)
.setSound(notificationSound)
.setFullScreenIntent(pendingIntent, true)
.setContentIntent(pendingIntent);
notificationManager.notify(0, notifBuilder.build());
}
Try replacing .setContentIntent(pendingIntent) with .setFullScreenIntent(pendingIntent, true) in your Notification Builder.

With multiple notifications second notification showing in collapse mode Android

I'm showing multiple notifications in my android application.
When there is single notification it is showing correctly.
But when I generate second notification, second notification showing in collapse by default.
If I manually expand second notification it is showing as:
I want to show a notifications in expanded mode by default.
Here is my code:
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(messageBody);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.corco)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(bigTextStyle)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());
Am i missing something? or doing something wrong?
Thanks in advance.
I resolved it. I was not setting any ContentTitle and ContentText for collapse notification.
Here is my updated code:
int num = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, num /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(messageBody);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.corco)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(bigTextStyle)
.setContentTitle(title)
.setContentText(messageBody)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num /* ID of notification */, notificationBuilder.build());

FCM : I want to group all my notifications into one. How can I achieve this?

I have used FCM to receive notifications. When I receive multiple notifications it fills up the notification status bar. How do I group them into one?
My code:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("ABC")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setGroup(GROUP_KEY_NOTIFICATIONS)
.setContentIntent(pendingIntent);
Using setGroup() it is not working.
Edit:
.setGroupSummary(true)
is working for OS lollipop but not in marshmallow. Can you please help
To group notifications, you have to insert notifications in database and using NotificationCompat.InboxStyle you can group notifications.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("ABC")
.setPriority(Notification.PRIORITY_HIGH)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setGroup(GROUP_KEY_NOTIFICATIONS)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (int i = 0; i < data.size(); i++)
{
inboxStyle.addLine(data.get(i));
}
inboxStyle.addLine("");
notificationBuilder.setStyle(inboxStyle);
Where data is list fetched from database containing all notifications.

Categories

Resources