只能在 Xamarin 上推送一次通知 android
Can only push notification once on Xamarin android
我正在使用 Xamarin 制作一个提醒应用程序。我正在尝试发送通知,但是每次我 运行 该应用程序时,无论用户创建了多少提醒,我都只能推送一个通知。
这是我的通知代码:
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);
}
}
}
我没有发现我的代码有什么问题,我尝试了很多不同的方法,但没有任何效果。
这是我的完整代码,以防有帮助:https://github.com/CrazyDanyal1414/ReminderApp
感谢任何帮助!
在ScheduleReminder
方法中,请修改PendingIntent第二个参数如下:
PendingIntent.GetActivity(this, new Random().Next(), myIntent, 0);
确保 PendingIntent 第二个参数的值是唯一的,否则新警报将覆盖旧警报。
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);
}
我正在使用 Xamarin 制作一个提醒应用程序。我正在尝试发送通知,但是每次我 运行 该应用程序时,无论用户创建了多少提醒,我都只能推送一个通知。
这是我的通知代码:
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);
}
}
}
我没有发现我的代码有什么问题,我尝试了很多不同的方法,但没有任何效果。
这是我的完整代码,以防有帮助:https://github.com/CrazyDanyal1414/ReminderApp
感谢任何帮助!
在ScheduleReminder
方法中,请修改PendingIntent第二个参数如下:
PendingIntent.GetActivity(this, new Random().Next(), myIntent, 0);
确保 PendingIntent 第二个参数的值是唯一的,否则新警报将覆盖旧警报。
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);
}