
Few months ago, I wrote an article for DNC Magazine titled “Hello Xamarin.Forms!” which was later republished online on DotnetCurry.com. This article demoed all awesomeness of Xamarin.Forms with a simple Vehicle Service Reminder app as an example. One of the aspects of the app is to remind users when their vehicle was due for service. While I showed how to go about doing that in iOS and Windows Phone, I left the Android part as an action item for the readers. While some of you may have got it already, if you haven’t, here’s how you do it –
In Android, unlike in iOS, local notifications does not take the fireDate as a parameter while configuring Notifications. So we have to split this task into two –
- Use system alarm services to wake your app up at a specified time (Using Alarm Manager)
- Write the code to notify users in the “OnReceive()” method of the Broadcast Receiver
Using Alarm Manager
AlarmManager class is used to schedule your application to be run at some point in the future. Create an Alarm Intent, specify the broadcast receiver and finally set the time when you intend to wake your app up.
public void Remind (DateTime dateTime, string title, string message)
{
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.ElapsedRealtime, SystemClock.ElapsedRealtime () + 5 * 1000, pendingIntent);
}
Notifying Users
Now that the AlarmManager will ensure that your app is run at a specified time, you will have to write the code to notify users in the “OnReceive()” method of the Broadcast Receiver. In this case the broadcast receiver is the AlarmReceiver – a custom class subclassed from BroadcastRecevier.
[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);
int resourceId;
if (App.SelectedModel.VehicleType == "Car")
resourceId = Resource.Drawable.Car;
else if (App.SelectedModel.VehicleType == "Bike")
resourceId = Resource.Drawable.Bike;
else
resourceId = Resource.Drawable.Other;
var wearableExtender = new NotificationCompat.WearableExtender()
.SetBackground(BitmapFactory.DecodeResource(context.Resources, resourceId))
;
//Generate a notification with just short text and small icon
var builder = new NotificationCompat.Builder (context)
.SetContentIntent (contentIntent)
.SetSmallIcon (Resource.Drawable.ic_launcher)
.SetContentTitle(title)
.SetContentText(message)
.SetStyle(style)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetAutoCancel(true)
.Extend(wearableExtender);
var notification = builder.Build();
manager.Notify(0, notification);
}
}
The code above uses NotificationCompat.Builder and sets the WearableExtender which will ensure the notifications are also sent to the connected Android Wear device. To know more about wearable programming using Xamarin, don’t forget to check out the post on Tips for your First Wear App.
You can download the full source from my GitHub repo.