johnc
johnc

Reputation: 40223

Android notification sound not playing, intermittently

I have searched for this, so apologies if this has been answered already (I am happy to be redirected), but specifically our issue is intermittent.

Our clients are complaining that the notification audio is, intermittently, not 'chiming' when an event is sent to their phones from our software. It will work fine for a time, then 'just stop for a few hours' (extrapolating from the complaints of our customers).

We've not been able to reproduce this in house, but the frustration we are getting from our customers is such that we really need to fix it.

Our customers insist they are not making phone calls, playing audio or running other software, and from seeing their phones, I largely believe them.

We have noticed this largely on Jellybean, as this is what most of our clients are running, however it may not be isolated to this case.

Am I doing something wrong, or is there a better way to 'chime' for a notification. At this stage I am happy to try something different

void updateNotification()
{
    Notification.Builder builder = new Notification.Builder(_context);

    // Set the appearance of the notification:
    int icon;
    String title;
    String description;

    // ...
    //CODE setting icon, title and description
    // ...

    builder.setSmallIcon(icon);
    builder.setContentTitle(title);
    builder.setContentText(description);
    builder.setTicker(description);

    // Set the sound for the notification:
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));

    Intent intent = new Intent(_context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(MainActivity.EXTRA_TAB, MainActivity.EXTRA_TAB_TASKS);

    builder.setContentIntent(PendingIntent.getActivity(_context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));

    // Update the notification:
    NotificationManager manager = (NotificationManager)_context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(_notificationId, builder.build());
}

Upvotes: 1

Views: 1027

Answers (2)

johnc
johnc

Reputation: 40223

Whilst the answer @Ricardo gave is certainly worth including in your code, and I have left it in ours, my problem was largely due to the phone's power-management (this may have been obvious, but I'm newish to Android development).

Our notifications are required to be delivered when sent, and our user's phones were going into sleep mode, so we added a WakeLock (though, caring for their battery life, we only have it 'on' when the user was 'Signed In' to our app. At the end of their shift, the WakeLock was released).

Upvotes: 0

Ricardo
Ricardo

Reputation: 7965

For some reason, I noticed some devices mute all volumes (including the alarm volume) when they are put on silent or vibrate mode. And in those modes, for these devices, this volume can't be changed unless you change the mode.

I had users complaining of a similar problem and had to handle it by forcing a change of mode to ensure the volume is high enough. I then played the audio and switched the mode back to the original configuration.

Edit: Here is a post from a user complaining about this. Notice on the screenshot there that the volume button for "alarms" is disabled on mute.

Edit 2: In my particular case, I run the following check to see if I need to increase the volume:

if (audioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL
            && audioManager.getStreamVolume(AudioManager.STREAM_ALARM) == 0) {
    // Save current state and increase the volume
}

Upvotes: 1

Related Questions