gpt4 book ai didi

Xamarin Forms - 处理通知点击

转载 作者:行者123 更新时间:2023-12-03 18:28:17 25 4
gpt4 key购买 nike

我有一个 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 :

LaunchMode.SingleTop

[Activity(~~~, LaunchMode = LaunchMode.SingleTop, ~~~]
public class MainActivity
{
~~~~

在您的 MainActivity (FormsAppCompatActivity 子类)添加一个 OnNewIntent覆盖:

OnNewIntent:
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
NotificationClickedOn(intent);
}

现在您可以查看 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();
}
}

发送通知示例:
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();
}
}

关于Xamarin Forms - 处理通知点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com