我正在開發我的第一個Android應用程序。我已經徹底搜索,但仍然無法獲得解決方案,並且在終點線之前我沒有太多時間了。GCM應用程序未收到消息
我的應用可以註冊到GCM服務器。我的服務器還沒有準備好,但是我測試了發送消息(服務器 - >設備)通過在線服務投擲它們(注意,GCM在這些發送中返回成功)。問題是似乎沒有消息從GooglePlay服務到達我的接收器。
從Android開發者指南起,我設置的那些代碼片段:
AndroidManifest.xml中
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE" />
<receiver
android:name="it.uniroma1.informatica.didapp.remote.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="it.uniroma1.informatica.didapp" />
</intent-filter>
</receiver>
<service android:name="it.uniroma1.informatica.didapp.remote.GCMIntentService" />
GCMBroadcastReceiver.java
public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
,最後,GCMIntentService.java
public class GCMIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GCMIntentService() {
super("GCMIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " + extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification("Message: " + extras.toString());
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Start.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
所有我需要的是非常簡單的:
- 服務器發送通知(無負載)來GCM
- GCM將其轉發給設備(一個或多個)
- 設備展在系統欄的通知並最終開始活動
我想這兩個移動主包內,更換組件名構造函數的參數的類,但問題仍然存在:( –
@OliverTran是'有史以來onReceive'叫?也許你應該運行在一個應用程序調試器並在該方法中放置一個斷點。 – Eran