简体   繁体   中英

Get information (GCM Token) from Xamarin.Android to Xamarin.Forms?

This might be a very short and trivial question, but how do I get information from Xamarin.Android to Xamarin.Forms?

Basically, what I would like to do is the following:

  • First I retrieve a token in my Android project from the Google Play Services.
  • Then I start my main application and show a login screen.
  • During the login I would like to forward the token to my server, so he is aware of my token and can send me messages.

Is that a good idea? How do I get my token into the Forms part of my application?

Thanks in advance!

There are multiple options for communicating between Xamarin.Forms and the individual platform specific projects.

You can call into Xamarin.Forms from each project rather easily by accessing the Xamarin.Forms.Application class and it's "Current" property which will give you the instance of your Xamarin.Forms application.

You can also do the reverse - call into your specific platform project by utilising the DependencyService class from your shared Xamarin.Forms code. With async/await and TaskCompletionSource this can be a good way to abstract the inherent async nature of obtaining tokens on android/ios into a single awaitable function call. You can find more information on DependencyService here

Lastly, from what I gather, you are figuring out how to register for push notifications. It might be best to simply use the PushNotifications plugin located here .

It should help you get started. Be sure to read through the documentation. There are a bunch of steps needed to get everything working.

If you have more specific questions or get stuck somewhere, post a new question, I have integrated push notifications on both Android and iOS succesfully before and might be able to help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM