本文介绍了Xamarin Android从通知中取消任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种方法,可以在每个TimeSpan间隔向用户发送一次本地通知:
I have a method that will send a user a local notification once per TimeSpan interval:
public async void GetVehiclePositionRepeatAsync(TimeSpan interval, CancellationToken cancellationToken)
{
while (true)
{
var task = new Task(() =>
{
cancellationToken.ThrowIfCancellationRequested();
var not = new PushNotificationGenerator(this, "Hooray!", "yay", "STOP_NOTIFICATIONS");
not.Push();
});
task.Start();
await Task.Delay(interval, cancellationToken);
}
}
我要拍摄的是在通知上有两个选项;将关闭当前通知,但允许此任务继续运行.另一个将取消此任务.这是PushNotificationGenerator类:
What I'm shooting for is to have two options on the notification; one will dismiss the current notification, but allow this Task to continue running. The other will cancel this task. Here's the PushNotificationGenerator class:
public class PushNotificationGenerator
{
public MapActivity Activity { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public string ChannelId { get; set; }
public PushNotificationGenerator(MapActivity act, string title, string txt, string channelId)
{
Activity = act;
Title = title;
Text = txt;
ChannelId = channelId;
}
public void Push()
{
var resultIntent = new Intent();
resultIntent.SetAction("Dismiss");
var stackBuilder = TaskStackBuilder.Create(Activity);
stackBuilder.AddParentStack(Class.FromType(typeof(MapActivity)));
stackBuilder.AddNextIntent(resultIntent);
var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);
var builder = new NotificationCompat.Builder(Activity, ChannelId)
.SetAutoCancel(true)
.SetContentTitle(Title)
.SetSmallIcon(Resource.Drawable.stop_bus)
.SetContentText(Text)
.AddAction(0, "Dont Dismiss", resultPendingIntent)
.AddAction(0, "Dismiss", resultPendingIntent);
var notificationManager = NotificationManagerCompat.From(Activity);
notificationManager.Notify(41144, builder.Build());
}
}
我不知道如何使用通知中的CancellationToken.
I'm at a loss of what to do to use the CancellationToken from the notification.
推荐答案
首先使用Push
方法:
public void Push()
{
...
Intent intentDismiss = new Intent(this, typeof(NotivicationBroadCast));
intentDismiss.SetAction("notification_dismiss");
PendingIntent pendingIntentDismiss = PendingIntent.GetBroadcast(this, 0,
intentDismiss, PendingIntentFlags.UpdateCurrent);
Intent intentCancel = new Intent(this, typeof(NotivicationBroadCast));
intentCancel.SetAction("notification_cancel");
PendingIntent pendingIntentCancel = PendingIntent.GetBroadcast(this, 0,
intentCancel, PendingIntentFlags.UpdateCurrent);
var builder = new NotificationCompat.Builder(Activity, ChannelId)
.SetAutoCancel(true)
.SetContentTitle(Title)
.SetSmallIcon(Resource.Drawable.stop_bus)
.SetContentText(Text)
.AddAction(0, "Dont Dismiss", pendingIntentDismiss)
.AddAction(0, "Dismiss", pendingIntentCancel );
var notificationManager = NotificationManagerCompat.From(Activity);
notificationManager.Notify(41144, builder.Build());
}
在GetVehiclePositionRepeatAsync
方法中:
public async void GetVehiclePositionRepeatAsync(TimeSpan interval, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested) {
var task = new Task(() =>
{
cancellationToken.Token.ThrowIfCancellationRequested();
var not = new PushNotificationGenerator(this, "Hooray!", "yay", "STOP_NOTIFICATIONS");
not.Push();
},cancellationToken.Token);
task.Start();
await Task.Delay(interval);
}
}
}
然后自定义NotivicationBroadCast
:
[BroadcastReceiver]
class NotivicationBroadCast : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;
if (action.Equals("notification_dismiss"))
{
NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Cancel(notificationId);
}
if (action.Equals("notification_cancel"))
{
cancellationToken.Cancel();
}
}
}
不要忘记激活注册接收器:
don't forget regist receiver in activiy:
protected override void OnResume()
{
base.OnResume();
NotivicationBroadCast recieve = new NotivicationBroadCast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.AddAction("notification_dismiss");
intentFilter.AddAction("notification_cancel");
RegisterReceiver(recieve, intentFilter);
begin();
}
这篇关于Xamarin Android从通知中取消任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!