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

时间:2019-09-24 13:04:27

标签: android firebase firebase-cloud-messaging android-service android-notifications

我想为我的FirebaseMessagingService创建的推送通知设置图标。问题是这些通知是由系统内部创建的(可能是在onStartCommand()函数中,它是最终的,我无权访问。我不知道该通知集的图标来自哪个来源。通常,如果您创建通知,您可以通过NotificationManager设置图标,但是此过程由Firebase在内部完成。之所以要更改图标,是因为我的应用图标具有透明背景(其矢量可绘制(.svg)。)为何显示为正方形。

有什么方法可以修改FirebaseMessagingService内的通知图标?

1 个答案:

答案 0 :(得分:0)

使用此功能显示通知

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


}