2017-08-22 9 views
0

私はfirebase fcmを使用してアプリ内のアンドロイドユーザーにプッシュ通知を送信しています。通常、プッシュ通知は正常に動作します。しかし、スワイプでメッセージを開いたり閉じたりした回数に関係なく、いつも同じメッセージが繰り返し表示されます。インターネットから切断されたときにメッセージが停止するので、アプリケーションに何も問題はありません。私はそれがいつか起こるだけで、それがfirebaseの不具合か私のコードの問題かどうか混乱しています。返信用firebase fcm 1つだけが送信されても​​複数のプッシュ通知を送信

public function android_notification_by_fcm($android_users, $message) 
{ 
    $device_tokens = array(); 
    if (!empty($android_users)) { 
     foreach ($android_users as $android_use) { 
      array_push($device_tokens, $android_use->reg_id); 
     } 

     $url = "https://fcm.googleapis.com/fcm/send"; 
     // $notification = ""; 
     $fields = array(
      'registration_ids' => $device_tokens, 
      'data' => $message 
     ); 


     $fields = json_encode($fields); 
     $headers = array(
      'Authorization:key = secretkey', 
      '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); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); 
     $result = json_decode(curl_exec($ch)); 
     if ($result) { 
      foreach ($result->results as $res) { 
       if (isset($res->error)) { 
        \Log::warning('android sending notification for detail version failed due to: ' . $res->error); 
       } else { 
        \Log::info('android sending notification for detail version success. Message id is: ' . $res->message_id); 
       } 
      } 
     } 
     curl_close($ch); 
     return 1; 

    } else { 
     return 2; 
    } 


} 

答えて

-1

`

function sendNotification($id, $message, $title='', $subTitle='', $tickerText='', $ids){ 
    if(count($ids)){// ids is an array 
     $msg = array 
     (
      'id' => $id, 
     'message'  => $message, 
     'title'  => $title, 
     'subtitle' => $subTitle, 
     'tickerText' => $tickerText, 
     'priority' => 'high', 
     'vibrate' => 1, 
     'sound'  => 1); 
     $post = array 
     (
      'registration_ids' => $ids, 
      'data'    => $msg, 
     'priority' => 'high', 
      'notification' => array('sound' => 'default', 'title' => $title,'body' => $tickerText) 
     ); 
     /* API_ACCESS_KEY: is a const contain API access key from google */ 
     $headers = array 
     ( 
      'Authorization: key=' . API_ACCESS_KEY, 
      'Content-Type: application/json' 
     ); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send'); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); 
     $result = curl_exec($ch); 
     if (curl_errno($ch)){ 
      echo 'GCM error: ' . curl_error($ch); 
     } 
     curl_close($ch); 
     return $result; 
    } 
} 

`

+1

ありがとう:

は、ここでプッシュ通知を送信するために使用されるコードです。しかし、これはどうやって問題を解決するのか。私は明らかに、私が質問に付けたコードが動作することを書いていた。あなたの答えを説明できますか? – Andrew

関連する問題