Notification channel error

陌路散爱 提交于 2021-02-10 14:14:24

问题


I have an app which will recieve FCM notifications.App was recieved notifications on os below Android oreo devices. But notificatons are not recieving in android oreo device.Instead of notification it gets toast "developer warning for package failed to post notification on channel null". I searched and understand that Notification channels are required.But I dont know where it is to be added.Please give me guidance.The toast is appearing on 8.0 emulator.In real device nothing is coming.


回答1:


UPDATE:

Notification channel is introduced in Android 8. It is common to create channel and assign to Notification Manager at Application class. sample code taken from Google Android. Following are the steps

Step 1. Add a class to extends Application and create channel Id in this class like following.

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);
        }
    }
}

Step 2. Edit AndroidManifest.xml such that

<?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>

Ensure the value of attribute android:name is the AppApplication class, like android:name="com.sample.app.AppApplication" in the code above

Step 3. When you build the notification anywhere in the app, use following sample code. Pay attention to 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);

Additional note from documentation that worth to look at,

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.

So you have to set targetSdkVersion to at least 26 to support it.



来源:https://stackoverflow.com/questions/51280615/notification-channel-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!