2014-03-13 35 views
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();   
    } 
} 

通過的方式,所有權限都存在。

感謝您的閱讀!

回答

3

,谷歌的例子是有點誤導,因爲它把所有的臨時演員爲一體。您是如何將信息從捆綁包中取出的取決於您發送的方式。無論你使用的方法,它會要求你得到一個「額外」出在客戶端。

假設你使用PHP來發送,那麼你的數據塊可能看起來像:

$fields = array(
      'registration_ids' => $registrationIDs, 
      'data' => array("message" => $message, 
           "moredata" => $morestuff), 
      'delay_while_idle'=> false, 
      'time_to_live' => 86400, 
      'collapse_key'=>"".$randomNum."" 
      ); 

其中「消息」和「MOREDATA」是鍵我發明的。要在客戶端提取你會:

if (intent.hasExtra("message")) { 
    String theMessage = intent.getStringExtra("message"); 
} 

if (intent.hasExtra("moredata")) { 
    String moreData = intent.getStringExtra("moredata"); 
} 

你應該能夠把這種代碼,以利用自己的鍵/值對。

+0

對不起,我在回答延遲!服務器端的數據是錯誤的!謝謝 ! – johann

+0

你使用一整天的時間在這方面努力保存。我有兩個類型的兩個不同的密鑰,現在我使用額外的消息,似乎我有錯。順便說一句,我用(extra.haskey(stringkey)和工作 –