我正在Xamarin开发一个打印服务应用程序,这个应用程序/服务通过POST请求与服务器对话。服务器通过向服务发送http 401响应来要求客户机输入他/她的凭据。收到401响应后,我将发送通知,用户必须点击通知以打开自定义弹出窗口并输入用户名/密码。
由于运行服务的限制,我使用broadcastReceiver来发送我的通知,像这样:
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());
});
}
在未打开App的情况下,如何处理通知打开自定义弹出窗口的点击事件?
我终于明白了。这是不可能得到任何类型的页面/弹出与应用程序关闭,我需要做的是从我的通知点击事件我打开一个透明的背景活动。
我用了这个样式
<style name="PopupTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowFullscreen">true</item>
在AndroidManifest.xml中我引用了它,如下所示:
android:theme="@style/PopupTheme"
现在,在mainActivity.cs中,我使用intent在透明主题和主基主题之间切换。
希望这能帮助到需要帮助的人。