目前正在开发一个android应用程序,我的通知未显示在手机中。
我正在将实体电话连接到android studio,并且看到发送通知的方法已被调用,但未在电话中显示任何通知。
private void createNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_chat)
.setContentTitle("New match!!!")
.setContentText("You got a new match!!!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mNotificationManager.notify(001,builder.build());
}
先前会初始化
mNotoficationManager
,如下所示:mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
关于我接到该方法调用时为什么手机不显示通知的任何想法?从我创建的用于更改数据库中事件的服务中调用。
最佳答案
找到了从Android 8开始可以使用的代码。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "Tortuga");
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("New match on WeRoom")
.setContentText("You got a new match, you can now chat with someone more!!!")
.setAutoCancel(false)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String NOTIFICATION_CHANNEL_ID = "1001";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert mNotificationManager != null;
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify(0 /* Request Code */, mBuilder.build());