![](/img/trans.png)
[英]Android push notification from xamarin.forms button click hangs
[英]Notification click in Android(Xamarin Forms)
我使用了Xamarin Form。 我有一个带有通知的应用程序,如果我单击它们,通知会打开某些活动。 我想要的是,如果我单击通知并且活动已经打开,则不会再次开始,而只是放在最前面。 这是我的通知代码:
public void Show(string title, string body, int id, DateTime notifyTime)
{
var intent = CreateIntent(id);
var localNotification = new LocalNotification();
localNotification.Title = title;
localNotification.Body = body;
localNotification.Id = id;
localNotification.NotifyTime = notifyTime;
if (NotificationIconId != 0)
{
localNotification.IconId = NotificationIconId;
}
else
{
localNotification.IconId = Resource.Drawable.Icon24;
}
var serializedNotification = SerializeNotification(localNotification);
intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);
var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);
var triggerTime = NotifyTimeInMilliseconds(localNotification.NotifyTime);
var alarmManager = GetAlarmManager();
alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent);
}
它是ScheduledAlarmHandler中的代码:
[BroadcastReceiver(Enabled = true)]
public class ScheduledAlarmHandler : BroadcastReceiver
{
...........
public override void OnReceive(Context context, Intent intent)
{
var extra = intent.GetStringExtra(LocalNotificationKey);
var notification = DeserializeNotification(extra);
var builder = new NotificationCompat.Builder(Application.Context)
.SetContentTitle(notification.Title)
.SetContentText(notification.Body)
.SetSmallIcon(notification.IconId)
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
.SetAutoCancel(true);
var resultIntent = LocalNotificationsService.GetLauncherActivity();
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context);
stackBuilder.AddNextIntent(resultIntent);
var resultPendingIntent =
stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
builder.SetContentIntent(resultPendingIntent);
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Notify(notification.Id, builder.Build());
}
....
}
这是对我的MainActivity的贡献
[Activity (Icon = "@drawable/icon",
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode.SingleTop)]
对我来说这是工作:
这是对我的MainActivity的贡献:
[Activity (Icon = "@drawable/icon", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode.SingleTop)]
[IntentFilter(new[] { "test.test.alarm" }, Categories = new[] { Intent.CategoryDefault })]
这是LocalNotificationsService
public class LocalNotificationsService
{
.....
public void Show(string title, string body, int id, DateTime notifyTime)
{
var intent = CreateIntent(id);
var localNotification = new LocalNotification();
localNotification.Title = title;
localNotification.Body = body;
localNotification.Id = id;
localNotification.NotifyTime = notifyTime;
if (NotificationIconId != 0)
{
localNotification.IconId = NotificationIconId;
}
else
{
localNotification.IconId = Resource.Drawable.Icon24;
}
var serializedNotification = SerializeNotification(localNotification);
intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);
var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);
var triggerTime = NotifyTimeInMilliseconds(localNotification.NotifyTime);
var alarmManager = GetAlarmManager();
alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent);
}
public static PendingIntent GetDialogPendingIntent()
{
return PendingIntent.GetActivity(
Forms.Context,
0,
new Intent("test.test.alarm")
.AddFlags(ActivityFlags.NewTask),
0);
}
private AlarmManager GetAlarmManager()
{
var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager;
return alarmManager;
}
}
这是ScheduledAlarmHandler
[BroadcastReceiver(Enabled = true, Label = "Local Notifications Plugin Broadcast Receiver")]
public class ScheduledAlarmHandler : BroadcastReceiver
{
public const string LocalNotificationKey = "LocalNotification";
public override void OnReceive(Context context, Intent intent)
{
var extra = intent.GetStringExtra(LocalNotificationKey);
var notification = DeserializeNotification(extra);
var currPath = AppSettings.Data.NotificationSoundUri;
var uri = string.IsNullOrEmpty(currPath) ? RingtoneManager.GetDefaultUri(RingtoneType.Alarm) : Uri.Parse(currPath);
var builder = new NotificationCompat.Builder(Application.Context)
.SetContentTitle(notification.Title)
.SetContentText(notification.Body)
.SetSmallIcon(notification.IconId)
.SetSound(uri)
.SetAutoCancel(true);
var resultPendingIntent = LocalNotificationsService.GetDialogPendingIntent();
builder.SetContentIntent(resultPendingIntent);
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Notify(notification.Id, builder.Build());
}
private LocalNotification DeserializeNotification(string notificationString)
{
var xmlSerializer = new XmlSerializer(typeof(LocalNotification));
using (var stringReader = new StringReader(notificationString))
{
var notification = (LocalNotification)xmlSerializer.Deserialize(stringReader);
return notification;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.