简体   繁体   English

Android-如何在用户注册或登录后不重新启动App的情况下将注册ID发送到服务器

[英]Android - How to send registration ID to server once user signs up or logins without restarting App

I have an app with a webview . 我有一个带有webview的应用程序。 My site allows registration and login. 我的网站允许注册和登录。 If the user installs the app for the first time, the app will register the device for GCM . 如果用户是首次安装该应用,则该应用将为GCM注册该设备。 However, when they actually login to the site, the gcm_id is not sent to the server because I have it attached to the webView.load_url as a parameter which is only ran when you first start the app. 但是,当他们实际登录该站点时,gcm_id不会发送到服务器,因为我将其作为参数附加到webView.load_url ,该参数仅在您首次启动该应用程序时才运行。

How do you reload or send the gcm_id without having to wait for them to run the app a 2nd time? 您如何重新加载或发送gcm_id,而不必等待它们第二次运行该应用程序? I don't want it to be continuous either where it appends a url on every page load if I can help it. 我不希望它是连续的,如果我能帮助的话,它也不希望在每个页面加载时都附加一个URL。 I've seen other examples but none have help my issue. 我看过其他示例,但没有一个对我的问题有所帮助。 BTW my server runs php if that matters. 顺便说一句,如果这很重要,我的服务器将运行php。

I found a very nice class that will send http get requests including authentication if you need it via http or https with two lines of code. 我发现了一个非常不错的类,如果需要通过带有两行代码的http或https来发送HTTP get请求(包括身份验证),则该类很好。

EasyHttpClient EasyHttpClient

Very easy to use and you can use it like so. 非常易于使用,您可以像这样使用它。

EasyHttpClient client = new EasyHttpClient();
System.out.println(client.get("https://encrypted.google.com/"));

So what I came up with is when they first launch the app there will be a flag set when the device is initially registered. 因此,我想到的是,当他们首次启动该应用程序时,将在最初注册该设备时设置一个标志。 Then as long as that flag is set, it will periodically send a request using this http client to register it and if successfull it will null the flag and not try anymore. 然后,只要设置了该标志,它将使用此http客户端定期发送请求以对其进行注册,如果成功,它将使该标志为空并且不再尝试。 Since that flag is only set if the registration ID has not been created yet, it will never be set again unless they uninstall it of course. 由于仅在尚未创建注册ID的情况下才设置该标志,因此,除非他们将其卸载,否则再也不会设置该标志。 Seems good so far. 到目前为止似乎还不错。 I have to do this because until they login they are anonymous and my site can't register anonymous users. 我必须这样做,因为在他们登录之前,他们是匿名的,而我的网站无法注册匿名用户。 Just thought someone else might like to know as I found all kinds of tedious ways but that EasyHttpClient is the best I've found. 当我发现各种各样的乏味方法时,以为别人可能想知道,但是EasyHttpClient是我发现的最好的方法。

BTW The reason I did this is because what if they use the app once and get a message after that, the push notification wont work so I had to get it to register with my server the first time they use it. 顺便说一句,我这样做的原因是因为如果他们一次使用该应用程序并在此之后收到消息,则推送通知将无法正常工作,因此我必须在第一次使用该应用程序时将其注册到我的服务器。

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

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