Wake up application on tapping Push Notifications in Xamarin Android - xamarin

I am trying to integrate Push notifications to the application.
The behaviour i am trying to implement is waking up the app from background and resuming to previous state.
But the control always come to protected override void OnCreate (Bundle bundle)
initially and then to OnResume upon tapping the notification message instead of coming
to protected async override void OnResume().
Because of this the app is always relaunching instead of waking up.
How can i just wake the app instead of relaunching when tapped on notification.?
Here is how the notification is created.
private void CreateNotification(string title, string desc, string type)
{
Random random = new Random();
int id = random.Next(9999 - 1000) + 1000;
const int pendingIntentId = id;
const int notificationId = id;
var startupIntent = new Intent(this, typeof(MainActivity));
var stackBuilder = TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
stackBuilder.AddNextIntent(startupIntent);
var pendingIntent =
stackBuilder.GetPendingIntent(pendingIntentId, PendingIntentFlags.OneShot);
var builder = new Notification.Builder(this)
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(desc)
.SetSmallIcon(Resource.Drawable.appicon);
var notification = builder.Build();
notification.Flags = NotificationFlags.AutoCancel;
var notificationManager =
GetSystemService(NotificationService) as NotificationManager;
notificationManager.Notify(notificationId, notification);
}
Other scenarios like when the app is not running and in foreground are working as expected.

Related

Local notifications not shown

Help me find the error in the code
My MainActivity:
string CHANNEL_ID = "MyChannel";
protected override void OnCreate(Bundle savedInstanceState)
{
....
...
CreateNotificationChannel();
LoadApplication(new App());
}
public void CreateNotification(string SetContentTitle, string SetContentText)
{
var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.SetContentTitle(SetContentTitle)
.SetContentText(SetContentText)
.SetChannelId(CHANNEL_ID)
.SetSmallIcon(Resource.Drawable.icon);
// Finally, publish the notification:
var notificationManager = NotificationManagerCompat.From(this);
// Get the notification manager:
// NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
int notificationId = 1000;
notificationManager.Notify(notificationId, builder.Build());
}
void CreateNotificationChannel(){
if (Build.VERSION.SdkInt<BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
string name = "MyName";
var description = "desc";
var channel = new NotificationChannel(CHANNEL_ID, name, NotificationImportance.High)
{
Description = description
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
and I have a class in which when I need I start displaying local notifications using the code below
var urlOb = new MainActivity();
urlOb.CreateNotification(titleback, textback);
I think I did everything right, but local notifications are not shown.
You can call the method in other activity
public static MainActivity instance;
In the method onCreate
//...
base.OnCreate(savedInstanceState);
instance = this;
And you can call it in other method
MainActivity.instance.CreateNotification(titleback, textback);

How to open a Content page after FCM notification click on Xamarin Forms?

I already implement FCM notification on xamarin forms android.so I want,when user click on notification then it will open a Xamarin forms content page.
here is my code for receiving FCM notification:
Inside MyFireMessagingService class-
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
}
public void SendNotificatios(string body, string Header)
{
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.AppLauncher);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
builder.SetContentTitle(Header);
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
so How to open a content page after click on notification?
As the first thing I have replaced
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
}
With
public override void HandleIntent(Intent intent)
{
CreateNotification(intent);
}
Then created a new method CreateNotification as follows,
private void CreateNotification(Object e)
{
try
{
string title = "";
string body = "";
var intent = new Intent(this, typeof(MainActivity));
var i = e as Intent;
var bundle = i.Extras;
title = bundle.GetString("gcm.notification.title");
body = bundle.GetString("gcm.notification.body");
intent.PutExtra("title", title);
intent.PutExtra("body", body);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop );
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.AppLauncher);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.AppLauncher));
builder.SetContentTitle(Header);
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Now we have to update the MainActivity.cs to handle the push notification click by overriding OnNewIntent method as follows
protected async override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
var title = intent.GetStringExtra("title");
//This method will get called while the app is launching from the app icon or from the notification
if (title != null)
{
//Means new Intent from push notification
//Code to open the page
}
}
Make sure you have kept LaunchMode = LaunchMode.SingleTask in the activity

AlarmManager and Notifications in Android 8.0(Oreo)

In my app i have a alarm that is repeting every thursday, i am using AlarmManger, everything is working fine in all previous versions of android, but now with android 8.0(oreo), the alarm Isn't firing, below is the classes that i use to set my alarm.
From what i searched i need to set the alarm explicitly, but I
dont understand how.
MainActivity:
try
{
Intent alarmIntent = new Intent(this, typeof(AlarmReceiver));
PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
if (Settings.AccessAlarm == 0)
{
alarmManager.SetRepeating(AlarmType.RtcWakeup, BootReceiver.FirstReminder(), BootReceiver.reminderInterval, pending);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);
Settings.AccessAlarm = 1;
}
}
catch (Exception e)
{
Settings.AccessAlarm = 0;
}
Bootreceiver.cs:
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
//the interval currently every one minute
//to set it to dayly change the value to 24 * 60 * 60 * 1000
public static long reminderInterval = AlarmManager.IntervalDay * 7;
//public static long reminderInterval = 3 * 1000;
public static long FirstReminder()
{
System.Random rnd = new System.Random();
int minutenumber = rnd.Next(20, 40);
Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.Set(Java.Util.CalendarField.DayOfWeek, Calendar.Thursday);
calendar.Set(Java.Util.CalendarField.HourOfDay, 10);
calendar.Set(Java.Util.CalendarField.Minute, minutenumber);
return calendar.TimeInMillis;
}
public override void OnReceive(Context context, Intent intent)
{
try
{
Console.WriteLine("BootReceiver: OnReceive");
var alarmIntent = new Intent(context, typeof(AlarmReceiver));
var pending = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
alarmManager.SetRepeating(AlarmType.RtcWakeup, FirstReminder(), reminderInterval, pending);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0);
Settings.AccessAlarm = 1;
}
catch (Exception e)
{
Settings.AccessAlarm = 0;
}
}
}
AlarmReceiver:
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
private int z = 0;
private int i;
public override void OnReceive(Context context, Intent intent)
{
try
{
Settings.AlarmNotification = 1;
if (System.DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
{
Settings.AlarmCount =0;
}
var title = "Test";
var message = "Something";
Intent backIntent = new Intent(context, typeof(MainActivity));
backIntent.SetFlags(ActivityFlags.NewTask);
var resultIntent = new Intent(context, typeof(MainActivity));
PendingIntent pending = PendingIntent.GetActivities(context, 0,
new Intent[] { backIntent, resultIntent },
PendingIntentFlags.OneShot);
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(1331, notification);
}
catch (Exception)
{
}
}
}
Your alarms would be firing correctly as you are using explicit intents. But in Oreo/API26 .SetDefaults(NotificationDefaults.All is obsolete and will cause a silent failure and your notifications will not be displayed.
So you need to setup a notification channel to properly display your notification with all the bells and whistles.
If you replace your notification builder & notify code:
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
~~~~
manager.Notify(1331, notification);
With an API check for Oreo/API26(+) you can established an notification channel for your app:
using (var notificationManager = NotificationManager.FromContext(context))
{
Notification notification;
if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
{
notification = new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetDefaults(NotificationDefaults.All)
.SetContentIntent(pending)
.Build();
}
else
{
// Setup a NotificationChannel, Go crazy and make it public, urgent with lights, vibrations & sound.
var myUrgentChannel = context.PackageName;
const string channelName = "SushiHangover Urgent";
NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.SetSound(
RingtoneManager.GetDefaultUri(RingtoneType.Notification),
new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel?.Dispose();
notification = new Notification.Builder(context)
.SetChannelId(myUrgentChannel)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pending)
.Build();
}
notificationManager.Notify(1331, notification);
notification.Dispose();
}
Now in settings, your app has a channel assigned to it that the user can customize if they so choose:

Xamarin.android Notification onClick doesn't take to new activity

This is going to be a long post! (grab a cup of coffee/popcorn)
I am using AltBeacon Xamarin sample in my code to show the beacons.
I have come across this example in creating Notifications in Xamarin.
Here there's an Application class where the core logic goes.
public class AltBeaconSampleApplication : Application, IBootstrapNotifier
{
private const string TAG = "AltBeaconSampleApplication";
BeaconManager _beaconManager;
private RegionBootstrap regionBootstrap;
private Region _backgroundRegion;
private BackgroundPowerSaver backgroundPowerSaver;
private bool haveDetectedBeaconsSinceBoot = false;
private string nearbyMessageString = "A beacon is nearby.";
private string nearbyTitleString = "AltBeacon Reference Application";
private MainActivity mainActivity = null;
public MainActivity MainActivity
{
get { return mainActivity; }
set { mainActivity = value; }
}
private NotificationActivity notificationActivity = null;
public NotificationActivity NotificationActivity
{
get { return notificationActivity; }
set { notificationActivity = value; }
}
public AltBeaconSampleApplication() : base() { }
public AltBeaconSampleApplication(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer) { }
public override void OnCreate()
{
base.OnCreate();
_beaconManager = BeaconManager.GetInstanceForApplication(this);
var iBeaconParser = new BeaconParser();
// Estimote > 2013
iBeaconParser.SetBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24");
_beaconManager.BeaconParsers.Add(iBeaconParser);
Log.Debug(TAG, "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
_backgroundRegion = new Region("backgroundRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, _backgroundRegion);
// simply constructing this class and holding a reference to it in your custom Application
// class will automatically cause the BeaconLibrary to save battery whenever the application
// is not visible. This reduces bluetooth power usage by about 60%
backgroundPowerSaver = new BackgroundPowerSaver(this);
PerformHttpRequest();
}
public void DidDetermineStateForRegion(int state, AltBeaconOrg.BoundBeacon.Region region)
{
}
public async void PerformHttpRequest()
{
try
{
using (var client = new HttpClient())
{
var uri = "http://exampleuri";
var result = await client.GetStringAsync(uri);
var response = JsonConvert.DeserializeObject<BeaconURL>(result);
SendNotificationFromBeacon(response);
}
}
catch(Exception ex)
{
throw ex;
}
}
private void SendNotificationFromBeacon(BeaconURL receivedNotification)
{
// Setup an intent for SecondActivity:
Intent notificationIntent = new Intent(this, typeof(NotificationActivity));
// Pass some information to SecondActivity:
notificationIntent.PutExtra("CompaignUrl", receivedNotification.CompaignUrl);
notificationIntent.PutExtra("MediaUrl", receivedNotification.MediaUrl);
notificationIntent.PutExtra("titleText", receivedNotification.Title);
notificationIntent.SetFlags(ActivityFlags.NewTask);
// Create a task stack builder to manage the back stack:
Android.App.TaskStackBuilder stackBuilder = Android.App.TaskStackBuilder.Create(this);
// Add all parents of SecondActivity to the stack:
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationActivity)));
// Push the intent that starts SecondActivity onto the stack:
stackBuilder.AddNextIntent(notificationIntent);
// Obtain the PendingIntent for launching the task constructed by
// stackbuilder. The pending intent can be used only once (one shot):
const int pendingIntentId = 0;
PendingIntent pendingIntent =
stackBuilder.GetPendingIntent(pendingIntentId, PendingIntentFlags.OneShot);
// Instantiate the builder and set notification elements, including
// the pending intent:
var builder =
new NotificationCompat.Builder(this)
.SetContentTitle(receivedNotification.Title)
.SetContentText(receivedNotification.Text)
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo);
// Build the notification:
Notification notification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager =
GetSystemService(Context.NotificationService) as NotificationManager;
// Publish the notification:
const int notificationId = 0;
notificationManager.Notify(notificationId, notification);
}
}
BeaconURL is a POCO class
NotificationActivity is a basic Activity class.
I perform the HttpClient request and get data. I create a notification and present it on my screen. It goes like this
Now when I tap on the notification, I dont go to the NotificationActivity. I am trying to invoke an activity from an ApplicationClass. Is this the right way to perform such stuff. Kindly provide details.
Thanks.
Edit: Added NotificationActivity Class
[Activity(Label = "NotificationActivity")]
public class NotificationActivity : MainActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.NotificationLayout);
TextView titleTextView = FindViewById<TextView>(Resource.Id.txtTitle);
titleTextView.Text = Intent.Extras.GetString("titleText", "");
ImageView mediaImage = FindViewById<ImageView>(Resource.Id.imgViewMedia);
mediaImage.SetImageBitmap(GetImageBitmapFromUrl(Intent.Extras.GetString("MediaUrl", "")));
}
private Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
}
First thing you need to do is to set the your pending intent within the notification builder, it will get your NotificationActivity launching:
var builder =
new NotificationCompat.Builder(this)
.SetContentTitle("receivedNotification.Title")
.SetContentText("receivedNotification.Text")
.SetSmallIcon(Android.Resource.Drawable.IcDialogInfo)
.SetContentIntent(pendingIntent);
The second will be to get your back stack setup, from what you posted I'm not sure what the flow should be as the user will exit the app if they use the back button.
If you want the user to go back to the MainActivity when that press the back button, then you can add a ParentActivity to your NotificationActivity activity attribute, i.e.:
[Activity(Label = "NotificationActivity", ParentActivity = typeof(MainActivity))]
And thus the line:
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(NotificationActivity)));
Would add the MainActivity to the back stack.

Xamarin Android - How to schedule and alarm with a BroadcastReceiver

(I'm asking here because I didn't get help at Xamarin forums) I'm creating an alarm with this code:
Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
notificationClickIntent = PendingIntent.GetActivity(context, 0, new Intent(), 0);
pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
am = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
DateTime setTime = new DateTime(temp.Ticks + offset); //temp is the current time where seconds field = 0
if ((int)Build.VERSION.SdkInt >= 21) //my device enters this case
{
AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(setTime.Ticks, notificationClickIntent);
am.SetAlarmClock(info, pendingIntent);
}
else {
am.SetExact(AlarmType.RtcWakeup, setTime.Ticks, notificationClickIntent);
}
Before that code is called, my class makes sure that these have also been executed:
ComponentName receiver = new ComponentName(context, Java.Lang.Class.FromType(typeof(AlarmReceiver)));
PackageManager pm = context.PackageManager;
pm.SetComponentEnabledSetting(receiver, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp);
Intent alarmIntent = new Intent(context, typeof(AlarmReceiver));
notificationClickIntent = PendingIntent.GetActivity(context, 0, new Intent(), 0);
pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
am = (AlarmManager)Android.App.Application.Context.GetSystemService(Context.AlarmService);
And here is my receiver:
[BroadcastReceiver (Process = ":remote")]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Console.WriteLine("alarm fired");
Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
}
}
Okay, so the receiver is being registered by Xamarin correctly. I know this because if I give an incorrect tick value to AlarmClockInfo (a value outside of DateTime's tick range) the alarm goes off immediately and my OnReceive method is called. However when I give it a tick value, say a minute ahead of the current time, the alarm doesn't go off. Maybe the time is wrong?... Doesn't seem so because I have logged the time of the the system's next scheduled alarm and it reports back with the same time I set it for. Any thoughts?
EDIT: So I already have an android app that performs all this correctly. When I convert it to Xamarin and C#, it no longer works.
This is how I'm creating a local notification in my Xamarin app.
DateTime time = ... // whatever time
AlarmManager manager = (AlarmManager)context.GetSystemService(Context.AlarmService);
Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
calendar.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
calendar.Set(time.Year, time.Month - 1, time.Day, time.Hour, time.Minute, 0);
manager.SetRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis,
AlarmManager.IntervalDay, pendingIntent);
And here is the BroadcastReceiver class:
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
NotificationManager nManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
Intent repeatingIntent;
// Here I'm opening two different Activities based on condition
if (CommonUtils.isLoggedIn()))
{
repeatingIntent = new Intent(context, typeof(MainActivity));
repeatingIntent.PutExtra(MainActivity.SELECT_TAB, 1);
}
else
{
repeatingIntent = new Intent(context, typeof(SplashActivity));
}
repeatingIntent.SetFlags(ActivityFlags.ClearTop);
PendingIntent pIntent = PendingIntent.GetActivity(context, 100, repeatingIntent, PendingIntentFlags.UpdateCurrent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.SetContentIntent(pIntent)
.SetSmallIcon(Resource.Drawable.az_logo_small)
.SetColor(ContextCompat.GetColor(context, Resource.Color.PrimaryColor))
.SetContentTitle(CommonUtils.MAIN_TITLE)
.SetContentText(UIMessages.VITAL_REMINDER)
.SetAutoCancel(true);
nManager.Notify(100, builder.Build());
}
}
and in AndroidManifest.xml, you need this permission
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
also need the register the BroadcastReceiver in AndroidManifest.xml
<application android:label="YourAppName" android:largeHeap="true" android:icon="#drawable/ic_launcher">
<receiver android:name=".AlarmReceiver"></receiver>
</application>
Hope it helps.
First you have to Declare <receiver android:name=".AlarmReceiver"></receiver> in your Manifest inside yout app
And create a BroadcastReceiver class:
[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
//Miramos si la alarma debe repetirse
var repeat = intent.GetStringExtra("repeat");
//Hacemos sonar la alarma
Uri notification = RingtoneManager.GetDefaultUri(RingtoneType.Alarm);
Ringtone r = RingtoneManager.GetRingtone(context, notification);
r.Play();
//
}
The following code is to Start and cancel the pending intent
public void SetAlarm(long miliseconds)
{
AlarmManager alarmManager = (AlarmManager)this.Activity.GetSystemService(Context.AlarmService);
Intent intent = new Intent(this.Activity, typeof(AlarmReceiver));
intent.PutExtra("repeat", repeat);
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.Activity, /*id de la alarma que sea unico */0, intent, PendingIntentFlags.CancelCurrent);
alarmManager.Set(AlarmType.RtcWakeup, miliseconds, pendingIntent);
Toast toast = Toast.MakeText(this.Activity, Resource.String.set_alarm, ToastLength.Short);
toast.Show();
}
public void CancelAlarm()
{
AlarmManager alarmManager = (AlarmManager)this.Activity.GetSystemService(Context.AlarmService);
Intent intent = new Intent(this.Activity, typeof(AlarmReceiver));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this.Activity, /*a traves del anterior id ahora podemos pedir que se actualice */0, intent, PendingIntentFlags.UpdateCurrent);
//Con el pending intent actualizado podemos cancelarlo
pendingIntent.Cancel();
alarmManager.Cancel(pendingIntent);
Toast toast = Toast.MakeText(this.Activity, Resource.String.remove_alarm, ToastLength.Short);
toast.Show();
}
To finish you have to call them for example when clicking them
if (alarm.Active == true)
{
alarm.Active = false;
alarmsFragment.CancelAlarm();
}
else
{
alarm.Active = true;
// the time you want in milliseconds
long miliseconds = Java.Lang.JavaSystem.CurrentTimeMillis() + 10000 ;
alarmsFragment.SetAlarm(miliseconds);
}

Resources