gpt4 book ai didi

android - 服务通知 channel 无效 : Notification

转载 作者:行者123 更新时间:2023-12-04 00:23:55 28 4
gpt4 key购买 nike

只要我使用 Android Emulator 进行测试,我的代码就可以正常工作,但是一旦我构建它并尝试在我的手机上使用它,我就会收到以下错误:

    An uncaught Exception occurred on "main" thread.
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_01 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

StackTrace:
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=channel_01 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1760)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

这是我的代码:

@JavaProxy('org.nativescript.nstestandroid.ForegroundService')
class ForegroundService extends android.app.Service {
onStartCommand(intent, flags, startId) {
console.log('onStartCommand');
super.onStartCommand(intent, flags, startId);
return android.app.Service.START_STICKY;
}

onCreate() {
console.log('onCreate')
super.onCreate();
this.startForeground(1, this.getNotification());
}



private getNotification() {
const channel = new android.app.NotificationChannel(
'channel_01',
'ForegroundService Channel',
android.app.NotificationManager.IMPORTANCE_DEFAULT
);
// <const notificationManager = this.getSystemService(android.content.Context.NOTIFICATION_SERVICE) as android.app.NotificationManager;
//notificationManager.createNotificationChannel(channel);
const builder = new android.app.Notification.Builder(this.getApplicationContext(), 'channel_01');

return builder.build();
}

最佳答案

您需要先创建 NotificationChannel,然后才能将新的 Notification 发布到其中。像这样的:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);

// Don't see these lines in your code...
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

您只是创建新 channel (作为对象),但从不调用 createNotificationChannel

您可能在模拟器上创建了通知 channel ,但在设备上没有。出于兼容性目的,某些具有早期操作系统版本的设备也可能会自动创建“默认”通知 channel ,但较新的操作系统版本可能需要在显示通知之前创建 channel

HERE 中的一些指南

关于android - 服务通知 channel 无效 : Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58519864/

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