Oleksiy
Oleksiy

Reputation: 816

Android O notification not showing

I'm trying to implement notifications for Android O version. I've read about notification manager and channels. So Android O still doesn't want to reproduce notifications. On main Activity in PostCreate method I wrote this.

    NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String CHANNEL_ID = "my_channel_01";
    String CHANNEL_NAME = "my Channel Name";
    int NOTIFICATION_ID = 1;

    Intent notificationIntent = new Intent(this, MainActivity.class);

    PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    NotificationChannel notificationChannel = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        notificationChannel = new NotificationChannel(CHANNEL_ID,
                CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        mNotifyManager.createNotificationChannel(notificationChannel);
    }
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification myNotification = new NotificationCompat.Builder(MainActivity.this)
            .setContentTitle("You have been notify")
            .setContentText("This is your Notifiaction Text")
            .setSmallIcon(R.drawable.ic_donut_large_black_24dp)
            .setChannel(CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setContentIntent(notificationPendingIntent)
            .setAutoCancel(true)
            .setSound(alarmSound)
            .build();

    mNotifyManager.notify(NOTIFICATION_ID, myNotification);
    Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show();

After build for 26th API it doesn't create Notification, Toast message fires, and logs say me:

W/Notification: Use of stream types is deprecated for operations other than volume control W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

How to handle this case of error?

Upd. After some investigating, I've found that 26 api uses a little changes in notification builder. Now it accepts chanelid too. So for 26 use builder with two parameters.

Upvotes: 3

Views: 2993

Answers (4)

AEQ
AEQ

Reputation: 1429

I get the warning too but I noticed that I'm still actually getting the notification(pulled down the tray), just that it was silent and not displaying the icon in the system bar...

I see that your importance is set to low: NotificationManager.IMPORTANCE_LOW for a channel (mine was too), which wouldn't play the sound, you need to set it to IMPORTANCE_HIGH or IMPORTANCE_DEFAULT to actually see and hear your notification.

Upvotes: 2

Arun Sethumadhavan
Arun Sethumadhavan

Reputation: 76

Pass channel id while creating notification/notificationcompact object.

Notification.Builder(Context context, String channelId)

or

NotificationCompat.Builder(Context context, String channelId)

Upvotes: 4

xdbas
xdbas

Reputation: 723

You should not use the setSound method on the notifcation builder but instead use your created notification channel to set those settings.

notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);

Upvotes: 2

Frank
Frank

Reputation: 12300

Your alarmSound should probably be something else, not a URI?

Upvotes: -3

Related Questions