我正在为 android 使用 GCM,这是 androidhive.com 上给出的示例代码。我能够将数据存储在数据库中,但它无法推送数据。我是这个代码的新手。所以请帮助我。我的连接 java 文件和相应的 php 文件以及 logcat 消息如下所示---
错误日志-
11-07 16:55:42.306: E/Trace(1025): error opening trace file: No such file or directory (2)
11-07 16:55:43.146: D/libEGL(1025): loaded /system/lib/egl/libEGL_emulation.so
11-07 16:55:43.166: D/(1025): HostConnection::get() New Host Connection established 0x2a1193e0, tid 1025
11-07 16:55:43.188: D/libEGL(1025): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-07 16:55:43.196: D/libEGL(1025): loaded /system/lib/egl/libGLESv2_emulation.so
11-07 16:55:43.337: W/EGL_emulation(1025): eglSurfaceAttrib not implemented
11-07 16:55:43.366: D/OpenGLRenderer(1025): Enabling debug mode 0
11-07 16:56:00.818: W/EGL_emulation(1025): eglSurfaceAttrib not implemented
11-07 16:56:04.207: E/SpannableStringBuilder(1025): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-07 16:56:04.207: E/SpannableStringBuilder(1025): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
11-07 16:56:21.786: W/EGL_emulation(1025): eglSurfaceAttrib not implemented
11-07 16:56:35.676: D/dalvikvm(1025): GC_CONCURRENT freed 238K, 4% free 8207K/8519K, paused 109ms+131ms, total 355ms
11-07 16:56:35.766: D/GCMRegistrar(1025): resetting backoff for com.androidhive.pushnotifications
11-07 16:56:35.806: V/GCMRegistrar(1025): Registering app com.androidhive.pushnotifications of senders 706448096441
11-07 16:56:35.886: I/Choreographer(1025): Skipped 34 frames! The application may be doing too much work on its main thread.
11-07 16:56:36.052: W/EGL_emulation(1025): eglSurfaceAttrib not implemented
11-07 16:56:37.826: V/GCMBroadcastReceiver(1025): onReceive: com.google.android.c2dm.intent.REGISTRATION
11-07 16:56:37.826: V/GCMBroadcastReceiver(1025): GCM IntentService class: com.androidhive.pushnotifications.GCMIntentService
11-07 16:56:37.846: V/GCMBaseIntentService(1025): Acquiring wakelock
11-07 16:56:37.876: V/GCMBaseIntentService(1025): Intent service name: GCMIntentService-706448096441-1
11-07 16:56:37.896: E/GCMRegistrar(1025): internal error: retry receiver class not set yet
11-07 16:56:37.896: V/GCMRegistrar(1025): Registering receiver
11-07 16:56:37.906: D/GCMBaseIntentService(1025): handleRegistration: registrationId = APA91bG-OslPbDcT_9iy0QTP7e_4ih9q95m6fyZJ4geYx1JojDhjmGUTy7aCvVMAq9vLXo31Kh5uP6YpenVc1zDOavhrX1gyayQAXevq8j1T0t1lf88Qm3ohz5mNQ7iC7kY52FG1hD8pgH5aSMGqb55xNGvCIuuFSbIJ3wmiGkpzHhYH2WEp-Q4, error = null, unregistered = null
11-07 16:56:37.906: D/GCMRegistrar(1025): resetting backoff for com.androidhive.pushnotifications
11-07 16:56:37.946: V/GCMRegistrar(1025): Saving regId on app version 1
11-07 16:56:37.978: I/GCMIntentService(1025): Device registered: regId = APA91bG-OslPbDcT_9iy0QTP7e_4ih9q95m6fyZJ4geYx1JojDhjmGUTy7aCvVMAq9vLXo31Kh5uP6YpenVc1zDOavhrX1gyayQAXevq8j1T0t1lf88Qm3ohz5mNQ7iC7kY52FG1hD8pgH5aSMGqb55xNGvCIuuFSbIJ3wmiGkpzHhYH2WEp-Q4
11-07 16:56:38.016: D/NAME(1025): radha
11-07 16:56:38.016: I/AndroidHive GCM(1025): registering device (regId = APA91bG-OslPbDcT_9iy0QTP7e_4ih9q95m6fyZJ4geYx1JojDhjmGUTy7aCvVMAq9vLXo31Kh5uP6YpenVc1zDOavhrX1gyayQAXevq8j1T0t1lf88Qm3ohz5mNQ7iC7kY52FG1hD8pgH5aSMGqb55xNGvCIuuFSbIJ3wmiGkpzHhYH2WEp-Q4)
11-07 16:56:38.016: D/AndroidHive GCM(1025): Attempt #1 to register
11-07 16:56:38.056: V/AndroidHive GCM(1025): Posting 'email=bhawna2909@gmail.com®Id=APA91bG-OslPbDcT_9iy0QTP7e_4ih9q95m6fyZJ4geYx1JojDhjmGUTy7aCvVMAq9vLXo31Kh5uP6YpenVc1zDOavhrX1gyayQAXevq8j1T0t1lf88Qm3ohz5mNQ7iC7kY52FG1hD8pgH5aSMGqb55xNGvCIuuFSbIJ3wmiGkpzHhYH2WEp-Q4&name=radha' to http://10.0.2.2/gcm_server_php/register.php
11-07 16:56:38.056: E/URL(1025): > http://10.0.2.2/gcm_server_php/register.php
11-07 16:56:39.057: V/GCMRegistrar(1025): Setting registeredOnServer status as true until 2012-11-14 16:56:39.057
11-07 16:56:39.166: V/GCMBaseIntentService(1025): Releasing wakelock
Java 文件——
package com.androidhive.pushnotifications;
import android.content.Context;
import android.content.Intent;
public final class CommonUtilities {
// give your server registration url here
static final String SERVER_URL = "http://10.0.2.2/gcm_server_php/register.php";
// Google project id
static final String SENDER_ID = "706448096441";
/**
* Tag used on log messages.
*/
static final String TAG = "AndroidHive GCM";
static final String DISPLAY_MESSAGE_ACTION =
"com.androidhive.pushnotifications.DISPLAY_MESSAGE";
static final String EXTRA_MESSAGE = "message";
/**
* Notifies UI to display a message.
* <p>
* This method is defined in the common helper because it's used both by
* the UI and the background service.
*
* @param context application's context.
* @param message message to be displayed.
*/
static void displayMessage(Context context, String message) {
Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
intent.putExtra(EXTRA_MESSAGE, message);
context.sendBroadcast(intent);
}
}
PHP 文件——
<?php
// response json
$json = array();
/**
* Registering a user device
* Store reg id in users table
*/
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["regId"]))
{
$name = $_POST["name"];
$email = $_POST["email"];
$gcm_regid = $_POST["regId"];
// GCM Registration ID
// Store user details in db
include_once './db_functions.php';
include_once './GCM.php';
$db = new DB_Functions();
$gcm = new GCM();
$res = $db->storeUser($name, $email, $gcm_regid);
$registatoin_ids = array($gcm_regid);
$message = array("product" => "shirt");
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
else {
// user details missing
}
?>