- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 GCM 消息有疑问。如果是数组形式的消息,我不知道如何处理。目前我的代码适用于类似“$message = 1;”的简单消息
但是我想发送一条包含数据库查询数组的消息。目前我正在从数据库查询(多个 ID)中获取注册 ID 并且它正在工作,所以大概它对消息的工作方式相同,但到目前为止运气不好。
下面提供了客户端和服务器端(php),我需要一种在客户端处理数组消息的方法。任何帮助表示赞赏。
这是 php 服务器部分。这部分正在工作。
<?php
// Message to be sent
function Reg() {
require_once __DIR__ . '/db_connect.php';
$db = new DbConnect();
$response = array();
$response1 = array();
$API_Key='AIzaSyCQgPGhpAzuWGuUd0DCI8pYaXXAItthEsg';
$url = 'https://android.googleapis.com/gcm/send';
$result1 = mysql_query("SELECT *FROM test") or die(mysql_error());
$stack = array();
while ($row = mysql_fetch_array($result1)) {
$Reg_ID = $row["Reg_ID"];
$Distance = $row["Distance"];
array_push($response1, $Distance);
}
$result = mysql_query("SELECT *FROM registrationid") or die(mysql_error());
$stack = array();
while ($row = mysql_fetch_array($result)) {
$Reg_ID = $row["Reg_ID"];
$Registration_ID = $row["Registration_ID"];
array_push($response, $Registration_ID);
}
$fields = array(
'registration_ids' => $response,
'data' => $response1 ,
);
print_r ($fields);
print_r ($response1);
$headers = array(
'Authorization: key=' . $API_Key,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
}
Reg();
?>
这是客户端。
package com.techlovejump.gcm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.EditText;
public class GcmIntentService extends IntentService{
Context context;
private static String URL_GPS = "http://10.0.2.2/GET_EVERYTHING_TOGTHER/Get_GPS_From_Bus.php";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
JSONParser jsonParser = new JSONParser();
public static final String TAG = "GCM Demo";
GPSTracker gps;
public GcmIntentService() {
super("GcmIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Bundle extras = intent.getExtras();
String msg = intent.getStringExtra("message");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// This loop represents the service doing some work.
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
//sendNotification("Received: " + extras.toString());
Log.i("MSG", msg);
Log.e("msg", msg);
Log.i(TAG, "Received: " + extras.toString());
sendNotification(msg);
///////ADDED on 27/12/2013
//////////////////////////////////////////////////////add one here
GPS_Configure(msg);
//////////////////////////////////////////////////////
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent myintent = new Intent(this, ReceiveActivity.class);
myintent.putExtra("message", msg);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_gcm)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
private void GPS_Configure(String msg) {
gps = new GPSTracker(GcmIntentService.this);
// check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Log.i("latitude", Double.toString(latitude));
Log.i("longitude", Double.toString(longitude));
new GPS_GetData().execute(Double.toString(latitude),
Double.toString(longitude), msg);
} else {
gps.showSettingsAlert();
}
}
class GPS_GetData extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("latitude", args[0]));
//params.add(new BasicNameValuePair("longitude", args[1]));
params.add(new BasicNameValuePair("latitude", "31.97198937240043"));
params.add(new BasicNameValuePair("longitude", "35.1956698624676"));
params.add(new BasicNameValuePair("Request_ID", args[2]));
Log.i("LAT", args[0]);
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(URL_GPS,
"POST", params);
// check log cat fro response
// Log.d("Create Response", json.toString());
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
}
}
}
最佳答案
你想要完成的是不可能的。 GCM 支持在单个 HTTP 请求中向多个注册 ID 发送相同的消息,但所有注册 ID 的消息负载都是相同的。
如果您需要向每个注册 ID 发送不同的消息负载,则必须分别发送每条消息(在不同的 HTTP 请求中)。
关于php - GCM 向多个客户端发送多条消息(数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20832064/
我想我理解 GCM 的新 notification_key 功能背后的基本原理是“只需将消息发送给用户并且只让他们阅读一次”。我从文档的这一部分推断出来: With user notification
我实现了网络推送通知。获取错误的步骤: 打开网站 订阅推送通知 通过 gcm 发送许多推送 - 一切正常 关闭网站标签 发送推送和接收“双推送”- 第一个正常,第二个是“此站点已在后台更新” 重新打开
我正在手机上测试 GCM。 (2.3.6 安卓). list 文件(MainActivity、First 和 Second Activity 不执行任何操作,它们用于其他一些测试目的,不干扰 GCM)
首先,我是 iOS 开发的新手,也是 swift 的新手。我正在尝试让适用于 iOS 的谷歌云消息传递示例。 我已经从 https://developers.google.com/cloud-mess
你们中的任何人都可以提供 GCM 推送通知的代码,其中消息从移动设备推送到 GCM 服务器。我已经从 GCM 服务器到移动设备及其工作进行了操作,但我不知道反之亦然如何操作。任何人都可以提供帮助吗?谢
我们有两个不同的应用程序,但都需要来自 Google Cloud Messages 的相同推送通知,并且我在两个应用程序中只使用了一个发件人 ID(启用 Google 控制台之一的项目编号)。 这样做
我有一个广播接收器,当应用程序打开并在后台时,它会检测即将到来的通知,但当最近的应用程序被清除时,接收器无法工作,请给我建议。 public class GcmBroadcastReceiver ex
我想从我的应用程序向 GCM 服务器发送一个心跳信号,以便连接保持有效。 我该怎么做,我怎么知道我的 GCM 服务器的 URL? 提前致谢! 最佳答案 如何发送心跳 这个类可以发送正确的 Intent
新的 GCM 3.0 应该允许 GCM 自动显示从服务器发送的通知,如果它们包含 notification 参数。 如 docs 中所述: The notification parameter wit
这个问题在这里已经有了答案: Do I need to migrate GCM to FCM on client side? (2 个答案) 关闭 3 年前。 Google 已于 2018 年 4
我遇到了 GCM 推送通知无法正确到达 Android 设备的问题。经过几天的研究,我发现 Android 设备使用心跳来保持与 GCM 服务的连接。遗憾的是,心跳似乎太高了,因此 Android 设
阅读堆栈溢出 2 天后我做了什么: 问题关键字:“Apple-Mach-O 链接器错误”、“libGcmLib.a(GCMRmqManager.o) ", sqlite3", "GCMRmq2Pers
我已经从 GCM 订阅了主题,当我通过 Android 设置删除所有应用程序数据时,GCM token 是相同的,关于主题的 GCM 通知仍然可用,所以我收到了我不想收到的通知。 我的问题是: 如何从
由于 gcm 已弃用,我们希望迁移我们的代码。正如在谷歌的迁移指南中提到的那样,对于我们的服务器应用程序,应该只需要将端点从 gcm 更改为 fcm。应用迁移已成功完成。 我们现在使用的是 com.g
这只发生在我的 Kyocera Rise 上。我有一个依赖 GCM 在手机之间进行通信的应用程序。我的 Nexus 4 和我的 HTC One X 之间的通信工作正常,每当我发送推送通知时,两部手机都
我遇到了与 this 相同的问题.我会尝试提供更多信息。 我正在使用 Play Framework,用 Java 编写。我写了一个叫做 PushNotificationQueue 的插件。 PushN
我需要在我的应用程序中接收来自不同发件人的推送通知。会成功吗? 最佳答案 你的问题的答案是是! 根据 GCM 的官方文档,您的应用可以接收来自多个发件人的消息(限制为 100 个不同的发件人),并且您
我有一个使用 GCM 推送通知的应用程序。它工作正常,我的设备注册并接收推送消息。 如果我从我的设备上卸载该应用程序,我将不再像您期望的那样收到消息。在我卸载应用程序后,您在服务器上发送消息的文本框仍
我正在处理另一个开发人员的现有项目,我需要获取 PubNub 设置的 GCM 服务器 key ,因为它被意外删除了。 有什么方法可以从 Google 设置中检索它?我在凭据和项目设置中找不到它。 我暂
嘿,我在调用 `subscribeToTopicP 类时遇到了一些 Gcm Intent 服务的问题,总是出现空指针异常。 这是我的代码: GcmIntentService.java private
我是一名优秀的程序员,十分优秀!