1

Im currently aiming to open the app to a particiualr page after the click of a push notification that successfully recieves. Il be sending an id once i get the basics working. For some reason the id is not being passed back to the mainactivity after the notification has been tapped.

BroadcastReciever class sending the push notification

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

main activity - onCreate

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

seems to be very simple but my parameter value is always null any thoughts thanks

UPDATE

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

the full method with a different approach but still the value does not get passed to the mainactivity. there is always a null?

Costas Aletrari
  • 387
  • 5
  • 22
  • `string parameterValue = this.Intent.GetStringExtra("param");` try this function in the `onResume()` lifecycle function. – Mike Ma Dec 15 '16 at 09:55

2 Answers2

0

This is what made it work for me.

initially the RegisterInGcm(); was above parameterValue but moving it below made it work.

//RegisterInGcm(); Wrong

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

        RegisterInGcm();

crazy simple change, many tests to do!!

Costas Aletrari
  • 387
  • 5
  • 22
0

PendingIntentFlags.UpdateCurrent should work. Please see more at PendingIntent does not send Intent extras

PendingIntent pending = PendingIntent.GetActivity(context, 0, mailDetail, PendingIntentFlags.UpdateCurrent);
CodeSi
  • 401
  • 4
  • 6