failed to post notification channel 1 code example

Example: failed to post notification on channel null android

public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1001;

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        String id = "_channel_01";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel mChannel = new NotificationChannel(id, "notification", importance);
        mChannel.enableLights(true);

        Notification notification = new Notification.Builder(getApplicationContext(), id)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My chat")
                .setContentText("Listening for incoming messages")
                .build();

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(mChannel);
            mNotificationManager.notify(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
        }

        startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
    }
}

Tags:

Misc Example