Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to post notification on channel "null" Target Api is 26

Two log showing

1: Use of stream types is deprecated for operations other than volume control

2: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

Showing this

like image 487
Mohit Singh Avatar asked Aug 16 '17 11:08

Mohit Singh


2 Answers

From the developer documentation:

When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users.

int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    CHANNEL_ID = "my_channel_01";
    CharSequence name = "my_channel";
    String Description = "This is my channel";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
    mChannel.setDescription(Description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    mChannel.setShowBadge(false);
    notificationManager.createNotificationChannel(mChannel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(title)
    .setContentText(message);

Intent resultIntent = new Intent(ctx, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(NOTIFICATION_ID, builder.build());

One interesting thing to note: If a channel already exists, calling createNotificationChannel has no effect, so you can create all channels whenever you start your application.

like image 53
Mohit Singh Avatar answered Oct 11 '22 06:10

Mohit Singh


Gulzar Bhat's answer is working perfectly if your minimum API is Oreo. If your minimum is lower however, you have to wrap the NotificationChannel code in a platform level check. After that you can still use the id, which will be ignored pre Oreo:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    int importance = NotificationManager.IMPORTANCE_LOW;
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.enableVibration(true);
    notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());
like image 16
Herrbert74 Avatar answered Oct 11 '22 08:10

Herrbert74