2

I want to show notifications in my app daily at specified time , The notifications are working fine . When i launch my app for first time no matter what time it is i get a notification and when i click on that notification it takes me to DetailActivity , when i click back button to get to MainActivity the another notification is created. I tried Every thing i could think of but nothing works.

P.S. It is my first time with notifications and AlarmManager.

Here is my code From MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    rvWord = findViewById(R.id.recyclerview);
    rvWord.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

    mDBHelper = new DatabaseHelper(this);

    dictionaryAdapter = new DictionaryAdapter();
    dictionaryAdapter.setCursor(mDBHelper.getDictionaryWord(""));
    rvWord.setAdapter(dictionaryAdapter);

    // Show banner ads
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    showTimedNotification(this); // Shows Notification

    showRatingDialog(); // Shows Rating Dialog

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    navigationView.setNavigationItemSelectedListener(this);

}
private void showTimedNotification(Context context) {

    Calendar updateTime = Calendar.getInstance();
    updateTime.set(Calendar.HOUR_OF_DAY, 8);
    updateTime.set(Calendar.MINUTE, 5);
    updateTime.set(Calendar.SECOND,10);

    Intent intent = new Intent(context, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);

}

1 Answers1

0

It's been a while since I used AlarmManager, but could this because you're setting the triggerAtMillis parameter to the current date with a time of 08:05:10, and in the cases where this is in the past, it fires the Alarm?

Each time you enter MainActivity onCreate the Alarm is scheduled again for the same time, thus re-firing the Alarm?

You could try adding: updateTime.add(Calendar.DAY_OF_YEAR, 1); When scheduling the alarm to make it fire at 8AM tomorrow morning.

If you wanted to be thorough, you could check if the current time is before 8AM (between 00:00 and 08:00) and only add the extra day if it is after 8AM, but try this out and see how it goes.

Mark
  • 535
  • 4
  • 13
  • I want to show the notification daily at 8 a.m , could please tell me how should I do it –  May 10 '19 at 15:45
  • Well you're doing everything right for the notification, but the problem is it's firing a bit early. I'll edit my answer to reflect this :) – Mark May 10 '19 at 15:47
  • @John updated answer with suggestion for adding 1 day to the trigger time – Mark May 10 '19 at 15:51
  • I will try your solution and let you know –  May 10 '19 at 16:01
  • Well it worked but instead of 08:05 the alarm fired at 09:08 –  May 11 '19 at 03:39
  • @John The only thing I can think of for that much of a difference would be a timezone issue? The `setInexactRepeating` doesn't guarantee that it will fire at exactly the time specified, but it shouldn't be late by 1 hour – Mark May 11 '19 at 08:49