2016-01-16 29 views
1

我試圖通過GCM發送推送通知,但我沒有得到通知:成功:1仍然在android系統

{ 
    "multicast_id":4793018731783267982, 
    "success":1, 
    "failure":0, 
    "canonical_ids":0, 
    "results": [ 
    { "message_id":"0:1452928098906665%a69ccee8f9fd7ecd" } 
    ] 
} 

,但仍然沒有通知即將到來。這裏是我的代碼:

//index.php code 

$app->get('/notify',function() use($app) 
     { 
        $response = array(); 
        $regId="device registration id"; //hard coded 
        $message="hi everyone.this is to notify you" ; 
        $registatoin_ids = array($regId); 
        $message = array("price" => $message); 
        $db = new DbHandler(); 
        $result = $db->send_notification($registatoin_ids, $message); 
        echoRespnse (200, $result); 

      }); 

//Dbhandler code 

public function send_notification($registatoin_ids, $message) { 


    $url = 'https://android.googleapis.com/gcm/send'; 

    $fields = array(
     'registration_ids' => $registatoin_ids, 
     'data' => $message, 
    ); 

    $headers = array(
     'Authorization: key=AIzaSyByR3xuTkhB-OZSNTlQqlwnqsLBzqXUWb0', //server key 
     'Content-Type: application/json' 
    ); 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $url); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Disabling SSL Certificate support temporarly 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    // Execute post 
    $result = curl_exec($ch); 
    if ($result === FALSE) { 
     die('Curl failed: ' . curl_error($ch)); 
    } 

    // Close connection 
    curl_close($ch); 
    return $result; 
} 

注:我是直接從其他先進的客戶端運行此API,而不是機器人device.I的我得到響應成功,但對設備,其REG_ID我已經硬編碼here.Please幫助任何通知

編輯================= 我已經寫在Android結束這段代碼:

if (checkPlayServices()) 
{ 
    gcm = GoogleCloudMessaging.getInstance(this); 

    if (regid.isEmpty()) 
    { 
     new register().execute(); 
    } 
    else 
    { 
     Log.i("hello", "No valid Google Play Services APK found."); 
    } 
} 

public class register extends AsyncTask<Void, Integer, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     String msg = ""; 
     try 
     { 
       if (gcm == null) 
       { 
        gcm = GoogleCloudMessaging.getInstance(getApplicationContext()); 
       } 
       regid = gcm.register(AppConst.SENDER_ID);    
       Log.i("hello", "Current Device's Registration ID is: "+regid);  
     } 
     catch (IOException ex) 
     { 
      msg = "Error :" + ex.getMessage(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
    } 
} 

在Mainfest我已經加入:

 <receiver 
     android:name=".GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" 
     > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.example" /> 
     </intent-filter> 

    </receiver> 
+0

具有u編寫的代碼,以獲得在烏拉圭回合Android應用的通知? – Rakesh

+0

看到我編輯的代碼 –

+0

擁有''成功':1'並不意味着消息已經成功發送。它在[documentation](https://developers.google.com/cloud-messaging/http-server-ref#interpret-downstream)中聲明消息已經被處理而沒有錯誤,這可能意味着消息排隊等候發送。你可以參考這個[線程](http://stackoverflow.com/questions/13929790/ive-got-this-response-from-the-gcm-server-success1-but-the-notification-no),它可以有幫助,可能會回答你的問題。 – gerardnimo

回答

0

手柄在Android設備上的通知:

@Override 
public void onMessageReceived(String from, Bundle data) { 
    String message = data.getString("message"); 
    Log.d(TAG, "From: " + from); 
    Log.d(TAG, "Message: " + message); 
    // Handle received message here. 
} 

欲瞭解更多信息,請訪問:https://developers.google.com/cloud-messaging/