【发布时间】:2014-06-21 13:26:17
【问题描述】:
我正在尝试使用 appengine 实施 GCM。我创建了默认的 appengine 后端 + GCM 模板并进行了部署。 我创建了一个 GCM 注册 ID:
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String regId = gcm.register(SENDER_ID); //sender_id is my project number
然后我使用模板中的默认注册端点将此 regID 存储在 appengine 上。我的广播接收器如下所示:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Ayush", "Received");
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="reach.project" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION" />
<category android:name="reach.project" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<permission
android:name="reach.project.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
</manifest>
BroadCastReceiver 和意图服务在我的项目中是独立的类。 appengine 后端 + GCM 是一个单独的模块。 在创建此模块期间:
编译项目(路径:':app2',配置:'android-endpoints')
这一行被自动附加到主模块的 build.gradle 中。
package com.example.mymodule.app2,是我的路径 消息端点。
当我注册设备时,我的广播接收器被激活,但是当我尝试从 apppot.com 网站或 api explorer 发送消息时,它没有收到任何内容。我可以确认消息正在发送,如后端的日志所示。 请帮忙。
我正在关注本教程: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints
【问题讨论】:
-
你能展示更多你的清单吗? (应用的包名,权限)
-
您好,我已经编辑了我的问题,请看一下。
标签: android google-app-engine google-cloud-messaging