2012-11-27 46 views
0

我在接收Google推送通知時遇到以下問題。 我在PHP下面提交通知,並在android GCMBaseIntentService中添加一個標籤data.notificationID以接收唯一標識。通過Google推送通知從額外密鑰中接收舊值

$data = array(
      'registration_id' => $deviceToken, 
      'collapse_key' => $collapseKey, 
      'data.message' => $messageText , 
      'data.notificationID' => $yourKey 
      ); 

通知來正常,但notificationID值是老年人我已經先推了。它不是鏈接的。但data.message消息字符串新來的,我已經發送。在PHP

推送通知的代碼是:

 <?php  
      $Key = "0000000"; 
      $deviceToken = "sdfsjdfljsd"; 
      $collapseKey = 1; 
      $messageText = "Hi welcome"; 
      //$yourKey  = 100; 
      $yourKey  = 101; 

     $headers = array('Authorization:key=' . $Key); 
     $data = array(
      'registration_id' => $deviceToken, 
      'collapse_key' => $collapseKey, 
      'data.message' => $messageText , 
      'data.notificationID' => $yourKey 
      ); 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send"); 
     if ($headers) 
      curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

     $response = curl_exec($ch); 
     $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     if (curl_errno($ch)) { 
      return false; 
     } 
     if ($httpCode != 200) { 
      return false; 
     } 
     curl_close($ch); 
    ?> 

而在Android的GCMIntentService實現:

 import android.app.IntentService; 
    import android.app.Notification; 
    import android.app.NotificationManager; 
    import android.app.PendingIntent; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.graphics.Color; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.Toast; 

    import com.google.android.gcm.GCMBaseIntentService; 
     public class GCMIntentService extends GCMBaseIntentService { 

     @SuppressWarnings("hiding") 
     private static final String TAG = "GCMIntentService"; 

     public GCMIntentService() { 
      super("XXXXXXXXXX"); 
     } 

     /** 
     * Issues a notification to inform the user that server has sent a message. 
     */ 
     private static void generateNotification(Context context, String message , String notificationID) { 
      long when = System.currentTimeMillis(); 
      NotificationManager notificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification notification = new Notification(R.drawable.ic_launcher, 
        message, when); 

      String title = context.getString(R.string.app_name); 
      Intent notificationIntent = new Intent(context, ViewNotification.class); 
      //notificationIntent.removeExtra("notificationID"); 
      notificationIntent.putExtra("notificationID", notificationID); 
      // set intent so it does not start a new activity 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
        | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      PendingIntent intent = PendingIntent.getActivity(context, 0, 
        notificationIntent, 0); 
      notification.setLatestEventInfo(context, title, message, intent); 
      notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL; 
      notification.ledARGB = Color.BLUE; 
      notification.ledOnMS = 1000; 
      notification.ledOffMS = 300; 


      notificationManager.notify(1 , notification); 
     } 

     @Override 
     protected void onError(Context arg0, String arg1) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     protected void onMessage(Context arg0, Intent arg1) { 

      Log.d("GCM", "RECIEVED A MESSAGE"); 

      generateNotification(arg0, arg1.getStringExtra("message") , arg1.getStringExtra("notificationID")); 

      //generateNotification(arg0, arg1.getStringExtra("key1")); 
     } 

     @Override 
     protected void onRegistered(Context arg0, String arg1) { 
      // TODO Auto-generated method stub 


     } 

     @Override 
     protected void onUnregistered(Context arg0, String arg1) { 
      // TODO Auto-generated method stub 

     } 

    } 

在這裏,在下面的線上,我越來越老有效notificationID,但每次我提出新的ID 。

generateNotification(arg0,arg1.getStringExtra(「message」),arg1.getStringExtra(「notificationID」));

如果出現問題,請幫我解決問題。

+0

更因爲它的服務器部分PHP問題似乎被誤認爲的.... –

回答

9

這是一個常見的問題,懸而未決的意圖沒有被更新, 只是改變這樣的:

PendingIntent intent = PendingIntent.getActivity(context, 0, 
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
+0

謝謝!!!工作正常。 – gauravsanu

+0

請接受答案,可能對其他人有幫助。 –

+0

謝謝。實施和工作正常。 – gauravsanu