xamarin forms alarm

public string Create(string title, string message, DateTime scheduleDate, Dictionary<string, string> extraInfo)
 {
 // Create the unique identifier for this notifications.
 var notificationId = Guid.NewGuid().ToString();
 
 
 // Create the alarm intent to be called when the alarm triggers. Make sure
 // to add the id so we can find it later if the user wants to update or
 // cancel.
 var alarmIntent = new Intent(Application.Context, typeof(NotificationAlarmHandler));
 alarmIntent.SetAction(BuildActionName(notificationId));
 alarmIntent.PutExtra(TitleExtrasKey, title);
 alarmIntent.PutExtra(MessageExtrasKey, message);
 
 
 // Add the alarm intent to the pending intent.
 var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
 
 
 // Figure out the alaram in milliseconds.
 var utcTime = TimeZoneInfo.ConvertTimeToUtc(scheduleDate);
 var epochDif = (new DateTime(1970, 1, 1) - DateTime.MinValue).TotalSeconds;
 var notifyTimeInInMilliseconds = utcTime.AddSeconds(-epochDif).Ticks / 10000;
 
 
 // Set the notification.
 var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;
 alarmManager?.Set(AlarmType.RtcWakeup, notifyTimeInInMilliseconds, pendingIntent);
 
 // All done.
 return notificationId;
 }

4.33
19

                                    public string Create(string title, string message, DateTime scheduleDate, Dictionary&lt;string, string&gt; extraInfo)
&nbsp;{
&nbsp;// Create the unique identifier for this notifications.
&nbsp;var notificationId = Guid.NewGuid().ToString();
&nbsp;
&nbsp;
&nbsp;// Create the alarm intent to be called when the alarm triggers. Make sure
&nbsp;// to add the id so we can find it later if the user wants to update or
&nbsp;// cancel.
&nbsp;var alarmIntent = new Intent(Application.Context, typeof(NotificationAlarmHandler));
&nbsp;alarmIntent.SetAction(BuildActionName(notificationId));
&nbsp;alarmIntent.PutExtra(TitleExtrasKey, title);
&nbsp;alarmIntent.PutExtra(MessageExtrasKey, message);
&nbsp;
&nbsp;
&nbsp;// Add the alarm intent to the pending intent.
&nbsp;var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
&nbsp;
&nbsp;
&nbsp;// Figure out the alaram in milliseconds.
&nbsp;var utcTime = TimeZoneInfo.ConvertTimeToUtc(scheduleDate);
&nbsp;var epochDif = (new DateTime(1970, 1, 1) - DateTime.MinValue).TotalSeconds;
&nbsp;var notifyTimeInInMilliseconds = utcTime.AddSeconds(-epochDif).Ticks / 10000;
&nbsp;
&nbsp;
&nbsp;// Set the notification.
&nbsp;var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;
&nbsp;alarmManager?.Set(AlarmType.RtcWakeup, notifyTimeInInMilliseconds, pendingIntent);
&nbsp;
&nbsp;// All done.
&nbsp;return notificationId;
&nbsp;}

4.33 (9 Votes)
0
4
7

                                    [BroadcastReceiver]
internal class NotificationAlarmHandler : BroadcastReceiver
{
&nbsp;&nbsp;public override void OnReceive(Context context, Intent intent)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;// Pull out the parameters from the alarm.
&nbsp;&nbsp;&nbsp;&nbsp;var message = intent.GetStringExtra(&quot;message&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;var title = intent.GetStringExtra(&quot;title&quot;);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// Create the notification.
&nbsp;&nbsp;&nbsp;&nbsp;var builder = new NotificationCompat.Builder(Application.Context)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetContentTitle(title)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetContentText(message)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetSmallIcon(Resource.Drawable.logo_square_22x22)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.SetAutoCancel(true);
&nbsp;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// Set this application to open when the notification is clicked. If the application
&nbsp;&nbsp;&nbsp;&nbsp;// is already open it will reuse the same activity.
&nbsp;&nbsp;&nbsp;&nbsp;var resultIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
&nbsp;&nbsp;&nbsp;&nbsp;resultIntent.SetAction(intent.Action);
&nbsp;&nbsp;&nbsp;&nbsp;resultIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;var resultPendingIntent = PendingIntent.GetActivity(Application.Context, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
&nbsp;&nbsp;&nbsp;&nbsp;builder.SetContentIntent(resultPendingIntent);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// Show the notification.
&nbsp;&nbsp;&nbsp;&nbsp;var notificationManager = NotificationManagerCompat.From(Application.Context);
&nbsp;&nbsp;&nbsp;&nbsp;notificationManager.Notify(0, builder.Build());
&nbsp;&nbsp;}
}

4 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source