为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 xamarin 上正确接收 Intent Extras

如何在 xamarin 上正确接收 Intent Extras

C#
倚天杖 2022-12-31 12:54:14
目前,我正在将本教程用于 xamarin 推送通知。它就像一个魅力。我现在的目标是正确处理意图。在我的 firebase 消息传递服务中,我创建了一个通知并添加了附加信息。private void CreateNotification(Object e){    try    {        string title = "";        string body = "";        var intent = new Intent(this, typeof(MainActivity));        var i = e as Intent;        var bundle = i.Extras;        title = bundle.GetString("gcm.notification.title");        body = bundle.GetString("gcm.notification.body");        intent.PutExtra("view", "test");        intent.PutExtra("title", title);        intent.PutExtra("body", body);        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);        Notification.Builder builder = new Notification.Builder(this);        builder.SetSmallIcon(Resource.Drawable.icon_notification);        builder.SetContentIntent(pendingIntent);        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon_notification));        builder.SetContentTitle("Test");        builder.SetContentText(body);        builder.SetDefaults(NotificationDefaults.Sound);        builder.SetAutoCancel(true);        NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);        notificationManager.Notify(1, builder.Build());    }    catch (Exception ex)    {        Console.WriteLine(ex.Message);    }}目前后台推送通知处理得很好。我能够在其中的OnCreate方法中运行一条简单的线MainActivity.csIntent.GetStringExtra("view");这让我得到了在 CreateNotification 中设置的值。我遇到的问题是试图将意图放在前台。此代码驻留在 MainActivity.cs 上,当在前台单击推送通知时触发。protected async override void OnNewIntent(Intent intent){    base.OnNewIntent(intent);    if (Intent.Extras != null)    {        foreach (var key in Intent.Extras.KeySet())        {            var value = Intent.Extras.GetString(key);            Log.Debug(TAG, "Key: {0} Value: {1}", key, value);        }    } }Intent.Extras 始终为空,请问我哪里出错了。
查看完整描述

3 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

也许答案迟了,但它可以帮助其他人。

唯一的错误是您使用Intent代替intent(传递的参数)

if (Intent.Extras != null)

代替

if (intent.Extras != null)

我陷入了同样的分心。


查看完整回答
反对 回复 2022-12-31
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

请通过这个。 实施 FirebaseMessagingService - 前台通知

您必须创建 FirebaseMessagingService,当您的应用程序处于前台时,您可以在其中接收 RemoteMessage。


查看完整回答
反对 回复 2022-12-31
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

实际上,根据文档,处理前台通知的正确方法是实现一个FirebaseMessagingService


这里有更好的解释


您需要创建一个类似这样的服务类:


    [Service]

    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]

    public class MyFirebaseMessagingService : FirebaseMessagingService

    {

       // private string TAG = "MyFirebaseMsgService";

          public override void OnMessageReceived(RemoteMessage message)

       {

           base.OnMessageReceived(message);

           string messageFrom = message.From;

           string getMessageBody = message.GetNotification().Body;

           SendNotification(message.GetNotification().Body);

       }

    void SendNotification(string messageBody)

    {

        try

        {

            var intent = new Intent(this, typeof(MainActivity));

            intent.AddFlags(ActivityFlags.ClearTop);

            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);


            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

                .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)

                .SetContentTitle("Title")

                .SetContentText(messageBody)

                .SetAutoCancel(true)

                .SetContentIntent(pendingIntent);


            NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);

            notificationManager.Notify(0, notificationBuilder.Build());

        }

        catch (Exception ex)

        {


        }

       }

     }

有关更多信息,您可以查看我的 SO 问题


如何处理 Firebase 通知,即 Android 中的通知消息和数据消息


查看完整回答
反对 回复 2022-12-31
  • 3 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信