我正在GCM (Google Cloud Messaging- PUSH Notifications)
我的应用程序中实施。我已按照 developer.android.com 的 GCM 教程中给出的所有步骤进行操作
我的应用程序的构建目标指向Goolge API 8 (Android 2.2 version)
.
我能够从 GCM 成功获取注册 ID,并将此 ID 传递给我的应用程序服务器。这样注册步骤就成功执行了。
现在,当我的应用程序服务器向我的设备发送 PUSH 消息时,服务器收到的消息为SUCCESS=1 FAILURE=0, etc.
,即服务器发送消息成功,但我的设备从未收到消息。
经过大量搜索,我知道 GCM 在端口号 5228、5229 或 5230 上推送消息。
最初,我的设备和笔记本电脑被限制访问某些网站,但后来我被授予访问所有网站的所有权限,所以我猜这些端口号对我的设备是开放的。
所以我的问题是:我从未收到来自 GCM 的任何 PUSH 消息。我的 GCMIntenService 类的 onMessage() 永远不会被调用。可能是什么原因?
请参阅我的以下代码并相应地指导我:
我在我的清单中声明了以下内容:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<permission
android:name="package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="package.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<receiver
android:name="com.google.android.gcm.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" />
<category android:name="packageName" />
</intent-filter>
</receiver>
<receiver
android:name=".ReceiveBroadcast"
android:exported="false" >
<intent-filter>
<action android:name="GCM_RECEIVED_ACTION" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
/>
/**
* @author Shrikant.
*
*/
public class GCMIntentService extends GCMBaseIntentService {
/**
* The Sender ID used for GCM.
*/
public static final String SENDER_ID = "myProjectID";
/**
* This field is used to call Web-Service for GCM.
*/
SendUserCredentialsGCM sendUserCredentialsGCM = null;
public GCMIntentService() {
super(SENDER_ID);
sendUserCredentialsGCM = new SendUserCredentialsGCM();
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
sendUserCredentialsGCM.sendRegistrationID(registrationId);
}
@Override
protected void onUnregistered(Context context, String arg1) {
Log.i(TAG, "unregistered = " + arg1);
sendUserCredentialsGCM
.unregisterFromGCM(LoginActivity.API_OR_BROWSER_KEY);
}
@Override
protected void onMessage(Context context, Intent intent) {
Log.e("GCM MESSAGE", "Message Recieved!!!");
String message = intent.getStringExtra("message");
if (message == null) {
Log.e("NULL MESSAGE", "Message Not Recieved!!!");
} else {
Log.i(TAG, "new message= " + message);
sendGCMIntent(context, message);
}
}
private void sendGCMIntent(Context context, String message) {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("GCM_RECEIVED_ACTION");
broadcastIntent.putExtra("gcm", message);
context.sendBroadcast(broadcastIntent);
}
@Override
protected void onError(Context context, String errorId) {
Log.e(TAG, "Received error: " + errorId);
Toast.makeText(context, "PUSH Notification failed.", Toast.LENGTH_LONG)
.show();
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
}