简体   繁体   中英

FirebaseMessagingService push notification creation - how to set icon?

I want to set icon for my push notification which is created by FirebaseMessagingService . Problem is that these notifications are created by system internally (probably in function onStartCommand() which is final and I don't have access to it. I don't know from which source is icon for this notification set. Normally if you create notification you can set icon by NotificationManager , but this process is done internally by Firebase. Reason why I want to change icon is because my icon for app has transparent background (its vector drawable (.svg). But for some reason it is shown as square.

Is there any way how to modify notification icon inside FirebaseMessagingService ?

Use this Function for display notification

  public void showNotificationMessage(final String title, final String message,Intent intent) {
    Random random = new Random();
    NotificationManager notifManager = null;
    if (notifManager == null) {
        notifManager = (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    NotificationCompat.Builder mBuilder=null;
    if (TextUtils.isEmpty(message))
        return;

    final int icon = R.mipmap.ic_launcher;
    String notificationId = String.format("%04d", random.nextInt(10000));

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    final PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    mContext,
                    Integer.parseInt(notificationId),
                    intent,
                    PendingIntent.FLAG_CANCEL_CURRENT
            );
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notifManager.getNotificationChannel("Test");
        if (mChannel == null) {
            mChannel = new NotificationChannel("Test", title, importance);
            mChannel.enableVibration(true);
            notifManager.createNotificationChannel(mChannel);
        }
        mBuilder = new NotificationCompat.Builder(mContext, "Test");

    }else{
        mBuilder = new NotificationCompat.Builder(mContext, "Test");
    }

    Notification notification;
    notification = mBuilder.setTicker(title)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            //.setStyle(inboxStyle)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentText(message)
            .build();
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Integer.parseInt(notificationId), notification);


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM