在棉花糖中=显示白色图标。在马斯梅洛。 在Kitkat =显示应用程序图标。 在奥利奥(Oreo)中=使用应用程序图标显示通知,何时是背景,但在前景中则到达但不显示。
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.e(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage == null)
return;
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
handleNotification(remoteMessage.getNotification().getBody());
}
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
//Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
String title = remoteMessage.getData().get("title");
String message = remoteMessage.getData().get("body");
String click_action = remoteMessage.getData().get("click_action");
System.out.println("clickAction==@@@" + click_action);
handleDataMessage(title, message, click_action);
}
}
private void handleNotification(String message) {
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void handleDataMessage(String noti_title,String noti_message,String noti_click_action) {
try {
System.out.println("noti_title="+ noti_title);
System.out.println("noti_message="+ noti_message);
System.out.println("noti_click_action="+ noti_click_action);
Intent intent=new Intent(noti_click_action);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this);
notificationBuilder.setContentTitle(noti_title);
notificationBuilder.setContentText(noti_message);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
/**
* Showing notification with text only
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotificationMessage(Context context, String title, String message, String timeStamp, Intent intent) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent);
}
/**
* Showing notification with text and image
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void showNotificationMessageWithBigImage(Context context, String title, String message, String timeStamp, Intent intent, String imageUrl) {
notificationUtils = new NotificationUtils(context);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
}
使用上面的代码我得到了不同版本的不同图标。是否有任何错误?
答案 0 :(得分:0)
似乎后端正在通过通知键发送数据,然后图标的控件来自服务器,您无法处理,要求后端向您发送带null的通知并发送数据键中的所有数据>
答案 1 :(得分:0)
据我所记得,您应该有一个白色透明的图标。因此,如果您有一个彩色图标,它将以不同的方式显示。
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
这是一个常见的错误。您必须创建自己的PNG并将其放置在@drawable中。绘制一个仅包含白色(和透明背景)的轮廓。
答案 2 :(得分:0)
您需要在AndroidManifest.xml中添加它
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/statusbar_not_icon" />
另外,为您的notification Builder设置相同的图标,
notificationBuilder.setSmallIcon(R.drawable.statusbar_not_icon)