我正在我的应用程序中设置Firebase云消息传递,一切正常,但接收到通知时通知图标会显示为白色正方形。
我也尝试将builder.setSmallIcon更改为其他图标,我在AndroidManifest.xml中添加了用于Firebase消息传递default_notification_icon的资源图标
FcmMessagingService.java
private static int VIBRATION_TIME = 500; // in millisecond
private SharedPref sharedPref;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sharedPref = new SharedPref(this);
if (sharedPref.getNotification()) {
// play vibration
if (sharedPref.getVibration()) {
((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(VIBRATION_TIME);
}
RingtoneManager.getRingtone(this, Uri.parse(sharedPref.getRingtone())).play();
if (remoteMessage.getData().size() > 0) {
Map<String, String> data = remoteMessage.getData();
FcmNotif fcmNotif = new FcmNotif();
fcmNotif.setTitle(data.get("title"));
fcmNotif.setContent(data.get("content"));
fcmNotif.setPost_id(Integer.parseInt(data.get("post_id")));
displayNotificationIntent(fcmNotif);
}
}
}
private void displayNotificationIntent(FcmNotif fcmNotif) {
Intent intent = new Intent(this, ActivitySplash.class);
if (fcmNotif.getPost_id() != -1) {
intent = new Intent(this, ActivityPostDetails.class);
Post post = new Post();
post.title = fcmNotif.getTitle();
post.id = fcmNotif.getPost_id();
boolean from_notif = !ActivityMain.active;
intent.putExtra(ActivityPostDetails.EXTRA_OBJC, post);
intent.putExtra(ActivityPostDetails.EXTRA_NOTIF, from_notif);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle(fcmNotif.getTitle());
builder.setSmallIcon(R.drawable.ic_notification2);
//builder.setSmallIcon(R.drawable.splash_icon);
//builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
R.mipmap.ic_launcher));
builder.setStyle(new Notification.BigTextStyle().bigText(fcmNotif.getContent()));
builder.setContentText(fcmNotif.getContent());
builder.setDefaults(Notification.DEFAULT_LIGHTS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setPriority(Notification.PRIORITY_HIGH);
}
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int unique_id = (int) System.currentTimeMillis();
notificationManager.notify(unique_id, builder.build());
}
答案 0 :(得分:1)
您需要致电builder.setSmallIcon(getNotificationIcon())
并确保图标背景应该是透明的,即R.mipmap.transparent
,如果您的Build.VERSION.SDK_INT
是LOLLIPOP
或Build.VERSION_CODES.LOLLIPOP
以上,否则可以使用您的应用程序图标
private int getNotificationIcon()
{
boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
return useWhiteIcon ? R.mipmap.transparent: R.mipmap.appicon;
}
答案 1 :(得分:0)
Firebase 9.8.0 it is possible to change this icon, by adding info about this in Manifest:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />