如何在 xamarin 上正确接收 Intent Extras

目前,我正在将教程用于 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.cs


Intent.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 始终为空,请问我哪里出错了。


倚天杖
浏览 92回答 3
3回答

一只萌萌小番薯

也许答案迟了,但它可以帮助其他人。唯一的错误是您使用Intent代替intent(传递的参数)if (Intent.Extras != null)代替if (intent.Extras != null)我陷入了同样的分心。

芜湖不芜

请通过这个。 实施 FirebaseMessagingService - 前台通知您必须创建 FirebaseMessagingService,当您的应用程序处于前台时,您可以在其中接收 RemoteMessage。

开心每一天1111

实际上,根据文档,处理前台通知的正确方法是实现一个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 中的通知消息和数据消息
打开App,查看更多内容
随时随地看视频慕课网APP