我正在我的应用程序中实现GCM (Google Cloud Messaging- PUSH Notifications)
。我已经按照developer.android.com
我的应用程序的构建目标指向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);
}
}
答案 0 :(得分:0)
如果重命名了包名,则应覆盖GCMBroadcastReceiver类child中的getGCMIntentServiceClassName()方法。示例here。