【发布时间】:2016-07-01 17:41:31
【问题描述】:
我使用 Google Cloud Messaging 实现了应用引擎模块,设备已成功注册,并且我已经实时部署了它。我的问题是,通过使用这种集成做一个 RESTful API,我是否需要制作服务器端代码来发送推送通知,还是可以通过客户端部分发送它们?
这是我的 Android 清单代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myPackage">
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature
android:name="android.hardware.Camera2"
android:required="true" />
<!-- This filters devices that don't support sip -->
<uses-feature
android:name="android.hardware.sip.voip"
android:required="true" />
<uses-feature
android:name="android.hardware.wifi"
android:required="true" />
<uses-feature
android:name="android.hardware.microphone"
android:required="true" />
<receiver
android:name=".IncomingCallReceiver"
android:label="Call Receiver" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- [START gcm_permission] -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" >
<!-- [END gcm_permission] -->
<permission
android:name="myPackage.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="myPackage.permission.C2D_MESSAGE" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity android:name=".LogInActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_app_id" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
<activity android:name=".MainActivity" />
<activity
android:name=".Messaging.Activities.FriendsRequestsActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".Messaging.Activities.ChatActivity"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/ChatTheme"
android:windowSoftInputMode="adjustResize|stateHidden">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="myPackage" />
</intent-filter>
</receiver>
<service
android:name=".Utils.InstanceIDListener"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="myPackage.Utils.RegistrationIntent"
android:exported="false"></service>
<service
android:name="myPackage.Utils.GcmListener"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</application>
</manifest>`
这是我的 GcmListener 接收应该接收消息:
public class GcmListener extends GcmListenerService {
private static final String TAG = "Push";
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param data Data bundle containing message data as key/value pairs.
* For Set of keys use data.keySet().
*/
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Log.d(TAG, "Push received");
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
sendNotification("Received: " + jsonObject.toString(5));//TODO Forward to be written
} catch (JSONException e) {
e.printStackTrace();
}
if (from.startsWith("/topics/")) {
// message received from some topic.
} else {
// normal downstream message.
}
// [START_EXCLUDE]
/**
* Production applications would usually process the message here.
* Eg: - Syncing with server.
* - Store message in local database.
* - Update UI.
*/
/**
* In some cases it may be useful to show a notification indicating to the user
* that a message was received.
*/
//sendNotification(message); TODO create the correct notification to be showed to the user
// [END_EXCLUDE]
}
}
sendNotification 方法还没有写,但是我放了一个日志来检查是否收到消息,但我什么也没得到,老实说,我认为我使用了错误的 Google Developers Console 的 API_KEY,我使用的是: 服务器密钥(由 Google 服务自动创建),或者可能是清单中服务声明的顺序。这些是我能想到的唯一可能导致应用程序无法收到推送消息的事情
【问题讨论】:
-
您可以使用本教程(答案已被接受)检查您的 API_KEY 或注册 ID 是否错误.. 之后请告诉我:stackoverflow.com/questions/22168819/…
-
我收到此消息:未经授权的错误 401
-
当我将服务器 API_KEY 更改为 Android API_KEY 时,我得到以下信息: {"multicast_id":4751777491966629353,"success":0,"failure":1,"canonical_ids":0,"results" :[{"error":"MismatchSenderId"}]}
-
这意味着你的项目ID是错误的。你可以去谷歌项目网站再检查一下
-
我做错了,这是我使用项目 ID 而不是项目编号...非常感谢!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!你太棒了!
标签: android google-cloud-messaging