gpt4 book ai didi

android - 通过单击按钮创建 android 通知

转载 作者:行者123 更新时间:2023-11-29 18:27:00 26 4
gpt4 key购买 nike

我尝试创建 android 通知,当我点击一个按钮但不起作用时。这是我在按钮中写的代码

NotificationManager Nm;
public void Notify(View view) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("LOGIN")
.setContentText("You are login successfully")
.setSmallIcon(R.drawable.done);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}

最佳答案

正如 Ikazuchi 所说,对于 android 8 之后的 android 版本,您需要添加一个通知 channel 。这是可以做到的,如此处的文档所示:https://developer.android.com/training/notify-user/build-notification#java ,像这样:

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);
}
}

关于android - 通过单击按钮创建 android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58302133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com