我正在开发一个以GCM为主要功能的项目。
我遵循此AndroidHive.com来实现GCM客户端和GCM服务器。
在xampp服务器中运行html文件时,我在屏幕上得到这种类型的输出:
{"multicast_id":7075430686045407371,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{"message_id":"0:1394625413704378%93abcc37f9fd7ecd"}
]
}
我在Android设备上没有获得任何类型的消息或通知。
我对php知之甚少,所以我无法理解 是什么。
我的php代码有问题或我的Android代码有问题吗?
答案 0 :(得分:1)
确保推送通知有3件事情可以使其正常工作。
服务器端:项目ID (您可以从Google云端获取)
客户端: API密钥(也来自谷歌云)和注册号(您的设备ID)
答案 1 :(得分:1)
首先,您是否尝试过PHP脚本以查看与Google服务器的连接是否成功?尝试回应你的回答,看看是否一切顺利。
其次,您需要构建GCMBroadcastReceiver'java
客户端以接收消息:
package myApp.packageName;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.games.Notifications;
import com.google.android.gms.gcm.GoogleCloudMessaging;
/**
* Handling of GCM messages.
*/
public class GcmBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Intent intent)
{
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType))
{
sendNotification("MyNotificationTitle", "Send error: " + intent.getExtras().toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType))
{
sendNotification("MyNotificationTitle", "Deleted messages on server: " + intent.getExtras().toString());
}
else
{
String payload = intent.getStringExtra("payload");
String type = intent.getStringExtra("type");
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
}
// Put the GCM message into a notification and post it.
private void sendNotification(String title, String msg) {
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, myActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.icon)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
在onRecieve()
方法中尝试将消息打印到日志,任何消息,只是为了查看事件是否正确触发。
答案 2 :(得分:1)
您可以关注this link。它对你有帮助。