适用于 Android 的 React Native 推送通知

Thi*_*vya 4 android push-notification react-native

过去 2 周,我们一直坚持使用 react-native 中的 android 推送通知,并且我们尝试使用以下 react native 模块

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

使用上述模块,我们能够获得本地通知(来自应用程序的静态通知),该通知有效但未显示来自服务器的通知。我们已经尝试过“ https://github.com/oney/react-native-gcm-android ”这也..

能够注册 GCM 并从 GCM 获取令牌,但使用该注册令牌,无法获得通知和

我们正在使用 php 从服务器发送通知,php 代码如下

这是我们用来从服务器发送通知的代码,

<?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;
  }
 ?>
Run Code Online (Sandbox Code Playgroud)

我们怎样才能克服这个问题?

Khi*_*yat 5

尝试以下php代码

<?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;
    }   
?>
Run Code Online (Sandbox Code Playgroud)