我想在通知(存档)的按钮上添加操作,就像有人点击它一样,它会显示一个Toast消息,下面是一个示例图片:
var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent);
这是我的通知构建器代码:
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle(title)
.SetStyle(style).SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.AddAction(Resource.Drawable.tick_notify, "ARCHIVE", contentIntent)
.AddAction(Resource.Drawable.cancel_notify, "REPLY", pIntent)
.SetAutoCancel(true);
//check bellow
builder.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate));
所以我通常想制作一个按钮来制作通知,而不是启动一个Activity等。
请帮助我,我是Xamarin.android的新手。
编辑:
这是我的完整代码,根据您提供给我的答案,但我仍然得到例外:
using System;
using Android.App;
using Android.Content;
using Android.Media;
using Android.Support.V4.App;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using Android.Widget;
namespace Diabetes.Droid
{
[BroadcastReceiver]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
//Show toast here
Toast.MakeText(context, "Hello it's me ", ToastLength.Short).Show();
var extras = intent.Extras;
if (extras != null && !extras.IsEmpty)
{
NotificationManager manager_ = context.GetSystemService(Context.NotificationService) as NotificationManager;
var notificationId = extras.GetInt("NotificationIdKey", -1);
if (notificationId != -1)
{
manager_.Cancel(notificationId);
}
}
AlarmReceiver customReceiver = new AlarmReceiver();
//Create intent for action 1 (TAKE)
var actionIntent1 = new Intent();
actionIntent1.SetAction("ARCHIVE");
var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent);
//Create intent for action 2 (REPLY)
var actionIntent2 = new Intent();
actionIntent2.SetAction("REPLY");
var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent);
Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
/*
var resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);*/
var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent);
var pending = PendingIntent.GetActivity(context, 0,
resultIntent,
PendingIntentFlags.CancelCurrent);
// Instantiate the Big Text style:
Notification.BigTextStyle textStyle = new Notification.BigTextStyle();
var builder = new Notification.Builder(context)
.SetContentTitle("Diabetics Reminder")
.SetDefaults(NotificationDefaults.Sound)
.AddAction(Resource.Drawable.tick_notify, "REPLY", pIntent1)
.AddAction(Resource.Drawable.cancel_notify, "ARCHIVE", pIntent2)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetStyle(new Notification
.BigTextStyle()
.SetSummaryText("")
.SetBigContentTitle(title)
.BigText(message))
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
//Add intent filters for each action and register them on a broadcast receiver
var intentFilter = new IntentFilter();
intentFilter.AddAction("ARCHIVE");
intentFilter.AddAction("REPLY");
context.RegisterReceiver(customReceiver, intentFilter);
var manager = NotificationManager.FromContext(context);
manager.Notify(10010, notification);
}
}
}
这是错误图片:
答案 0 :(得分:1)
单击该操作时,您需要BroadcastReceiver
才能显示Toast
。
对于每个操作,您需要一个Intent
来触发BroadcastReceiver Intent。这是一个如何看起来的例子:
自定义BroadcastReceiver
[BroadcastReceiver]
public class CustomActionReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Show toast here
Toast.MakeText(context, intent.Action, ToastLength.Short).Show();
var extras = intent.Extras;
if (extras != null && !extras.IsEmpty)
{
NotificationManager manager = context.GetSystemService(Context.NotificationService) as NotificationManager;
var notificationId = extras.GetInt("NotificationIdKey", -1);
if (notificationId != -1)
{
manager.Cancel(notificationId);
}
}
}
}
构建通知
var customReceiver = new CustomActionReceiver();
// Create intent for action 1 (ARCHIVE)
var actionIntent1 = new Intent();
actionIntent1.SetAction("ARCHIVE");
var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent);
// Create intent for action 2 (REPLY)
var actionIntent2 = new Intent();
actionIntent2.SetAction("REPLY");
var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent);
Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent);
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent).SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentTitle(title)
.SetStyle(style)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.AddAction(Resource.Drawable.tick_notify, "ARCHIVE", pIntent1)
.AddAction(Resource.Drawable.cancel_notify, "REPLY", pIntent2)
.SetAutoCancel(true);
builder.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate));
// Add intent filters for each action and register them on a broadcast receiver
var intentFilter = new IntentFilter();
intentFilter.AddAction("ARCHIVE");
intentFilter.AddAction("REPLY");
context.RegisterReceiver(customReceiver, intentFilter);
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify(0, builder.Build());
答案 1 :(得分:1)
您无法在其他广播接收器中注册广播接收器意图。试试这个:
using System;
using Android.App;
using Android.Content;
using Android.Media;
using Android.Support.V4.App;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using Android.Widget;
namespace Diabetes.Droid
{
[BroadcastReceiver]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.LowPriority)]
public class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
// Show toast here
Toast.MakeText(context, "Hello it's me ", ToastLength.Short).Show();
var extras = intent.Extras;
if (extras != null && !extras.IsEmpty)
{
var manager_ = context.GetSystemService(Context.NotificationService) as NotificationManager;
var notificationId = extras.GetInt("NotificationIdKey", -1);
if (notificationId != -1)
{
manager_.Cancel(notificationId);
}
}
// Create intent for action 1 (ARCHIVE)
var actionIntent1 = new Intent();
actionIntent1.SetAction("ARCHIVE");
var pIntent1 = PendingIntent.GetBroadcast(context, 0, actionIntent1, PendingIntentFlags.CancelCurrent);
// Create intent for action 2 (REPLY)
var actionIntent2 = new Intent();
actionIntent2.SetAction("REPLY");
var pIntent2 = PendingIntent.GetBroadcast(context, 0, actionIntent2, PendingIntentFlags.CancelCurrent);
Intent resultIntent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
// var resultIntent = new Intent(context, typeof(MainActivity));
// resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var contentIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent);
var pending = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.CancelCurrent);
// Instantiate the Big Text style:
Notification.BigTextStyle textStyle = new Notification.BigTextStyle();
var builder = new Notification.Builder(context).SetContentTitle("Diabetics Reminder")
.SetDefaults(NotificationDefaults.Sound)
.AddAction(Resource.Drawable.tick_notify, "REPLY", pIntent1)
.AddAction(Resource.Drawable.cancel_notify, "ARCHIVE", pIntent2)
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetStyle(new Notification
.BigTextStyle()
.SetSummaryText("")
.SetBigContentTitle(title)
.BigText(message))
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(10010, notification);
}
}
[BroadcastReceiver]
[IntentFilter(new string[] { "ARCHIVE" , "REPLY" })]
public class CustomActionReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Show toast here
Toast.MakeText(context, intent.Action, ToastLength.Short).Show();
var extras = intent.Extras;
if (extras != null && !extras.IsEmpty)
{
var manager = context.GetSystemService(Context.NotificationService) as NotificationManager;
var notificationId = extras.GetInt("NotificationIdKey", -1);
if (notificationId != -1)
{
manager.Cancel(notificationId);
}
}
}
}
}