Nic*_*nes 6 notifications local xamarin.android xamarin xamarin.forms
我有一个 Xamarin Forms 应用程序,它会引发 Android 通知,但我无法创建一个简单的页面,当用户单击通知时,该页面将与用户进行交互。我知道在 Xamarin.Forms 中只有 1 个活动,因此挂起的 Intent 必须是该 mainActivity
我已将 LaunchMode 设置为 SingleTop 和 Intent Filter 以匹配 pendingIntent 中使用的 Intent 名称
现在,当我单击通知时,我确实被路由到了 MainActivity 的 OnResume,但我不明白如何:1) 由于通知单击而认识到我在此活动中 - 我尝试向待处理的 Intent 添加一个 Extra,但是当我检查 this.Intent.Extras 时它不在那里 2) 即使我知道我由于点击通知而在活动中,我如何从活动中启动特定页面。我是 Xamarin 的新手,但看不到如何导航到内容页面或访问导航堆栈。
这一定是一个非常常见的用例,但我找不到任何相关的内容。
确保您已LaunchMode.SingleTop在您的MainActivity:
[Activity(~~~, LaunchMode = LaunchMode.SingleTop, ~~~]
public class MainActivity
{
~~~~
Run Code Online (Sandbox Code Playgroud)
在您的MainActivity(FormsAppCompatActivity 子类)中添加一个OnNewIntent覆盖:
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
NotificationClickedOn(intent);
}
Run Code Online (Sandbox Code Playgroud)
现在您可以检查intent.Action/intent.HasExtra以确定发送的通知是否是您的通知,从而对其进行处理。凭借Xamarin.Forms最简单的是使用MessagingCenter发那就是你的.NetStd / PCL内订阅消息Xamarin.Forms的代码库。
void NotificationClickedOn(Intent intent)
{
if (intent.Action == "ASushiNotification" && intent.HasExtra("MessageFromSushiHangover"))
{
/// Do something now that you know the user clicked on the notification...
var notificationMessage = intent.Extras.GetString("MessageFromSushiHangover");
var winnerToast = Toast.MakeText(this, $"{notificationMessage}.\n\n Please send 2 BitCoins to SushiHangover to process your winning ticket! ", ToastLength.Long);
winnerToast.SetGravity(Android.Views.GravityFlags.Center, 0, 0);
winnerToast.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
void SendNotifacation()
{
var title = "Winner, Winner, Chicken Dinner";
var message = "You just won a million StackOverflow reputation points";
var intent = new Intent(BaseContext, typeof(MainActivity));
intent.SetAction("ASushiNotification");
intent.PutExtra("MessageFromSushiHangover", message);
var pending = PendingIntent.GetActivity(BaseContext, 0, intent, PendingIntentFlags.CancelCurrent);
using (var notificationManager = NotificationManager.FromContext(BaseContext))
{
Notification notification;
if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
{
#pragma warning disable CS0618 // Type or member is obsolete
notification = new Notification.Builder(BaseContext)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetDefaults(NotificationDefaults.All)
.SetContentIntent(pending)
.Build();
#pragma warning restore CS0618 // Type or member is obsolete
}
else
{
var myUrgentChannel = BaseContext.PackageName;
const string channelName = "Messages from SushiHangover";
NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(myUrgentChannel);
if (channel == null)
{
channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
channel.EnableVibration(true);
channel.EnableLights(true);
channel.SetSound(
RingtoneManager.GetDefaultUri(RingtoneType.Notification),
new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel?.Dispose();
notification = new Notification.Builder(BaseContext)
.SetChannelId(myUrgentChannel)
.SetContentTitle(title)
.SetContentText(message)
.SetAutoCancel(true)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentIntent(pending)
.Build();
}
notificationManager.Notify(1331, notification);
notification.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3402 次 |
| 最近记录: |