在棉花糖中=显示白色图标。 在奇巧中 = 显示应用程序图标。 在奥利奥 = 在后台时显示带有应用程序图标的通知,但在前台时通知到达但不显示。
@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);
}
使用上面的代码,我得到了不同版本的不同图标。有什么错误吗?
后端正在通知密钥中发送数据,然后图标的控制权来自服务器,您无法处理它要求后端向您发送带有 null 的通知并发送数据密钥中的所有数据
据我所知,你应该有一个白色透明的图标。因此,如果您有彩色图标,则显示方式会有所不同。
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
这是一个常见的错误。您必须创建自己的PNG并将其放在@drawable中。绘制一个轮廓,仅包含白色(和透明背景(。
你需要在AndroidManifest中添加它.xml
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/statusbar_not_icon" />
此外,对于您的通知生成器,将相同的图标设置为
notificationBuilder.setSmallIcon(R.drawable.statusbar_not_icon)
如何创建状态栏图标?