gpt4 book ai didi

java - 通知 oreo api 26 - 工作正常,现在不行

转载 作者:行者123 更新时间:2023-12-02 10:53:05 25 4
gpt4 key购买 nike

在实现 channel 等后,我的通知工作完美,但随后它突然停止工作。我想知道为什么,可能是依赖项,还是我的代码有其他问题?

通知代码:

 public void initiateNotification(){

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder = new Notification.Builder(this);


Intent notiIntent = new Intent(this, MainPage.class);
notiIntent.putExtra("fileName", storage.getFileName(Calendar.getInstance()));
notiIntent.setAction("NOTI");
notiIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pi = PendingIntent.getActivity(this, 0, notiIntent, 0);

Intent stopIntent = new Intent(this, Services.class);
stopIntent.setAction("STOP");
PendingIntent stopPi = PendingIntent.getService(this, 0, stopIntent, 0);
Notification.Action stopAction = new Notification.Action.Builder(null, "STOP", stopPi).build();



//test
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

Notification noti = new Notification.Builder(Services.this)
.setContentTitle("Well Done! Stay Active!")
.setContentText("Number of Steps : " + mSteps)
.setSmallIcon(R.drawable.bino_small)
.setContentIntent(pi)
.setChannelId(CHANNEL_ID)
.build();


startForeground(1, noti);
}

SDK 版本:

  • 编译SdkVersion:27
  • buildTools版本:27.0.3
  • minSdkVersion:26
  • 目标SdkVersion:26

我查看了许多解决方案并尝试了所有解决方案,但似乎没有任何帮助。提前致谢。

最佳答案

您还需要创建通知 channel ,以防通知 channel 不存在。 (第一次启动时总是如此。)

这可以通过

来完成
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);

另请注意,创建 channel 后您无法再更改重要性。

更新

添加 channel 创建是在 channel 初始化后完成的。所以你的代码中的这一行:

NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

将成为

NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(mChannel);

关于java - 通知 oreo api 26 - 工作正常,现在不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52010775/

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