简体   繁体   English

获取从Xamarin.Android到Xamarin.Forms的信息(GCM令牌)?

[英]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? 这可能是一个简短而琐碎的问题,但是如何从Xamarin.Android到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. 首先,我从Google Play服务中检索了Android项目中的令牌。
  • 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. Xamarin.Forms与各个平台特定项目之间有多种通信选项。

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. 您可以通过访问Xamarin.Forms.Application类及其“当前”属性来轻松地从每个项目中调用Xamarin.Forms,该属性将为您提供Xamarin.Forms应用程序的实例。

You can also do the reverse - call into your specific platform project by utilising the DependencyService class from your shared Xamarin.Forms code. 您还可以进行反向操作-通过使用共享Xamarin.Forms代码中的DependencyService类来调用特定的平台项目。 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. 使用async / await和TaskCompletionSource,这可能是将在android / ios上获取令牌的固有异步特性抽象为单个可等待函数调用的好方法。 You can find more information on DependencyService here 您可以在此处找到有关DependencyService的更多信息

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 . 最好只使用此处的PushNotifications插件。

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. 如果您有其他更具体的问题或被困在某个地方,请发布新问题,我之前已成功在Android和iOS上成功集成了推送通知,也许可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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