Android 通知不执行任何操作
Android Notification does not do anything
我想要在按下按钮时收到通知,按钮按下效果很好,因为吐司“没问题!”正在显示,但未显示通知。当按下按钮时,我刚刚在 logcat 中发现了一个警告:
W/libEGL: EGLNativeWindowType 0x75cc082010 disconnect failed
我知道我必须使用 CHANNEL_ID,但它并没有更好。
public void sendNotification() {
Toast.makeText(this, "ITS OKAY!", Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, "CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("It is my notification's Title!")
.setContentText("Notification Body!");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
我想要通知,但我的代码只显示 toast
据说你必须为比 Android O 更新的版本创建一个通知渠道。正如文档中所说,你可以这样做:
createNotificationChannel();
Notification notification = new NotificationCompat.Builder(this, "channelID")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.build();
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel serviceChannel = new NotificationChannel(
"channelID",
"Channel Name",
importance
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
我想要在按下按钮时收到通知,按钮按下效果很好,因为吐司“没问题!”正在显示,但未显示通知。当按下按钮时,我刚刚在 logcat 中发现了一个警告:
W/libEGL: EGLNativeWindowType 0x75cc082010 disconnect failed
我知道我必须使用 CHANNEL_ID,但它并没有更好。
public void sendNotification() {
Toast.makeText(this, "ITS OKAY!", Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, "CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("It is my notification's Title!")
.setContentText("Notification Body!");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
我想要通知,但我的代码只显示 toast
据说你必须为比 Android O 更新的版本创建一个通知渠道。正如文档中所说,你可以这样做:
createNotificationChannel();
Notification notification = new NotificationCompat.Builder(this, "channelID")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.build();
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel serviceChannel = new NotificationChannel(
"channelID",
"Channel Name",
importance
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}