0
我已經實現了谷歌GCM爲Android完全一樣寫在這個教程: http://developer.android.com/google/gcm/client.html的Android GCM:問題與消息
當發送消息時,廣播接收器捕捉意圖和調用GcmIntentService類:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 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);
}
}
但在'sendNotification'方法(服務類)中,「msg」不是我發送的消息,而是類似於:
Bundle[{android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=153438952475}]
我的服務:
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
public NotificationCompat.Builder builder;
private String TAG = this.getClass().getSimpleName();
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
Log.e(TAG,"GoogleCloudMessaging: error: "+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
Log.e(TAG,"GoogleCloudMessaging: deleted: "+ extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
GcmResult gcm = GcmResult.getGcm(msg);
GcmResultAsync async = new GcmResultAsync();
async.setInfo(getApplicationContext(),gcm);
async.execute();
}
}
通過的方式,所有權限都存在。
感謝您的閱讀!
對不起,我在回答延遲!服務器端的數據是錯誤的!謝謝 ! – johann
你使用一整天的時間在這方面努力保存。我有兩個類型的兩個不同的密鑰,現在我使用額外的消息,似乎我有錯。順便說一句,我用(extra.haskey(stringkey)和工作 –