Home > OS >  Android xamarin local notification in specific day and month
Android xamarin local notification in specific day and month

Time:09-29

For some time he has been trying to set a notification in the application for a given day and month. Unfortunately, the notification appears after a few seconds of calling it every day.

This is my code:

private void Remind_Click(object sender, System.EventArgs e)
    {
        string title = "If you see this";
        string message = "it means it works";

        Intent alarmIntent = new Intent(Application.Context, typeof(AlarmReceiver));
        alarmIntent.PutExtra("message", message);
        alarmIntent.PutExtra("title", title);

        var pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
        var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();

        DateTime nowDate = DateTime.Now;
        int month = 09;
        int day = 24;
        DateTime newDate = new DateTime(nowDate.Year, month, day);                   

        DateTime date = newDate;
        var ms = (long)(date - new DateTime(1970, 1, 1)).TotalMilliseconds;
        alarmManager.SetInexactRepeating(AlarmType.RtcWakeup, 3600000, ms, pendingIntent);

        ShowSnack(main, $"Set date: {date} ");
    }

Please help me with this problem.

CodePudding user response:

  1. For setInexactRepeating (int type, long startTime, long intervalTime, PendingIntent pi); The second parameter is the start execution time, and the third parameter is the interval time between two executions. So you have an error in the input of the parameters.
  2. If you only trigger the event at one time, you can use set(int type, long startTime, PendingIntent pi); Using this method will only trigger the event once.

Note: Because you are using AlarmType.RtcWakeup. The time of DateTime.Now may be different from Utc time (you can use DateTime.Now and DateTime.UtcNow to see if there is a time difference between the two).

  • Related