Repeating Local Notification in android

2020-08-01 06:22发布

I am trying to make a notification, Whever app is installed, it automatically set alarm/notification every 1st of month and upon clicking that notification it opens a specific activity of the application

Here is my code for alarm manager

 // get a Calendar object with current time
    Calendar cal = Calendar.getInstance();
    // add 5 minutes to the calendar object
    cal.add(Calendar.MINUTE, 5);
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra(MESSAGE);

    PendingIntent sender = PendingIntent.getBroadcast(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Get the AlarmManager service
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

I have made an alarm class

 @Override
 public void onReceive(Context context, Intent intent) 
 {
   try {
         Intent newIntent = new Intent(context, Appointment.class);
         context.startActivity(newIntent);
        } 
   catch (Exception e) 
    {
       Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
         e.printStackTrace();
      }
}

My problem is, i want to set it as a repeating notification, each month on 1st. The alarm code, I have it in my mainActivity. How can i set it that it will repeat each month

1条回答
趁早两清
2楼-- · 2020-08-01 06:37

Schedule a repeating alarm can be achieved by setRepeating() of AlarmManager()

public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

Hope you can achieve your goal by checking which month is and modify the INTERVAL_DAY *28 Or INTERVAL_DAY*30 OR INTERVAL_DAY*31 by setting flags and all that.

 am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY*30, sender);
查看更多
登录 后发表回答