Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 8.1 W/Notification: Use of stream types is deprecated

I have a FLAG_ONGOING_EVENT notification remains in the status bar as long as my service is working and it updates the time every second.

After Android 8, I had added Notification Channel and both 8- and 8+ devices are working well, but 8+ devices keep filling my logcat with below warning, every second, which is quite annoying:

04-10 20:36:34.040 13838-13838/xxx.xxxx.xxxx W/Notification: Use of stream types is deprecated for operations other than volume control
See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

I am pretty sure that I set no sound to either channel or notification itself.

Here is how I create my notification channel:

channel = new NotificationChannel(CHANNEL_ONGOING_ID,
                getString(R.string.channel_ongoing_name),
                NotificationManager.IMPORTANCE_LOW);
channel.setDescription(getString(R.string.channel_ongoing_desc));
mNotificationManager.createNotificationChannel(channel);

And my notification:

RemoteViews view = new RemoteViews(getPackageName(),
    R.layout.notification_ongoing);

view.setImageViewResource(R.id.notification_button,
        mState == STATE_ONGOING ? R.drawable.ic_pause : R.drawable.ic_start);
view.setTextViewText(R.id.notification_text, Utils.getReadableTime(mMilliseconds));

view.setOnClickPendingIntent(R.id.notification_button,
    PendingIntent.getBroadcast(mContext, 1, new Intent(INTENT_NOTIFICATION_TOGGLE),
        PendingIntent.FLAG_UPDATE_CURRENT));

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext,
        CHANNEL_ONGOING_ID);

builder.setContent(view)
       .setOngoing(true)
       .setSmallIcon(R.drawable.ic_app_icon)
       .setContentIntent(PendingIntent.getActivity(mContext, 0,
               new Intent(mContext, MainActivity.class), 0));

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
    // before android 8 notification takes care of priority by itself, and start
    // from 8 the priority is with the channel.
    builder.setPriority(Notification.PRIORITY_DEFAULT);
}

Notification notification = builder.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;

And I did try to set sound to the channel like even I really don't need to make any sound, but still the warning is keeping all over my logcat.

channel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
        new AudioAttributes.Builder().setUsage(
            AudioAttributes.USAGE_NOTIFICATION).build())

Any idea? Help would be really appreciated!

like image 527
ZhouX Avatar asked Apr 10 '18 13:04

ZhouX


1 Answers

Had the exact same issue with a persistent Service Notification and tried numerous variations of setSound(null, null), etc with the NotificationChannel and using NotificationCompat.

The solution was to eliminate NotificationCompat when Build.VERSION.SDK_INT >= Build.VERSION_CODES.O, because the support libraries (appcompat-v7:26.1.0) still implement the deprecated Builder.setSound() methods which implicitly make use of the STREAM_ types which cause the WARNING.

So I now do the following and the warnings are gone:

Notification mNotification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mNotification =
            new Notification.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle("API Service")
                    .setContentText("Service is running")
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setContentIntent(pendingIntent)
                    .setTicker("API")
                    .build();
} else {
    mNotification =
            new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                    .setContentTitle("API Service")
                    .setContentText("Service is running")
                    .setSmallIcon(R.mipmap.ic_launcher_round)
                    .setSound(null)
                    .setContentIntent(pendingIntent)
                    .setTicker("API")
                    .build();
}
like image 176
Francois B Avatar answered Oct 16 '22 00:10

Francois B