Firebase has a default simple notification layout as Android's default one. How can I change it to a custom layout and display the notification when generated.
Asked
Active
Viewed 4,407 times
4

Alireza Noorali
- 3,129
- 2
- 33
- 80

PArth SOni
- 117
- 1
- 11
-
u means add images or icon with your notification – Dhruv Tyagi May 05 '17 at 07:32
-
nop bro creating custom notication or can say collapsing notification. – PArth SOni May 05 '17 at 07:33
3 Answers
1
In FirebaseMessaging service write the following :
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
try {
JSONObject jsonObject = new JSONObject(remoteMessage.getData());
Log.e("Tag",remoteMessage.getData().toString());
sendNotification(remoteMessage.getData().toString());
} catch (Exception e) {
}
}
private void sendNotification(String msg) {
Intent intent = new Intent(this, NewTransactionsHistActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, new Random().nextInt(100) , intent,
PendingIntent.FLAG_ONE_SHOT);
long when = System.currentTimeMillis();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
mNotifyBuilder.setVibrate(new long[] { 1000, 1000,1000,1000,1000,1000});
boolean lollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
if (lollipop) {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setColor(Color.TRANSPARENT)
.setLargeIcon(
BitmapFactory.decodeResource(
getResources(),
R.drawable.rlogo))
.setSmallIcon(R.drawable.ic_icon_lollipop)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
} else {
mNotifyBuilder = new NotificationCompat.Builder(this)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentTitle(getString(R.string.app_name)).setContentText(msg)
.setSmallIcon(R.drawable.rlogo)
.setWhen(when).setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(100) /* ID of notification */, mNotifyBuilder.build());
}

Alireza Noorali
- 3,129
- 2
- 33
- 80

Rajesh N
- 6,198
- 2
- 47
- 58
-
3**onMessageReceived** method triggers when the app is in foreground. It won't trigger if the app is running in the background. Check [This Answer](https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background) . – Niamatullah Bakhshi Mar 11 '18 at 04:34
-
This method is the only way, but only triggered all the time when using data payload via the API. – JoshuaTree Apr 02 '18 at 09:13
-
1
0
On your server side, remove the notification
attribute.
When you send a notification without the notification
attribute firebase will not handle the notifications.
You can then extend FirebaseMessagingService
to handle the notification.
Don't forget to register the service in the manifest.

Alireza Noorali
- 3,129
- 2
- 33
- 80

Dishonered
- 8,449
- 9
- 37
- 50
-
1i am getting notification but i want to change it with a custom layout how to handle it – PArth SOni May 05 '17 at 07:35
0
One thing to keep in mind is that, if you haven't added the data block on your payload of firebase... your onMessageReceived method will not be called
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
**FROM HERE TO COPY**
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RunNotification(title,body);
**METHOD**
private void RunNotification(String title, String messageBody) {
RemoteViews contentView;
Notification notification;
NotificationManager notificationManager;
int NotificationID = 1005;
NotificationCompat.Builder mBuilder;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(getApplicationContext(), "notify_001");
contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.img);
contentView.setTextViewText(R.id.title, title);
contentView.setTextViewText(R.id.text, messageBody);
mBuilder.setSmallIcon(R.drawable.ic_baseline_location_on_24);
mBuilder.setAutoCancel(false);
mBuilder.setContentTitle(title);
mBuilder.setContentText(messageBody);
mBuilder.setPriority(Notification.PRIORITY_HIGH);
mBuilder.setOnlyAlertOnce(true);
mBuilder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH;
mBuilder.setContent(contentView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "channel_id";
NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(channelId);
}
notification = mBuilder.build();
notificationManager.notify(NotificationID, notification);
}

Aaraf Rao
- 19
- 1
- 4