简体   繁体   English

FirebaseMessagingService 推送通知创建 - 如何设置图标?

[英]FirebaseMessagingService push notification creation - how to set icon?

I want to set icon for my push notification which is created by FirebaseMessagingService .我想为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.问题是这些通知是由系统内部创建的(可能在 function onStartCommand()中,这是最终的,我无权访问它。我不知道此通知集的图标来自哪个来源。通常,如果您创建通知您可以通过NotificationManager设置图标,但此过程由 Firebase 在内部完成。我想更改图标的原因是因为我的应用程序图标具有透明背景(其矢量可绘制(.svg)。但由于某种原因,它显示为正方形.

Is there any way how to modify notification icon inside FirebaseMessagingService ?有什么方法可以修改FirebaseMessagingService中的通知图标吗?

Use this Function for display notification使用此 Function 进行显示通知

  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);


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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