Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Push Notification for Android

We have stuckup with android push notification in react-native for past 2 weeks and also we have tried with following react native module

https://www.npmjs.com/package/react-native-push-notification

With above module, we are able to get local notification ( static from app ), that is working but notification from server is not displaying. we have tried " https://github.com/oney/react-native-gcm-android " this also ..

Able to register with GCM and get token from GCM but using that registered token ,Cannot get Notification and

we are using php to send notification from server and the php code is below

This is the code we are using to send notification from server,

<?php
function sendPushNotificationToGCM($registatoin_ids, $message) {
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array('registration_ids' => $registatoin_ids, 'data' => array("title" => 'hi', "message" => $message, ),  );
 define("GOOGLE_API_KEY", "YOUR API KEY");       
  $headers = array(
    'Authorization: key=' . GOOGLE_API_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);
 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;
  }
 ?>

How can we overcome this ?

like image 721
Thivya Avatar asked Feb 08 '23 12:02

Thivya


1 Answers

Try following php code

<?php
    //Generic php function to send GCM push notification
   function sendMessageThroughGCM($registatoin_ids, $message) {
        //Google cloud messaging GCM-API url
        $url = 'https://android.googleapis.com/gcm/send';
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
        // Update your Google Cloud Messaging API Key
        define("GOOGLE_API_KEY", "Browswer Key");       
        $headers = array(
            'Authorization: key=' . GOOGLE_API_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);
        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;
    }
?>
<?php

    //Post message to GCM when submitted
    $pushStatus = "GCM Status Message will appear here";    
    if(!empty($_GET["push"])) { 
        $gcmRegID  = file_get_contents("GCMRegId.txt");
        $pushMessage = $_POST["message"];   
        if (isset($gcmRegID) && isset($pushMessage)) {      
            $gcmRegIds = array($gcmRegID);
            $message = array("m" => $pushMessage);  
            $pushStatus = sendMessageThroughGCM($gcmRegIds, $message);
        }       
    }

    //Get Reg ID sent from Android App and store it in text file
    if(!empty($_GET["shareRegId"])) {
        $gcmRegID  = $_POST["regId"]; 
        file_put_contents("GCMRegId.txt",$gcmRegID);
        echo "Done!";
        exit;
    }   
?>
like image 190
Khizar Hayat Avatar answered Feb 10 '23 22:02

Khizar Hayat