47

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

Run
  • 2,148
  • 2
  • 12
  • 29
Mohit Singh
  • 1,402
  • 1
  • 10
  • 19

8 Answers8

48

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.

Mehrdad
  • 92
  • 1
  • 12
Mohit Singh
  • 1,402
  • 1
  • 10
  • 19
16

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());
Herrbert74
  • 2,578
  • 31
  • 51
  • 1
    Good point. But did you forget the `NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel);` ? – Don Hatch Dec 15 '17 at 12:52
  • Also it'll crash if you use the 2-arg Builder constructor when android.os.Build.VERSION.SDK_INT < O, so that has to be inside the conditional too. – Don Hatch Dec 15 '17 at 18:19
  • @DonHatch The channel is already created above, I added the rest for clarification, but it's the same as before. The whole point of using NotificationCompat is that you can use the same format on all API levels, so it won't crash. It builds the correct Notification format for you, with or without channels. – Herrbert74 Dec 16 '17 at 21:42
  • 1
    Oh sorry, you're right about NotificationCompat; I had omitted the Compat in my code when I ran into the ctor problem. However, aren't you still missing the call to `notificationManager.createNotificationChannel(notificationCh‌​annel)`? – Don Hatch Dec 16 '17 at 23:28
  • Wonderful...Thanks bro – Rahul Singh Chandrabhan Sep 17 '18 at 06:00
10

You can solve this in two ways but for both of them you need to create a notification channel with an specific channel id.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String id = "my_channel_01";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(id, name,importance);
mChannel.enableLights(true);
mNotificationManager.createNotificationChannel(mChannel);

First way is to set channel for notification in constructor:

Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title");
mNotificationManager.notify("your_notification_id", notification);

Second way is to set the channel by Notificiation.Builder.setChannelId()

Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title").
setChannelId(id);
mNotificationManager.notify("your_notification_id", notification);

Hope this helps

Milad Moosavi
  • 1,587
  • 10
  • 20
4

First create the notification channel:

public static final String NOTIFICATION_CHANNEL_ID = "4655";
//Notification Channel
        CharSequence channelName = NOTIFICATION_CHANNEL_NAME;
        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 notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

then use the channel id in the constructor:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_timers)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setSound(null)
                .setContent(contentView)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setLargeIcon(picture)
                .setTicker(sTimer)
                .setContentIntent(timerListIntent)
                .setAutoCancel(false);
Gulzar Bhat
  • 1,255
  • 11
  • 13
3

This issue is related to the old FCM version.

Update your dependency to com.google.firebase:firebase-messaging:15.0.2 or higher.

This will fix the error

failed to post notification on channel null

when your app receives notifications in the background because now Firebase provides a default notification channel with basic settings.

But you also can specify the default Notification Channel for FCM in the manifest.

<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>

To learn more here

Yuri Popiv
  • 519
  • 4
  • 15
1

If you get this error should be paid attention to 2 items and them order:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(this, id);

Also NotificationManager notifManager and NotificationChannel mChannel are created only once.

There are required setters for Notification:

builder.setContentTitle() // required  
       .setSmallIcon()    // required 
       .setContentText()  // required  

See the example in On Android 8.1 API 27 notification does not display.

Andy Sander
  • 1,888
  • 1
  • 13
  • 16
1

I had a similar problem but starting an service as a background activity, in the service this code worked:

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

It's not the best solution but just to start the service in the background It does work

Douglas Caina
  • 932
  • 10
  • 8
0
String CHANNEL_ID = "my_channel";

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);
Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105
Gowtham
  • 9
  • 1
  • 1
    try to highlight the keywords and be clear with the format it will help to reach out your answer for others – Agilanbu Jan 03 '19 at 06:28