通知图标未显示

时间:2019-08-02 05:56:06

标签: android firebase firebase-cloud-messaging

我收到通知,但没有通知图标... 我不明白为什么图标未显示在通知中

notification:
            {

                title: `Hey ${userName}`,
                body: `You Have One New Notification`,
                icon: "default",
                color: "#0000FF",
                sound: "default",
                priority: "high",   


            }

Firebasemessaging服务

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

onMessageRecieved->

检查->如果(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);

对不起,英语不好;-)