0

I was sending message to user. The message was succesffuly sent. But i am not reciving notification in android mobile. I have code:-

enter code here
public function send_android_notification($device,$message,$user,$sender_id,$reciever_id,$list_id,$username)  
{
        define( 'API_ACCESS_KEY', 'My key(i already embed)' );
        $registrationIds = array( $device );
        // prep the bundle
        $msg = array
        (
        'message'   => $message['Message']['message'],
        'user'=>$user,
        'sender_id' =>$sender_id,
        'reciever_id' =>$reciever_id,
        'list_id'=>$list_id,
        'username' =>$username
        );
        $fields = array
        (
            'registration_ids'  => $registrationIds,
            'data'          => $msg
        );

        $headers = array
        (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://android.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_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
        echo $result;
    }

Can anyone help me. I t gives me output:- enter image description here

{"multicast_id":8119665273981115453,"success":0,"failure":1,"canonical_ids":0

kunal
  • 4,122
  • 12
  • 40
  • 75

1 Answers1

1
?php 
function sendPushNotificationToGCM($device, $message,$user,$sender_id,$reciever_id,$list_id,$username) {
        //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $device,
            'data' => $msg = array (
                  'message'   => $message['Message']['message'],
                  'user'=>$user,
                  'sender_id' =>$sender_id,
                  'reciever_id' =>$reciever_id,
                  'list_id'=>$list_id,
                  'username' =>$username
                  ),
        );

        $headers = array(
            'Authorization: key=youKey',
            '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, json_encode($fields));
        $result = curl_exec($ch);               
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
        curl_close($ch);
        return $result;
    } ?>

please try this. This is working in my case. please have a try. And also check in your your device id is fetching the correct way or not.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • please see this http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging – Pranav MS Nov 18 '16 at 05:43
  • now i find the problem i am sending wrong dvice_type i.e the notification send to android is going to ios and ios notiification send to android anyways thank you for help – kunal Nov 18 '16 at 13:06