9
  • 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
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
user797725
  • 89
  • 1
  • 6

2 Answers2

8

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;
    }
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • I looked at the first paragraph [here](https://developer.android.com/reference/android/provider/AlarmClock.html) and it sounds like maybe the `SET_ALARM` permission is not needed in this case, is that true? I am trying to schedule local notifications and wondering if I should be adding that permission or not. Seems to be working so far without it. Thanks. – hvaughan3 Dec 09 '16 at 18:26
  • @hvaughan3 if you are writing your own alarm clock application, you need to set that perm to receive intents. If you are receiving intents from an alarm app, no it is not needed. – SushiHangover Dec 09 '16 at 18:32
  • That makes sense now. Thank you – hvaughan3 Dec 09 '16 at 18:37
  • @SushiHangover, can you make us a favor and edit the question posted to your answer, because i want to know which class i am supposed to place the Dependency in PCL – Lutaaya Huzaifah Idris Oct 29 '17 at 11:39
  • @LutaayaHuzaifahIdris Are you talking about a `Xamarin.Forms`' based dependency service? If so post a new question as the question was concerning `Xamarin.Android` native and all this code is written within a Xamarin.Android application or library project – SushiHangover Oct 29 '17 at 11:50
  • @SushiHangover, help me on this one please https://stackoverflow.com/questions/47052256/how-can-i-call-a-broadcastreceiver-inside-a-service-with-a-local-notification-x – Lutaaya Huzaifah Idris Nov 01 '17 at 15:07
  • @SushiHangover, i would suggest also please to Edit your answer , and make a Tutorial Demonstrating A scheduled Alarm notification with sticky service , i will be very grateful – Lutaaya Huzaifah Idris Nov 01 '17 at 15:11
  • @SushiHangover, am having a problem with your answer the App crashes when i call **StartService()** in the MainActivity why ?, i dont know why. Check out this please https://stackoverflow.com/questions/47092507/android-crashes-with-startservice-calling?noredirect=1# –  Nov 03 '17 at 15:20
0

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

Community
  • 1
  • 1
Sreeraj
  • 2,306
  • 2
  • 18
  • 31