Forum Xamarin.Forms

Android platform; passing data from multiple notifications to MainActivity

GerryHGerryH USMember ✭✭✭
edited March 2017 in Xamarin.Forms

My program may have multiple notifications for the user, each with a unique ID. When the user taps a notification I want to bring up a screen specific to that ID - yet this seems impossible.

See: https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

Notice the use of a static call to get a string value. The values are set when notifications are created, not when they are tapped. This means only the most recently created notification can pass data to MainActivity!

Any solutions out there?

Best Answer

  • GerryHGerryH USMember ✭✭✭
    Accepted Answer

    finally found a solution - when creating the notification, specify the ID in the call
    PendingIntent.GetActivity(context, ID, etc...
    and then it will be treated uniquely.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Do yo mean the droid MainActivity?
    The link you provided is Android specific... not Xamarin.Forms.

    As a Xamarin.Forms app... The logic should all be platform agnostic. You would really have something going back to the MainActivity. The PCL layer doesn't even know the MainActivity exists. At most you might have a platform dependent DependencyService that you can use; call a method with an object as a parameter to send it - all agnostically. Then on Droid the droid version would handle it... On iOS the iOS implementation would handle it.

    Does that pattern make sense - sound familiar?

  • GerryHGerryH USMember ✭✭✭

    A push notification arrives with an ID -- notification arrival is and must be platform specific as far as I can tell.

    DependencyService is for portable code to call platform specific code, something I do not see a need for in this scenario.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    notification arrival is and must be platform specific as far as I can tell.

    DependencyService is for portable code to call platform specific code,

    Ehh... Sort of. I would say it is closer to say _"So portable code can make use of platform-specific implementations without knowing anything about the platforms." _

    Its not just the PCL calling a method in the DependencyService. You can create an instance of your Interface, which creates the platform implementation, which then... say... has an Event on it you can subscribe to... The PCL hears the event regardless of platform and reacts to it. [Event is just one way as an example and 10 seconds of thought. You could do other ways] But the point is, as you pointed out, the notification is platform-specific... So you have to handle that part at each platform. Thus... DependencyService to go from the platform to the PCL.

  • GerryHGerryH USMember ✭✭✭

    Please read my original post again.

  • GerryHGerryH USMember ✭✭✭
    edited March 2017

    On thing of note, when the user taps the notification it launches the activity (using the class was specified in the notification builder), but the Bundle parameter (onCreate) of the launched Activity is null. If it was not null then maybe it could be used to get the extra parameters. Too bad.

    Turns out "Intent" isn't a static method, it is a property on Activity - yet strangely it still behaves as if it were static. If I create two notifications, attached different IDs to each, both activities (when launched via a tap) will return the same (most recent) value when base.Intent.GetStringExtra() is called!

  • GerryHGerryH USMember ✭✭✭
    Accepted Answer

    finally found a solution - when creating the notification, specify the ID in the call
    PendingIntent.GetActivity(context, ID, etc...
    and then it will be treated uniquely.

Sign In or Register to comment.