c# - 如何在 xamarin 上正确接收 Intent Extras

标签 c# xamarin firebase-cloud-messaging

目前我正在使用 this xamarin 推送通知教程。它就像一个魅力。我现在的目标是正确处理意图。

在我的 firebase 消息传递服务中,我创建了一个通知和 Put extras。

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

最佳答案

也许答案为时已晚,但它可以帮助其他人。
唯一的错误是您使用 Intent 代替了 Intent(传递的参数)

if (Intent.Extras != null)
代替
if (intent.Extras != null)
我陷入了同样的分心。

关于c# - 如何在 xamarin 上正确接收 Intent Extras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870102/

相关文章:

c# - .NET4.5下使用ArrayList进行转换

c# - 始终使用 Run Tag 的 TextBlock 样式

android - Xamarin Forms - 媒体插件 - 手机上的空缩略图

xamarin - 如何在加载页面时阻止触发开关上的切换事件?

android - FCM 推送通知不会在屏幕顶部的通知上显示头像

ios - FCM/APNS 集成对于新 token 可以正常工作,但在转换旧 APNS token 时就不行了?

c# - 您如何维护多个版本的数据库?

c# - 从控制台应用程序动态调用 Web 服务

xamarin - MVVM Xamarin Forms,最适合在等待服务调用几分钟响应时将函数保留在内存中

ios - 如何通过 FCM(Firebase) 或 native 在 iOS 10 中发送推送通知?