Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show firebase notification with custom layout?

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.

like image 621
PArth SOni Avatar asked Dec 05 '25 09:12

PArth SOni


1 Answers

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());
}
like image 68
Rajesh Nasit Avatar answered Dec 07 '25 22:12

Rajesh Nasit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!