Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android lock device notification not sounding

I have developed an application which starts a custom service that fires a countdowntimer. The service runs a notification on the start of the timer and other on the finish. During tests I am seeing a problem about the notification and the lock screen. I describe the tests I have done:

1) If I run the application and dont let the phone going off or locked, the service plays notification sound correctly, as if the main application (activity) is in front as not. In both situations the service fires notifications perfectly.

2) If I lock the phone and the application was in front before lock, the service plays the notification sound correctly.

3) Here is my problem: if I lock the phone and the application was not in front before lock, the notification does not sound, but the service looks like is running, because when I press the "on" button in my phone, then inmediatly after screen turn on, it plays the notification (sound and icon), just in the lock screen before I can unlock the phone.

My code in the service cancel previous notification, this is the code that run notification after timer ends:

private void notificacion_fin() {
    NotificationManager notificationManager = 
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.cancel(0);
    Notification not = new Notification();
    // Ponemos la nueva notificacion de que hemos acabado
    SharedPreferences preferences = this.getSharedPreferences("com.fsp.mypref", 0);
    String not_sound = preferences.getString("notification_tone","");
    not.audioStreamType = AudioManager.STREAM_NOTIFICATION;
    not.sound = Uri.parse(not_sound);
    if (tipo_cronometro == 1) {
        not.icon = R.drawable.ic_stat_notify_time;
        not.tickerText = "Task has finished";
    } else {
        not.icon = R.drawable.ic_stat_notify_complete;
        not.tickerText = "Type 2 task has finished";
    }
    not.when = System.currentTimeMillis();
    not.defaults |= Notification.DEFAULT_VIBRATE;
    not.vibrate = new long[] {300,300,300,300,300};
    not.flags |= Notification.FLAG_AUTO_CANCEL;
    Intent notificationIntent = new Intent(this, MyActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    not.setLatestEventInfo(this, "My app", not.tickerText, contentIntent);
    notificationManager.notify(0, not); 
    notificacion = true;
}

I suspect my problem could be something relative to the context, could be? does anybody knows what can be happening? why in my 3th test does not run correctly?
Any help will be wellcome.
Thanks in advance.

P.S.: When I say notification sound I mean sound+icon

like image 847
Tibor Avatar asked Nov 05 '22 10:11

Tibor


1 Answers

Well, whenever you lock the screen (within a minute or two generally) the phone goes into sleep mode. You have three options that, two of which are better.

  1. Use startForeground on your service. That way, it will be in the front essentially. But it still would be affected by sleep mode.
  2. Use a WAKE_LOCK - This is my favorite option, but it requires an extra permission in the app which ignorant users don't generally understand.
  3. Set an alarm with Alarm Manager - Since playing a notification takes about no proccessing time, this is probably the best way to do it, and then you would send a broadcast when the alarm triggers which would cause a BroadcastReceiver to make the notification sound.

If you need a visual countdown timer, then also run the service, but don't rely on a service to stay awake without a WAKE_LOCK. Services also can be destroyed if the user has a bad phone or tries to use an app that uses way too much RAM - My phone would probably kill it because my phone sucks.

I like wake locks, but Alarm Manager is probably the best way to do it.

And the Context will stay the same regardless of the phone being awake or asleep. The context is based on the running process/class/block of code it's in. If the context were causing a problem, I would expect it to throw an exception and force close.

like image 176
Reed Avatar answered Nov 09 '22 04:11

Reed