【发布时间】:2018-11-17 11:07:27
【问题描述】:
我是 Android 编程新手,我正在尝试构建我的第一个应用程序。现在我想发送本地通知。不幸的是,我无法在运行 API 28 及更高版本的设备上收到通知。我知道自 Oreo 以来发送通知的方式发生了变化,并且我已经包含了创建通道的代码。似乎如果我在具有较低 API(例如 19)的模拟器上运行该应用程序,则会收到通知。此外,如果我将代码复制到一个新项目中,我也会收到通知,即使在运行 Android Oreo 的模拟器上也是如此。项目中的哪些设置可能导致在 Android Oreo 上收不到通知? (Logcat中没有错误)
我发送通知的代码:
public static final String CHANNEL_1_ID = "channel1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannels();
}
public void setNotif(View view) {
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("hey")
.setContentText("world")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
private void createNotificationChannels() {
// if higher than android oreo
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("This is channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
通过点击按钮调用 setNotif 方法。同样,我是 Android 编程的新手,所以任何建议都会有所帮助。甚至可能是我可以诊断问题的不同方式。谢谢!
【问题讨论】:
标签: java android android-studio notifications