【问题标题】:PutExtra Value not being passed to mainactivity from push notification using xamarin forms and android使用 xamarin 表单和 android 的推送通知未将 PutExtra 值传递给 mainactivity
【发布时间】:2017-04-29 04:21:08
【问题描述】:

我目前的目标是在单击成功接收的推送通知后将应用程序打开到特定页面。一旦我掌握了基础知识,我就会发送一个 id。由于某种原因,在点击通知后,id 没有被传递回 mainactivity。

BroadcastReciever 类发送推送通知

 var uiIntent = new Intent(this, typeof(MainActivity));
 uiIntent.PutExtra("param", "54");
 var pendingIntent = PendingIntent.GetActivity(this, 0, uiIntent, 0);

主要活动 - onCreate

        string parameterValue = this.Intent.GetStringExtra("param");
        if (parameterValue != null)
        {
            LoadApplication(new App(parameterValue));
        }
        else
        {
            LoadApplication(new App(null));
        }

似乎很简单,但我的参数值总是为空,谢谢

更新

 private void CreateNotification(string title, string desc)
    {
        //Create notification
        var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        Intent startupIntent = new Intent(this, typeof(MainActivity));
        startupIntent.PutExtra("param", "54");
        Android.App.TaskStackBuilder stackBuilder = Android.App.TaskStackBuilder.Create(this);
        stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
        stackBuilder.AddNextIntent(startupIntent);
        const int pendingIntentId = 0;
        PendingIntent pendingIntent =
                       stackBuilder.GetPendingIntent(pendingIntentId, PendingIntentFlags.OneShot);

      //  var pendingIntent = PendingIntent.GetActivity(this, 0, startupIntent, 0);


        ////Create an intent to show ui
        //var uiIntent = new Intent(this, typeof(MainActivity));
        //uiIntent.PutExtra("param", "54");
        //var pendingIntent = PendingIntent.GetActivity(this, 0, uiIntent, 0);


        //Create the notification using Notification.Builder
        //Use Android Compatibility Apis

        var notification = new NotificationCompat.Builder(this).SetContentTitle(title)
            .SetSmallIcon(Android.Resource.Drawable.SymActionEmail)
            //we use the pending intent, passing our ui intent over which will get called
            //when the notification is tapped.
            .SetContentIntent(pendingIntent)
            .SetContentText(desc)
            .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))

             //Auto cancel will remove the notification once the user touches it
             .SetAutoCancel(true).
            Build();


        //Show the notification
        if (notificationManager != null)
        {
            notificationManager.Notify(1, notification);
        }
    }

使用不同方法的完整方法,但值仍然没有传递给 mainactivity。总是有一个空值?

【问题讨论】:

  • string parameterValue = this.Intent.GetStringExtra("param");onResume() 生命周期函数中试试这个函数。

标签: android xamarin.forms android-notifications main-activity


【解决方案1】:

这就是它对我有用的原因。

最初是 RegisterInGcm();高于 parameterValue 但将其移至下方使其工作。

//RegisterInGcm(); Wrong

        string parameterValue = this.Intent.GetStringExtra("param");
        if (parameterValue != null)
        {
             LoadApplication(new App(parameterValue));
        }
        else
        {
            LoadApplication(new App(null));
        }

        RegisterInGcm();

疯狂的简单改变,许多测试要做!

【讨论】:

    【解决方案2】:

    PendingIntentFlags.UpdateCurrent 应该可以工作。请在PendingIntent does not send Intent extras查看更多信息

    PendingIntent pending = PendingIntent.GetActivity(context, 0, mailDetail, PendingIntentFlags.UpdateCurrent);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 2019-12-06
      • 2018-03-01
      相关资源
      最近更新 更多