如何在需要时发送服务器上的设备的位置器上、位置、设备、如何在

2023-09-12 02:05:12 作者:美男去断袖吓哭单身狗

我开发的,我需要当前的纬度和长期的设备的应用程序。 时间框架来获取纬度/经度将被服务器决定。服务器将每当纬度/朗需要是获取发送通知。当设备得到通知 服务器,同时设备发送其位置到服务器。

I am developing a application in which i need current lat and long of the device. Time frame to get the lat/long will be decided by the server. Server will send notification whenever lat/lang is required to be fetched. When device get notified from the server,at the same time device send its location to server.

有些条件:

我要纬度/即使长时间,如果设备不具备GPS

I want lat/long even if device don't have gps

如果用户不触摸或单击该通知,然后我也想获取 纬度/经度,也就是说,即使设备处于睡眠模式

if user doesn't touch or click the notification,then also i want to fetch lat/long, i.e. even if the device is in sleep mode.

此外,我不想跑了整整一天作为后台服务 它消耗了设备的电池。

Also i don't want to run a background service for the whole day as it consumes battery of the device.

推荐答案

我肯定会建议使用谷歌云消息传递在这里。这样做,一旦设备运行你的应用程序的好处,你可以把它注册对 GCM ,你可以向他们发送一些信息,这将使他们注册自己的坐标到你的数据库。

I would definitely suggest using Google Cloud Messaging here. This has the benefit that once a device runs your app, you can make it register against GCM and you can send them some message that would make them register their coordinates into your database.

此方法需要,你必须实现某些服务器(例如,一个Web服务器,PHP),使用户与它通信,所以流量将是:

This approach would need that you have to implement some server (for instance, a web server with PHP) and make the users communicate with it, so the flow would be:

您的服务器发送一个广播消息给所有注册设备告诉他们,他们必须对 GCM 服务登记。

该设备得到消息,你实现了的BroadcastReceiver 来同时获得纬度和经度并将其发送到远程服务器,比如 http://www.mysuperserver.com/getlonglat.php ,通过 POST 的要求。

The devices get the message, and you implement the BroadcastReceiver to get both latitude and longitude and send it to your remote server, say http://www.mysuperserver.com/getlonglat.php, via a POST request.

您的服务器需要两个参数,并保存他们或做任何你需要的。

Your server takes both parameters and you save them or do whatever you need.

如果你想跟随这个做法,这是步骤,你必须遵循:

If you want to follow this approach, this are the steps you have to follow:

转到 https://console.developers.google.com 。有了您的谷歌帐户,注册一个新的项目。这将为您提供一个API密钥和发件人ID。 彻底搞懂计算机网络通信设备与协议

Go to https://console.developers.google.com. With your Google account, register a new project. This will provide you an API key and a Sender ID.

您需要做对您的项目在设备寄存器 GCM 。您可以通过在项目中导入谷歌播放服务做到这一点,而一旦做了,做什么事情都这样:

You'll need to make the device register against your project in GCM. You can achieve this by importing Google Play Services within your project, and once done, do something alike to this:

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
final String regid = gcm.register("YOUR_SENDER_ID");

在这个时候, GCM 服务器已经知道该用户已注册whithin您的项目,并给了他一个 REGID

At this time, the GCM server already knows that this user has registered whithin your project and has given him a regid.

下一步是告知用户已经这样做了,并保存在某处自己的远程服务器 REGID ,它已被赋予,所以你以后可以向他们发送消息。因此,在客户端,让对你的服务器 HTTP POST 要求发送的 REGID 和somethat这样的过程中,它

The next step is informing your own remote server that the user has done so, and save somewhere the regid that it has been given, so you can later send them messages. So in the client side, make a HTTP POST request against your server sending that regid and process it with somethat like this:

$regid = $_POST['regid'];

if (($regId) && ($ipAddr))
  sql_query("INSERT INTO gcm_subscribers(regid) VALUES($regid')");

一旦这样做,你知道谁已经对你的数据库中注册。您可以 SELECT 所有用户,并给他们发送信号给您发回自己的坐标。在下面的例子中,参数将是,首先,登记ID数组,其次要发送的消息(例如, $ messageData =sendMeYourCoords!)。

Once done, you know who have already registered against your database. You can SELECT all the users and send them a signal to send you back their coordinates. In the following example, the parameters would be, firstly, an array of registration IDs and secondly the message to send (for instance, $messageData = "sendMeYourCoords!").

function sendNotification($registrationIdsArray, $messageData) {
  $apiKey = "YOUR_API_KEY";

  $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
  $data = array(
    'data' => $messageData,
    'registration_ids' => $registrationIdsArray
  );

  $ch = curl_init();

  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
  curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

  $response = curl_exec($ch);
  curl_close($ch);

  return $response;
}

一旦在客户端,只是过程, GCM 消息,再拍 POST 另一个网​​址您的远程服务器上,并通过它的经度纬度。要处理的消息,我包括以下几个环节。

Once in your client, just process that GCM message and make another POST to another URL on your remote server and pass it the longitude and latitude. To process the messages I'm including a few links below.

和获取的坐标没有GPS似乎不会有太多的解决方案,但也有一些。这将是一个例子:How让不使用GPS的安卓Latitute经度值?

And for getting the coordinates without GPS seems there aren't too much solutions, but there are some. This would be an example: How to get Latitute Longitude value without using GPS in Android?

更多关于这一点:

借鉴Google云消息

Android按使用谷歌云通讯GCM通知 - 安卓例

谷歌云消息传递使用PHP

Connection PHP(服务器)和Android(客户端)使用HTTP和JSON之间

Notificaciones推的Andr​​oid:谷歌云消息(GCM)。 ImplementaciónCliente(努埃瓦版)(西班牙语)