Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Alarm Manager doesn't work with Android API 25

$
0
0

Hi. I have a problem with Alarm Manager that doesn't fired my schedule notification.
using Android.App;
using Android.Content;
using GNote.Android;
using GNote.Droid;
using GNote.Converters;
using System.Collections.Generic;
using Xamarin.Forms;
using GNote.Droid.Models;
using Android.OS;
using GNote.Res.SQLite;
using Android.Icu.Util;
using System;
using GNote.Models;

[assembly: Dependency(typeof(MyNotification))]
namespace GNote.Droid
{
    class MyNotification : INotification
    {
       private List<Note> ListNotes;
       Intent alarmIntent;
       DateTime baseTime = new DateTime(1970, 1, 1);

        public async void CreateScheduleNotifications()
       {
            ListNotes = await DataBaseService.getTimeActiveNotes();
           foreach(Note note in ListNotes )
            {
                long scheduleAlarmTime = this.setScheduleAlarmTime(note.AlarmTime);

                this.addNewScheduleNotification(note.Id,note.Title,note.Content,scheduleAlarmTime);
            }
        }

        private void addNewScheduleNotification(int id,string title, string content,long alarmTime)
        {
           alarmIntent = new Intent(MainActivity.AndroidContext, typeof(AlamReciver));

            alarmIntent.PutExtra("id", id);
            alarmIntent.PutExtra("title", title);
            alarmIntent.PutExtra("content", content);

            var PI = PendingIntent.GetBroadcast(MainActivity.AndroidContext, id, alarmIntent, PendingIntentFlags.CancelCurrent);
            AlarmManager alarmManager = (AlarmManager)MainActivity.AndroidContext.GetSystemService(Context.AlarmService);
            alarmManager.Set(AlarmType.RtcWakeup, alarmTime, PI);
        }

        private long setScheduleAlarmTime(DateTime noteAlarmTime)
        {
            TimeSpan calcTime = noteAlarmTime - baseTime;
            return (long)calcTime.TotalMilliseconds;
        }
    }
}

My "noteAlarmTime" format is (y,m,d,h,m,s).

It works perfect on emulator with API 23 but when I launch app on API 25 then nothing happen.
During testing I noticed something is wrong with this line:

alarmManager.Set(AlarmType.RtcWakeup, alarmTime, PI);

When I change it to:

alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 5 * 1000, PI);
then notification was displayed.

So, is my time calculation is wrong? Or is different time system between API 23 and 25.

Or problem is somewhere else.

Thank you.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>