对不起,我的英语不好。我想要按钮按下时的通知,按钮按下效果很好,因为祝酒词“ ITS OKAY!”正在显示,但不执行任何操作。我刚刚在按下按钮时在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());
}
我想要一个通知,但是我的代码什么也没做,只是吐司! :(
据说您必须为比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);
}
}