Значение PutExtra не передается в основную активность из push-уведомления с использованием форм xamarin и android

В настоящее время я стремлюсь открыть приложение на определенной странице после нажатия на push-уведомление, которое успешно получено. Я отправлю идентификатор, как только заработаю основы. По какой-то причине идентификатор не передается обратно в основную активность после нажатия на уведомление.

Класс BroadcastReciever, отправляющий push-уведомление

 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);
        }
    }

полный метод с другим подходом, но значение все равно не передается в основную активность. всегда есть ноль?


person Costas Aletrari    schedule 13.12.2016    source источник
comment
string parameterValue = this.Intent.GetStringExtra("param"); попробуйте эту функцию в функции onResume() жизненного цикла.   -  person Mike Ma    schedule 15.12.2016


Ответы (2)


Это то, что заставило это работать для меня.

изначально RegisterInGcm(); был выше параметраValue, но его перемещение ниже заставило его работать.

//RegisterInGcm(); Wrong

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

        RegisterInGcm();

сумасшедшее простое изменение, много тестов!

person Costas Aletrari    schedule 15.12.2016

PendingIntentFlags.UpdateCurrent должен работать. Дополнительные сведения см. на странице PendingIntent не отправляет дополнительные сведения о намерениях.

PendingIntent pending = PendingIntent.GetActivity(context, 0, mailDetail, PendingIntentFlags.UpdateCurrent);
person CodeSi    schedule 05.01.2018