user2790880
user2790880

Reputation: 87

Sometimes my Notification not shows

Why does my Notification sometimes not showing?

I create setRecurringAlarm(this); in MainActivity and I would like to use this code to show the notification every 30 minutes

This is my code:

private void setRecurringAlarm(Context context) {
    Log.d("MyService", "setRecurringAlarm() activated...");
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getDefault());
        updateTime.set(Calendar.HOUR_OF_DAY, 12);
        updateTime.set(Calendar.MINUTE, 30);
        Intent downloader = new Intent(context, MyStartServiceReceiver.class);
        downloader.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, downloader, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
   }

And this is my MyStartServiceReceiver :

public class MyStartServiceReceiver extends BroadcastReceiver {
       @Override
        public void onReceive(Context context, Intent intent) {
        Intent dailyUpdater = new Intent(context, MyService.class);
        context.startService(dailyUpdater);
        Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
       }
    }

This is my MyService :

public class MyService extends IntentService {
    public MyService() {
       super("MyServiceName");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("MyService", "Execute MyTask");
        new MyTask().execute();
        this.sendNotification(this);
    }
    private class MyTask extends AsyncTask<String, Void, Boolean> {
        @Override
         protected Boolean doInBackground(String... strings) {
               Log.d("MyService", "Calling doInBackground() in MyTask");
               return false;
        }
    }        
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void sendNotification(Context context) {
        Log.d("MyService", "Execute sendNotification()");
        // here is my notification code --> http://stackoverflow.com/questions/20558619/how-to-set-notification-to-clear-itself-on-click  

     }
}

Upvotes: 0

Views: 600

Answers (1)

Madhur Ahuja
Madhur Ahuja

Reputation: 22691

You are not using wakelock in your IntentService. There is no gaurantee that your phone will be awake because BroadcastReciever will return immediately. You need to have wakelock in your IntentService

Check this out

AlarmManager.setRepeating - Service does not seem to repeat

Upvotes: 1

Related Questions