6

I am setting large icon for push notification by using following code.

 new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(notification.getTitle())
                    .setContentText(notification.getBody())
                    .setAutoCancel(true)
                    .setColor(getResources().getColor(R.color.green))
                    .setSound(defaultSoundUri)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentIntent(pendingIntent);

This works fine and shows largeIcon when app is in foreground, but when app is not in foreground, it does not display large icon.

I am testing the app in samsung s7(oreo)

Mocas
  • 1,403
  • 13
  • 20
dev90
  • 7,187
  • 15
  • 80
  • 153
  • Where are you calling the builder? Have you checked that `BitmapFactory.decodeResource(getResources(), R.drawable.logo)` is returning non-null in those cases where you don't see the large icon? – Cheticamp Mar 07 '19 at 17:29
  • @Cheticamp : I am calling it in `onMessageReceived` method, and i cannot debug this, this only happens when app is closed. – dev90 Mar 08 '19 at 13:19
  • I see. Does [this](https://wajahatkarim.com/2018/05/firebase-notifications-in-background--foreground-in-android/) address the problem? – Cheticamp Mar 08 '19 at 13:39

6 Answers6

7

The code snipped you posted works fine only when your app is in the foreground because it only gets called when the app is in the foreground. When the application is in the background, the Android system displays the notification for you (based on the 'notification' object in the JSON object that the server sends to the device). Your receiver is not being called at all. Currently, Firebase Cloud Messaging doesn't support setting a large icon as a payload in the request.

A way around this is to not include a 'notification' object in the POST payload of the message. When you only include 'data' object in the payload, your receiver will be asked to handle the notification even when your application is in the background. Then, you can build the notification the same way as if the app was in the foreground and set a large icon for the notification.

Take a look at this answer for a more detailed explanation about this issue.

deluxe1
  • 737
  • 4
  • 15
4

Try this,

you're recommended to set default values to customize the appearance of notifications. You can specify a custom default icon and a custom default color that are applied whenever equivalent values are not set in the notification payload.

Add these lines inside the application tag to set the custom default icon and custom color:

  <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_ic_notification" />


    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
1

Set your application icon transparent

android:icon="@drawable/icon_transper"

and add this lime in AndroidManifest

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/gardi" />
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

If you set your application icon transparent it can take as notification icon in background.

prefer this link to generate notification icon.

https://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit

I hope this can help you!

Thank You.

Hardik Talaviya
  • 1,396
  • 5
  • 18
0

It's because you've to set the large icon first and small icon second to that, also you shouldn't use vector images as large icon. Use either Jpeg or PNG images.

new NotificationCompat.Builder(this, channelId)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setAutoCancel(true)
                .setColor(getResources().getColor(R.color.green))
                .setSound(defaultSoundUri)
                .setPriority(Notification.PRIORITY_MAX)
                .setContentIntent(pendingIntent);
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
0

try once.. Where are you storing your icon in drawable folder .. all the folders like hdpi, xhdpi, xxhdpi.. pls try this once..

satyan_android
  • 364
  • 4
  • 15
0

You need to check version. If version >= Oreo than you need to add channel id. Please see the sample code below which is working for me:

  public void createNotification(Bitmap bitmap, String aMessage, Context context, int icon, String titles, String noti_id, String mid, String att_url, String mname) {
        NotificationManager notifManager = null;
        final int NOTIFY_ID = 0; // ID of notification
        String id = context.getString(R.string.default_notification_channel_id); // default_channel_id
//        String title = context.getString(R.string.default_notification_channel_id); // Default Channel
        String title = titles;
        Intent intent;
        PendingIntent pendingIntent;
        NotificationCompat.Builder builder;
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();

        bigPictureStyle.setBigContentTitle(title);
        String msg = aMessage;
//        bigPictureStyle.setSummaryText("Summary text appears on expanding the notification");
        bigPictureStyle.setSummaryText(msg);
        bigPictureStyle.bigPicture(bitmap);
        if (notifManager == null) {
            notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        String m_namse = "Marwadi University - " + mname;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel mChannel = notifManager.getNotificationChannel(id);
            if (mChannel == null) {
                mChannel = new NotificationChannel(id, title, importance);
                mChannel.enableVibration(true);
                mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
                notifManager.createNotificationChannel(mChannel);
            }
            builder = new NotificationCompat.Builder(context, id);
            intent = new Intent(context, FullScreenActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            List<String> test = new ArrayList<String>();
//            test.add();
            intent.putExtra("title", titles);
            intent.putExtra("body", aMessage);

            ConstantData.setNotificaitonId(context);
//            ConstantData.getNotificaitonId(context);
            bundle.putString("values", str);
            intent.putExtra("img_path", imageUri);
            intent.putExtra("n_id", noti_id);
            intent.putExtras(bundle);

            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            builder.setContentTitle(aMessage)                            // required
                    .setSmallIcon(R.drawable.mu_top_white)   // required

                    .setContentText(m_namse) // required
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setStyle(bigPictureStyle)
                    .setContentIntent(pendingIntent)
                    .setTicker(aMessage)
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        } else {
            builder = new NotificationCompat.Builder(context, id);
//            intent = new Intent(context, AllNotificationActivity.class);
            intent = new Intent(context, FullScreenActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("title", titles);
            intent.putExtra("body", aMessage);
//            intent.putExtra("img_path", imageUri);
            intent.putExtra("img_path", imageUri);
            intent.putExtra("n_id", noti_id);
            intent.putExtra("mid", mid);

            ConstantData.setNotificaitonId(context);


            pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            builder.setContentTitle(aMessage)                            // required
                    .setSmallIcon(R.drawable.mu_top_white)   // required
                    .setContentText(m_namse) // required
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setStyle(bigPictureStyle)
                    .setContentIntent(pendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
                    .setTicker(aMessage)
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setPriority(Notification.PRIORITY_HIGH);
        }
        Notification notification = builder.build();
        notifManager.notify(NOTIFY_ID, notification);
    }
And then Simple call this method:

createNotification(bitmap, message, mContext, R.drawable.main_logo, title, noti_id, mid, att_url, msg);
parik dhakan
  • 787
  • 4
  • 19