我不知道为什么此通知将不会显示在设备上。我正在使用的设备正在运行OS9。任何帮助将是巨大的!
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("TestApp", "Notification Received : " + remoteMessage.getData());
NotificationUtils.getInstance(getApplicationContext()).createChannel(NotificationUtils.DEFAULT_CHANNEL);
NotificationCompat.Builder notification = buildBasicNotification(getApplicationContext(), remoteMessage.getFrom(), remoteMessage.getData().get("_msg"), NotificationUtils.DEFAULT_CHANNEL);
displayNotification(notification);
}
public static NotificationCompat.Builder buildBasicNotification(Context context, String title, String msgText, String channelId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msgText)
.setSmallIcon(R.drawable.alert_icon)
.setAutoCancel(false);
return builder;
}
private void displayNotification(NotificationCompat.Builder builder) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
这是NotificationUtils内部的两个重要方法
public static NotificationUtils getInstance(Context context) {
if (instance == null) {
instance = new NotificationUtils(context);
}
return instance;
}
NotificationUtils(Context context) {
super(context);
this.context = context;
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createAlertChannel();
deleteChannel("Miscellaneous");
}
}
答案 0 :(得分:0)
我终于明白了。
该方法内部有错误。
public static NotificationCompat.Builder buildBasicNotification(Context context, String title, String msgText, String channelId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msgText)
.setSmallIcon(R.drawable.alert_icon)
.setAutoCancel(false);
return builder;
}
确保NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
接受channelId。
所以应该是... NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)