我正在收到通知,但没有通知图标...我不明白为什么图标未显示在通知
中notification:
{
title: `Hey ${userName}`,
body: `You Have One New Notification`,
icon: "default",
color: "#0000FF",
sound: "default",
priority: "high",
}
firebasemessagingservice
String notificationTitle=remoteMessage.getNotification().getTitle();
String notificationbody=remoteMessage.getNotification().getBody();
String click_action=remoteMessage.getNotification().getClickAction();
// String from_user_id=remoteMessage.getData().get("User_data");
NotificationCompat.Builder mbuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.uemround)
.setContentText(notificationbody)
.setContentTitle(notificationTitle)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent resultIntent=new Intent(click_action);
//resultIntent.putExtra("user_id",from_user_id);
PendingIntent pendingIntent= (PendingIntent) PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mbuilder.setContentIntent(pendingIntent);
int mnotificationId=(int)System.currentTimeMillis();
NotificationManager mnotifymgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mnotifymgr.notify(mnotificationId,mbuilder.build());
You need to add notification channel for the version oreo and above. Copy the below code and run it. It will work
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "Channel_id";
CharSequence channelName = "Your app name";
int channelImportance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) // channel id is mandatory
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
// and icon should be transparent for lollipop and above version
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.icon_transperent);
} else {
notificationBuilder.setSmallIcon(R.drawable.icon);
}
notificationManager.notify(new Random().nextInt(9999 - 1000) + 1000/* ID of notification */, notificationBuilder.build());
}
} else {
if (notificationManager != null) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "")//no need to add the channel id
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
// and icon should be transparent for lollipop and above version
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setSmallIcon(R.drawable.icon_transperent);
} else {
notificationBuilder.setSmallIcon(R.drawable.icon);
}
notificationManager.notify(new Random().nextInt(9999 - 1000) + 1000/* ID of notification */, notificationBuilder.build());
}
}
onmessagerecred-->
检查 -> if(remotemessage.getData((。size((> 0(然后您可以根据要求设置数据,然后
notificationBuilder = new NotificationCompat.Builder(this, notification_channel_id)
.setContentTitle(remoteMessage.getData().get("Title"))
.setContentText(remoteMessage.getData().get("Body"))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
//.setContentinfo("重要"( .setlargeicon(bitmapfactory.decoderesource(getResources((,r.mipmap.ic_launcher(( .setColor(getResources((。getColor(r.color.coloraccent(( .setlights(color.Red,1000,300( .setDefaults(notification.default_vibrate( .setnumber( nummessages( .setsmallicon(r.mipmap.ic_launcher(;
对不起,英语不好; - (