当前位置: 首页 > 知识库问答 >
问题:

前台服务根据API 29上的信道ID而不是API 26返回通知错误。如何检索正确的信道ID?[副本]

虞俊美
2023-03-14

在智能手机(API 29)上运行此服务时,我收到以下错误,但在另一个(API26)中却没有。对于错误,我假设问题出在将我的服务作为前台服务运行时的通道ID,但我不知道如何解决它...我不知道哪个频道ID是合适的。有什么帮助吗?

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.testworkmanager, PID: 22582
    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=TestChannelID pri=2 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1969)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:224)
        at android.app.ActivityThread.main(ActivityThread.java:7520)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/MQSEventManagerDelegate: failed to get MQSService.

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.testworkmanager">

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <application
        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">
        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="false"
            android:icon="@drawable/ic_launcher_background"
            android:label="MyServiceLabel" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的服务:

package com.example.android.testworkmanager;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;


public class MyService extends Service {
    private static final int ONGOING_NOTIFICATION_ID = 2;
    private String channelID = "TestChannelID";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //TODO do something useful
        Log.v("APP_TEST","Service_startCommand");


        Intent notificationIntent = new Intent(this, MyService.class);
        PendingIntent pendingIntent =
                PendingIntent.getActivity(this, 0, notificationIntent, 0);


        Notification notification =
                new NotificationCompat.Builder(this, this.channelID)
                        .setContentTitle("TestNotification")
                        .setContentText("This is the text to display on notification")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(pendingIntent)
                        .setTicker("TickerText")
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .build();

        startForeground(ONGOING_NOTIFICATION_ID, notification);

        return Service.START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v("APP_TEST","Service_Create");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v("APP_TEST","Service_destroy");
    }

    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }
}

服务将通过以下命令启动和停止:

//For starting
        Intent intent = new Intent(this, MyService.class);
        startService(intent);

... (somewhere else)

//For stopping
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);

共有2个答案

华献
2023-03-14

看起来您缺少Android O及以上版本所需的通知通道。

文档:https://developer.android.com/training/notify-user/channels

尝试以下示例:

private String CHANNEL_ID;

private void createNotificationChannel() {
    CharSequence channelName = CHANNEL_ID;
    String channelDesc = "channelDesc";
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
        channel.setDescription(channelDesc);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        assert notificationManager != null;
        NotificationChannel currChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
        if (currChannel == null)
            notificationManager.createNotificationChannel(channel);
    }
}




public void createNotification(String message) {

    CHANNEL_ID = UiUtil.getStringSafe(R.string.app_name);
    if (message != null ) {
        createNotificationChannel();

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(UiUtil.getStringSafe(R.string.app_name))
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);
        Uri uri =RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        mBuilder.setSound(uri);


        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        int notificationId = (int) (System.currentTimeMillis()/4);
        notificationManager.notify(notificationId, mBuilder.build());
       }
}

我希望这个回答对你有帮助。非常感谢。

岳炎彬
2023-03-14

要在Android O及以上版本中启动前台服务,您必须在创建通知之前创建通道。您还必须在通知中添加相同的频道id。生成器或构造函数。以下是如何创建频道的示例:

/**
 * Method that create a channel for notification showed to user, for foreground service
 */
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel() {
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID,
            CHANNEL_NAME,  //name of the channel
            NotificationManager.IMPORTANCE_HIGH);   //importance level

    // Configure the notification channel.
    mChannel.setDescription("Description of your channel");
    mChannel.enableLights(true);

    // Sets the notification light color for notifications posted to this channel, if the device supports this feature.
    mChannel.setShowBadge(true);
    assert nm != null;
    nm.createNotificationChannel(mChannel);
}
 类似资料:
  • 所以,我有一个拥有50000用户的应用程序。最近我更新了我的android studio,把SDK编译成了27。我做了一些改变,以支持android 8的变化,包括通知的变化(例如,通知应该有唯一的渠道)。在Android One手机上测试了这个(运行android 8)。一切都很顺利。在更新playstore上的应用程序后,我在android 8设备上看到了这些崩溃(主要是谷歌Pixel手机)

  • 我想知道如何解析一个json对象,该对象将上载到我的服务器,以从中检索admob ad ID。 例子: 将发送到 任何帮助都将不胜感激! 编辑: 厌倦了这个: 我的Json文件:

  • 问题内容: 当我在Go中编写函数时,它应该返回一个值和一个错误,例如 我想在goroutine中执行此createHashedPassword,我想通过通道传递数据。 但是我的问题是,如何在此处或goroutine中处理错误? 问题答案: 通常将多个输出捆绑到一个结构中,然后通过一个通道将它们一起返回。

  • YouTube频道可以有几个“相关”频道的列表。例如,音乐频道有相关的流派频道:嘻哈、流行、摇滚、乡村等。 音乐频道:http://www.youtube.com/channel/UC-9-kyTW8ZkZNDHQJ6FgpwQ 音乐频道相关频道:http://www . YouTube . com/Channel/UC-9-kytw 8 zkzndhqj 6 fg pwq/Channels 我可

  • 给定频道的“虚荣url”(例如youtube.com/wheelockcollege,指向youtube.com/wheelockmarketing1),是否有办法检索频道详细信息? 我使用的是API的新版本3,以及最新的Python包。

  • 我无法从WebSphere Application Server与WebSphere MQ中的队列管理器进行远程连接。无论如何,它从安装在同一台机器上的WAS连接到队列管理器。我使用的是WebSphere MQ的7.5版和WebSphere Application Server的7.0版。 当试图远程连接到队列管理器时,随后记录了错误消息。 谢谢你,卡米尔