Reputation: 257
I am making a reminder app with Xamarin. I am trying to send notifications, however every time I run the app I can only push one notification no matter how many reminders the user creates.
Here is my notification code:
using Android.App;
using Android.Content;
using Newtonsoft.Json;
using ReminderApp.Models;
using ReminderApp.HelperRepository;
using Android.OS;
using System;
using static Android.Media.Audiofx.BassBoost;
using Android.Media;
namespace ReminderApp.Notifications
{
[Activity(Label = "ReminderApp")]
public class ReminderNotifications : Activity
{
Reminder reminder;
public ReminderNotifications()
{
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
string CHANNEL_ID = "dan1414";
int NOTIFY_ID = 0 + new Random().Next(1, 30);
var channel = new NotificationChannel(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
{
Description = "Firebase Cloud Messages appear in this channel"
};
string date = Intent.GetStringExtra("date");
string time = Intent.GetStringExtra("time");
reminder = ReminderHelper.SelectReminderByDateAndTime(this, date, time);
if (reminder != null)
{
Intent newIntent = new Intent(this, typeof(ReminderContent));
newIntent.PutExtra("reminder", JsonConvert.SerializeObject(reminder));
Android.Support.V4.App.TaskStackBuilder stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(this);
stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(ReminderContent)));
stackBuilder.AddNextIntent(newIntent);
PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
.SetAutoCancel(true)
.SetContentIntent(resultPendingIntent)
.SetContentTitle("Reminder!!")
.SetSmallIcon(Resource.Drawable.Screenshot_2020_11_11_at_4_57_02_PM)
.SetContentText("Click for details..");
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(NOTIFY_ID, builder.Build());
}
SetContentView(Resource.Layout.NotificationAlert);
}
}
}
I see nothing wrong with my code and I have tried many different things, but nothing works.
here is my full code, in case it helps: https://github.com/CrazyDanyal1414/ReminderApp
Any help appreciated!
Upvotes: 0
Views: 69
Reputation: 10356
On ScheduleReminder
method, please modify the PendingIntent second parameter like this:
PendingIntent.GetActivity(this, new Random().Next(), myIntent, 0);
making sure the unique value for PendingIntent second parameter, otherwise the new alarm will be overwrittethe old alarm.
public void ScheduleReminder(Reminder reminder)
{
AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(this, typeof(ReminderNotifications));
myIntent.PutExtra("date", reminder.Date);
myIntent.PutExtra("time", reminder.Time);
var t = reminder.Time.Split(':');
var ampm = t[1].Split(' ')[1];
var hrr = Convert.ToDouble(t[0]);
var min = Convert.ToDouble(t[1].Split(' ')[0]);
string dateString = Convert.ToString(reminder.Date + " " + hrr + ":" + min + ":00 " + ampm);
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dateString);
var millisec = dateOffsetValue.ToUnixTimeMilliseconds();
PendingIntent.GetActivity(this, new Random().Next(), myIntent, 0);
manager.Set(AlarmType.RtcWakeup, millisec, pendingIntent);
}
Upvotes: 1