Schedule notification using alarm manager in xamar

2019-02-09 07:01发布

问题:

  • I have created a dependencie to show the notifications

  • In My DeviceDetails_Droid.cs I've set set alarm for 30 seconds

  • The functionality for local notification works perfectly when app is active but when I killed the app (close app) the alarm receiver not getting called.


public void ShowNotification(string message, string title)
{

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

    PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
    AlarmManager alarmManager = (AlarmManager) Forms.Context.GetSystemService(Context.AlarmService);

    //TODO: For demo set after 5 seconds.
    alarmManager.Set(AlarmType.RtcWakeup, DateTime.Now.Millisecond + 30000, pendingIntent);
}

  • In Androids MainActivity

[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive (Context context, Intent intent)
    {

        var message = intent.GetStringExtra ("message");
        var title = intent.GetStringExtra ("title");

        var notIntent = new Intent (context, typeof(MainActivity));
        var contentIntent = PendingIntent.GetActivity (context, 0, notIntent, PendingIntentFlags.CancelCurrent);
        var manager = NotificationManagerCompat.From (context);

        var style = new NotificationCompat.BigTextStyle();
        style.BigText(message);



        //Generate a notification with just short text and small icon
        var builder = new NotificationCompat.Builder (context)
            .SetContentIntent (contentIntent)
            .SetSmallIcon (Resource.Drawable.Icon)
            .SetContentTitle (title)
            .SetContentText (message)
            .SetStyle (style)
            .SetWhen (Java.Lang.JavaSystem.CurrentTimeMillis ())
            .SetAutoCancel (true);

        var notification = builder.Build();
        manager.Notify(0, notification);
    }
}

  • And in manifest file

<receiver 
 android:name=".AlarmReceiver" 
 android:enabled="true" 
 android:exported="true" 
 android:process=":remote" 
 android:label="AlarmReceiver">

  • The above code is running perfectly when app is in running state But the notification is not working when app is closed or killed

回答1:

1) If someone kills your app, alarms registered to your app are cancelled.

2) You can start your service in the background on Boot of the device in order to register your alarms, or run any other code you need to setup your notifications...

  • Add "android.intent.action.BOOT_COMPLETED" to your BroadcastReceiver:

[BroadcastReceiver]
[IntentFilter(new string[]{"android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver

  • In your manifest add in the boot complete permission:

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>    

In the case of Xamarin's Stock Price example, if you set "RECEIVE_BOOT_COMPLETED" you are "auto" restart your service and the your will start receiving notifications upon reboot of their phone without first launching your app:

[BroadcastReceiver]
[IntentFilter(new string[]{StockService.StocksUpdatedAction,Boo "android.intent.action.BOOT_COMPLETED"}, Priority = (int)IntentFilterPriority.LowPriority)]
public class StockNotificationReceiver : BroadcastReceiver

3) You can use a Service vs. a SeviceIntent and override the StartCommandResult to return Sticky

With a Sticky-based Service, it is restarted if it gets terminated.

Constant to return from onStartCommand(Intent, int, int): if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        return StartCommandResult.Sticky;
    }


回答2:

AlarmManager will work when the App is running in Foreground or in Background. But when the App is quit/force-stopped/killed Alarm Manager is also killed. Alarm will go off again only if Android is Re-booted or App is again launched by the user. Check these answers also for reference link1 , link2