【问题标题】:invalid channel for service notification: Notification服务通知的无效渠道:通知
【发布时间】:2019-10-23 09:38:27
【问题描述】:

只要我使用 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();
    }

【问题讨论】:

  • 你的手机是什么版本的?
  • 你为什么不简单地使用 {N} 个本地通知插件?

标签: android angular nativescript


【解决方案1】:

您需要先创建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);
}

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

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

HERE中的一些指南

【讨论】:

  • 上帝保佑你!它完全按照您的描述工作!非常感谢您,祝您有美好的一天!
【解决方案2】:
  1. 像这样创建应用程序类

    public class App extends Application {
    
    public static final String CHANNEL_ID = "exampleServiceChannel";
    
    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel();
    }
    
    private void createNotificationChannel() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, "Example Service Channel",NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(serviceChannel);
    
        }
    }
    }
    
  2. 在您的服务类中创建通知并调用 startForeground

Intent notificationIntent =  new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);        
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Example Service")
            .setContentText(userInput)
            .setSmallIcon(R.drawable.ic_android)
            .setContentIntent(pendingIntent)
            .build();
    
    
        startForeground(1,notification);
  1. 非常重要 -> 忘记在清单文件中提及您的应用程序类 App -> android:name=".App"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.androidservicestemplate">
    
        <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> 
    <application
            android:name=".App"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"></activity>
            <service android:name=".ExampleService" />
        </application>
    
    </manifest>

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多