React Native安卓推送通知

4
We have been struggling with Android push notification in React-Native for the past two weeks. We have tried using the following React Native module: https://www.npmjs.com/package/react-native-push-notification. With this module, we are able to receive local notifications from the app, but notifications from the server are not displaying. We also tried using the https://github.com/oney/react-native-gcm-android module. Although we were able to register with GCM and obtain a token, we are unable to receive notifications using that token.
We are using PHP to send notifications from the server. Below is the code we are currently using to send notifications:
<?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;
  }
 ?>

我们该如何克服这个问题?

你是在使用从console.google.com获取的Server key、Browser key还是Android key? - Khizar Hayat
为了测试,请创建浏览器密钥,并在创建浏览器密钥时不提供任何包名称以确保安全,并使用该密钥。 - Khizar Hayat
好的,我会尝试并让您知道。 - Thivya
我已经按照你说的尝试使用浏览器密钥,但仍然无法从服务器接收通知。 - Thivya
4个回答

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;
    }   
?>

我已经在上面发布了相同的内容,尝试过这个方法,但服务器仍未发送任何通知。 - Thivya
最后一个 PHP 标签不同。 - Khizar Hayat
嗯,好的,我是安卓开发者。我在我的应用程序中使用了相同的PHP。所以除此之外,很抱歉我无法帮助你。 - Khizar Hayat
嗨,那是因为我检查过的设备,现在我已经用另一个设备进行了检查(推送通知在这个设备上可以工作)...谢谢 :-)(正如你所说,我已经尝试使用浏览器密钥) - Thivya
哇,太棒了。如果我把它作为答案发布,你能接受吗? - Khizar Hayat
没问题,Khizar :-) - Thivya

2

有时候,某些设备可能不支持推送通知并且会延迟接收通知。因此,请使用多个设备进行检查以便获得答案。


1
创建浏览器密钥进行测试,不要在创建浏览器密钥时提供任何包名称以确保安全,并使用该密钥。

0

您提供的Php代码是服务器端的,对吗?

首先检查从FCM服务器发送的推送通知是否在应用程序中工作?

新的云消息传递项目必须在Firebase控制台中创建Firebase项目。在此过程中,您将为您的项目生成配置文件和凭据。

1.如果您还没有Firebase项目,请在Firebase控制台中创建一个。如果您已经有一个与您的移动应用相关联的现有Google项目,请单击导入Google项目。否则,请单击创建新项目。

2.单击将Firebase添加到您的Android应用程序并按照设置步骤进行操作。如果您正在导入现有的Google项目,则可能会自动发生,并且您只需下载配置文件即可。

3.在提示时输入您的应用程序包名称。输入您的应用程序正在使用的包名称很重要;这只能在将应用程序添加到Firebase项目时设置。

4.最后,您将下载一个google-services.json文件。您可以随时再次下载此文件

5.如果您还没有这样做,请将其复制到项目的模块文件夹中,通常为app/。

完成所有步骤后,单击“Grow”选项下最左侧的“Notification”选项。

enter image description here

然后,您可以单击“发送您的第一条消息”并开始发送推送通知。
选择单个设备,输入 FCM 注册令牌,然后单击“发送消息”。
如果您仍未收到推送通知,请检查您是否按照 React Native 模块提供的所有步骤操作。 不要忘记在 android/app 文件夹中添加 google-services.json 文件。
对我有效。
如果您收到通知,则表示服务器端代码存在某些问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接