通知通道错误

Notification channel error

我有一个将接收 FCM 的应用程序 notifications.App 在 Android oreo 设备下面的 os 上收到了通知。但是通知没有在 android oreo device.Instead 中收到它得到 toast "developer warning for package failed to post notification on channel null" 的通知。我搜索并了解到通知渠道是 required.But 我不知道它在哪里 added.Please 给我 guidance.The toast 出现在 8.0 emulator.In 真实设备上什么都没有。

更新:

通知通道在Android8 中引入。通常在应用程序class 中创建通道并分配给通知管理器。示例代码取自 Google Android。以下是步骤

步骤 1。添加一个 class 以扩展应用程序并在此 class 中创建频道 ID,如下所示。

com.sample.app.Application.java

public class AppApplication extends Application{

    public static final String CAHNNEL_ID = "default_channel_id";

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannel()
    }

    private void createNotificationChannel() {
        // 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) {
            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);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

步骤 2。编辑 AndroidManifest.xml 使得

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.app">
    <application
        android:name="com.sample.app.AppApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        ...
    </application>

</manifest>

确保属性android:name的值是AppApplicationclass,如上面代码中的android:name="com.sample.app.AppApplication"

步骤 3。当您在应用程序的任何位置构建通知时,请使用以下示例代码。关注new NotificationCompat.Builder(this,CHANNEL_ID)

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

文档中值得一看的附加说明,

Notice that the NotificationCompat.Builder constructor requires that you provide a channel ID. This is required for compatibility with Android 8.0 (API level 26) and higher, but is ignored by older versions.

所以targetSdkVersion至少要设置为26才能支持。