Android 我应该如何更改应用程序的推送通知图标

Android 我应该如何更改应用程序的推送通知图标,android,push-notification,firebase-cloud-messaging,Android,Push Notification,Firebase Cloud Messaging,我想更改推送通知图标 代码片段: public class Mymessagingservice extends FirebaseMessagingService { public void onMessageReceived(RemoteMessage remoteMessage){ super.onMessageReceived(remoteMessage); getimage(remoteMessage.getNotification().getTitle(), rem

我想更改推送通知图标

代码片段:

public class Mymessagingservice extends FirebaseMessagingService {

public  void onMessageReceived(RemoteMessage remoteMessage){
    super.onMessageReceived(remoteMessage);
    getimage(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());

}
public void getimage(String title,String message){

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"mynotification")
            .setContentTitle(title)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.otplogo
            ))
            .setSmallIcon(R.drawable.otplogo)
            .setAutoCancel(true)
            .setContentText(message);


    NotificationManagerCompat manager =NotificationManagerCompat.from(this);
    manager.notify(999,builder.build());

}
}

不幸的是,这是SDK 9.0.0-9.6.1中Firebase通知的一个限制。当应用程序位于后台时,启动程序图标将从清单(带有必要的Android着色)中用于从控制台发送的消息

但是,使用SDK 9.8.0,您可以覆盖默认值!在AndroidManifest.xml中,可以设置以下字段以自定义图标和颜色:

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

请注意,如果应用程序位于前台(或发送数据消息),您可以完全使用自己的逻辑来定制显示。如果从HTTP/XMPP API发送消息,也可以始终自定义图标

您必须将此标记放在清单的应用程序标记中

官方文件-


多亏了这个家伙-

兄弟,事情太简单了,但你必须小心行事

首先,为通知制作一个黑白图标。 现在在
通知
中将其设置为
小图标
,如下所示

mBuilder.setSmallIcon(R.mipmap.notification_icon);
现在您可以设置颜色,但如果我是对的,那么您只能设置预定义的颜色,而不是自定义颜色。对于自定义颜色,您可以尝试使用您的逻辑,我将为您提供预定义颜色的代码

 mBuilder.setColor(Color.GREEN);
它将使您的
通知图标变为绿色。
快乐编码

更新
自定义通知的代码

PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
            .getPackageName() + "/" + R.raw.sniper_gun);


    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.notification_icon);
    mBuilder.setSound(soundUri);
    if (image!=null) {
        mBuilder.setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(false)
                .setLargeIcon(image)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image).setSummaryText(message).bigLargeIcon(null))
                .setColor(Color.GREEN)
                .setContentIntent(resultPendingIntent);
    }
//        else {
//            mBuilder.setContentTitle(title)
//                    .setContentText(message)
//                    .setAutoCancel(false)
//                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
//                    .setContentIntent(resultPendingIntent);
//        }

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        // set custom soundUri
        if(soundUri != null){
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            notificationChannel.setSound(soundUri,audioAttributes);
        }

        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

问题是什么?代码有效吗?代码有效…我想设置我的图标,以便在应用程序位于后台时发出通知在前台我的图标可见…但在后台黑圈即将到来我在manifestsir中也使用了此功能我使用的是apk级别29…这里将使用频道有什么问题兄弟可以在您的代码中使用它作为
。setmallicon(R.mipmap.notification_icon);
.setColor(Color.GREEN);
@Egaragee我也在使用API级别29。当我的应用程序在后台时,我想要这个图标……它从清单中获取图标新的频道概念是这样的吗