如何让Android AlarmManager可靠地发送重复警报?

时间:2018-01-04 01:34:17

标签: c# android xamarin alarmmanager repeatingalarm

我在Visual Studio 2017中使用Xamarin Forms为社交群组的成员编写了一个跨平台应用程序,该应用程序提供即将发生的事件的提醒以及回复参加邀请的方法。除提醒系统外,一切都在Android版本中有效。 (我要离开iOS直到稍后。)我发现我通常会在第二天早上收到通知,但之后不会收到通知。

提醒系统分为几个部分。

在MainActivity.cs的OnCreate()中,我有以下代码

        // Set the icon for notifications
        LocalNotificationsImplementation.NotificationIconId = Resource.Drawable.icon;

        // Prepare the periodic alarm.
        // The following is based on code in example 'BackgroundTasks' in 
        // https://github.com/adamped/BackgroundTasks

        // BackgroundReceiver sends a message to the main app to trigger
        // action on receipt of the alarm by code in IntroductionPage.xaml.cs
        var alarmIntent = new Intent(this, typeof(BackgroundReceiver));

        var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, 
            PendingIntentFlags.UpdateCurrent);

        // Set the time for alarms, once every day, in the hour following 5am.

        // How many hours till 6am?
        int hours_till_6 = 30 - DateTime.Now.Hour;
        if (hours_till_6 > 24)
            hours_till_6 -= 24;
        long time_since_boot = SystemClock.ElapsedRealtime();
        long first_alarm = time_since_boot + hours_till_6 * AlarmManager.IntervalHour;

        // Set the alarm
        var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
        alarmManager.SetInexactRepeating(AlarmType.ElapsedRealtime,
            first_alarm, AlarmManager.IntervalDay, pending);

以这种方式设置闹钟时间的原因是我希望用户的应用在稍微不同的时间发出警报,因为每个用户都会发出Web请求来检查新事件。顺便说一下。

然后在BackgroundReceiver.cs中,我有以下代码将警报发送到我的顶级项目中的代码

// Based on example in https://xamarinhelp.com/xamarin-background-tasks/
// See also MainActivity.cs for where this is set up to be called

[BroadcastReceiver]
public class BackgroundReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        // Call back to main application via messaging service
        MessagingCenter.Send<object>(this, "SendNotifications");
    }     
}

最后在我的第一页的构造函数中我有

        // Prepare to receive a message each day.
        // Messages are sent from BackgroundReceiver in Android, controlled
        // by AlarmManager in MainActivity.
        // *** IOS version not yet implemented.
        MessagingCenter.Subscribe<object>(this, "SendNotifications", (s) => {
            Device.BeginInvokeOnMainThread(PrepareSendNotifications);
        });

以后的功能

    private void PrepareSendNotifications()
    // Prepare and send notifications of occasions which are coming in the next few days.
    // Send the notifications using the LocalNotifications plugin.
    {
        /* This version is just for testing and this posting.
           The original looks at coming occasions, and notifies accordingly */
        CrossLocalNotifications.Current.Cancel(0);
        CrossLocalNotifications.Current.Show("Alarm", "BZEM notification time", 0);
    }

我发现我通常会在第二天早上收到通知,但之后不会收到通知。

任何人都可以告知我需要改变什么才能使这项工作可靠,或者是否建议采用不同的方法?

作为进一步的问题,任何人都可以建议如何在iOS中实现此功能,它似乎没有与AlarmManager等效的东西?

0 个答案:

没有答案