2015-11-04 51 views
2

我試圖接收推送通知消息併發送通知消息,但我得到的空值沒有收到任何消息。如何在android中獲取推送通知消息?

下面的代碼

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    String messageType = gcm.getMessageType(intent); 
    Log.d("push","msg type: " + messageType); 
    if (!extras.isEmpty()) { 
     if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR 
       .equals(messageType)) { 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED 
       .equals(messageType)) { 
     } else if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE 
       .equals(messageType)) { 
      Log.d("push notify msg: ", extras.toString()); 
      sendNotification(extras.toString()); 
     } 
    } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 
+0

參考此鏈接可能會幫助您:http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and- mysql/ –

回答

3

創建一個接收器,並在清單申報。

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    ComponentName comp = new ComponentName(context.getPackageName(),XYZIntentService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 
} 

清單:

<receiver 
     android:name="com.companyname.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="PKG_NAME" /> 
     </intent-filter> 
    </receiver> 

元數據標籤:

<meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

權限:

<permission 
    android:name="com.vuliv.player.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.vuliv.player.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> 

現在,您可以將您的代碼添加到XYZIntentService獲得GCM通知。

對於任何查詢都可以隨意提出。

+0

嗨Vibhor,我給了上面的細節提到,但我仍然只收到空消息... –