目前正在开发 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());
关于java - 创建通知android未在手机中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56030818/