【发布时间】:2021-12-11 14:18:07
【问题描述】:
我正在 Xamarin 中开发打印服务应用程序,此应用程序/服务通过发出 POST 请求与服务器对话。 服务器确实要求客户端通过将 http 401 响应发送回服务来输入他/她的凭据。收到 401 响应后,我将发送通知,用户必须点击通知以打开自定义弹出窗口并输入用户名/密码。
由于运行服务的限制,我使用广播接收器来发送通知,如下所示:
public class MyBroadcastReceiver : BroadcastReceiver
{
const string NOTIFICATION_ID = "1000";
Android.Content.Context _context;
Android.Content.Intent _intent;
NotificationCompat.Builder mBuilder;
public override void OnReceive(Context context, Intent intent)
{
//Toast.MakeText(context, "Received broadcast in MyBroadcastReceiver",
ToastLength.Long).Show();
this._context = context;
this._intent = intent;
if (intent.Action.Equals("MYNOTIFICATION"))
_ = CreateNotification("Authentication Required", "Tap to enter credentials");
}
private async Task CreateNotification(String title, String message)
{
await Task.Run( () =>
{
var intent = new Intent(_context, typeof(AuthActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra(title, message);
var pendingIntent = PendingIntent.GetActivity(_context, 0, intent, PendingIntentFlags.OneShot);
var sound = global::Android.Net.Uri.Parse(ContentResolver.SchemeAndroidResource + "://" + _context.PackageName + "/" + Resource.Raw.notification);
var alarmAttributes = new Android.Media.AudioAttributes.Builder()
.SetContentType(Android.Media.AudioContentType.Sonification)
.SetUsage(Android.Media.AudioUsageKind.Notification).Build();
mBuilder = new NotificationCompat.Builder(_context, NOTIFICATION_ID);
mBuilder.SetContentTitle(title)
.SetSound(sound)
.SetAutoCancel(true)
.SetContentText(message)
.SetChannelId(NOTIFICATION_ID)
.SetPriority((int)Android.App.NotificationPriority.High)
.SetVibrate(new long[0])
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate)
.SetVisibility((int)NotificationVisibility.Public)
.SetSmallIcon(Resource.Drawable.Icon)
.SetContentIntent(pendingIntent);
NotificationManager notificationManager = _context.GetSystemService(Context.NotificationService) as NotificationManager;
if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
{
NotificationImportance importance = global::Android.App.NotificationImportance.High;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_ID, title, importance);
notificationChannel.EnableLights(true);
notificationChannel.EnableVibration(true);
notificationChannel.SetSound(sound, alarmAttributes);
notificationChannel.SetShowBadge(true);
notificationChannel.Importance = NotificationImportance.High;
notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
if (notificationManager != null)
{
mBuilder.SetChannelId(NOTIFICATION_ID);
notificationManager.CreateNotificationChannel(notificationChannel);
}
}
notificationManager.Notify(0, mBuilder.Build());
});
}
如何处理通知的 Tap 事件以在不打开应用程序的情况下打开自定义弹出窗口。
【问题讨论】:
-
How can I handle the Tap event of the notification to open a custom popup without opening the App.由于您使用broadcastReceiver来实现您的功能,您不能单独使用broadcastReceiver,您必须将您的代码添加到您的应用程序中并启动您的应用程序。所以,你说的功能是不可能的。