Android phonegap send push notification using Google Cloud Messaging using php

给你一囗甜甜゛ 提交于 2019-12-13 02:56:16

问题


I have developed the android application in cordova 3.4 now I want to get the automatic notification to their mobile. For this I refer websites as below

  • http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/
  • http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/
  • https://github.com/hollyschinsky/PushNotificationSample30/tree/master/platforms/android

I read all above links then did all stuff they told like create project on google cloud messaging , get the project id , create server key (In textbox I given my private server IP) then API Key, I also have registration id device and I wrote code in php for sending push notification to my device but it is giving me "unauthorised Error 404"

my code is as below

    define( 'API_ACCESS_KEY', 'my api key' );

    //$registrationIds = array( $_GET['id'] );
    $registrationIds = array($id);
    //var_dump($registrationIds);die;
    //prep the bundle
    $msg = array
    (
            'message'       => 'New Jobs is updated',
            'title'         => 'get the job in cad',
            'subtitle'      => 'get the job in cad',
            'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
            'vibrate'   => 1,
            'sound'     => 1
    );

    $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;

I just hit url and send the registration id of device to the my php page but this page give me the error unauthorised Error 401.

Que.1. Is this possible to send the push notification from php which is hosted on private server?

Que.2 Is device registration id compulsory for sending push notification to those user they installed the apps?

Please anybody help me how to solve this above error and if anybody have another solution then tell me. I tried from yesterday but not achieve my goal so please help me.


回答1:


Here is the GCM code and its working properly with my device though its in CI framework but hope you can use it. It is compulsory to add device_id for the push and also the passphrase you'll get for the site. just try it.

function sendNotification($device_id,$message) {
        /*echo $device_id;
        echo "<br>";
        echo $message;*/

        // note: you have to specify API key in config before
        $this->load->library('gcm');
        // simple adding message. You can also add message in the data,
        // but if you specified it with setMesage() already
        // then setMessage's messages will have bigger priority
        $msg = $this->gcm->setMessage($message);
        //var_dump($message);

        // add recepient or few
        $this->gcm->addRecepient($device_id);
        //var_dump($device_id);
        // set additional data
        //$this->gcm->setData($params);
        // also you can add time to live
        $this->gcm->setTtl(500);
        // and unset in further
        //$this->gcm->setTtl(false);

        // set group for messages if needed
        //$this->gcm->setGroup('Test');
        // or set to default
        //$this->gcm->setGroup(false);
        // send
        return $this->gcm->send();
    }

Hope it'll solve your problem.




回答2:


<?php 
$register_keys = array("YOUR_REGISTRY_KEYS")
$google_api_key = "YOUR_API_KEY";
$registatoin_ids = $register_keys; 
$message = array("price" => $message);  
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);              
$headers = array(
'Authorization: key=' .$google_api_key,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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));
}
//print_r($result);
// Close connection
curl_close($ch);
?>


来源:https://stackoverflow.com/questions/23677313/android-phonegap-send-push-notification-using-google-cloud-messaging-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!