React Native Push Notification for Android

2019-03-31 11:40发布

问题:

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 ?

回答1:

For testing create Browser key and don't give any package name for security while creating broswer key and use that one



回答2:

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


回答3:

Some devices will not support push notification sometimes and delay receiving notifications.So please check with several devices.So that you can get the answer.



回答4:

Php code which you provided is server side, right?

First check push notification send from FCM server is working in app or not?

New Cloud Messaging projects must create a Firebase project in the Firebase console. In this process, you'll generate a configuration file and credentials for your project.

1.Create a Firebase project in the Firebase console, if you don't already have one. If you already have an existing Google project associated with your mobile app, click Import Google Project. Otherwise, click Create New Project.

2.Click Add Firebase to your Android app and follow the setup steps. If you're importing an existing Google project, this may happen automatically and you can just download the config file.

3.When prompted, enter your app's package name. It's important to enter the package name your app is using; this can only be set when you add an app to your Firebase project.

4.At the end, you'll download a google-services.json file. You can download this file again at any time.

5.If you haven't done so already, copy this into your project's module folder, typically app/.

Once you follow all the steps then click on Notification option which is present at the Leftmost side under Grow option.

Then you can click on Send your first message and start sending push notifications.

Select a single device and enter FCM registration token and click on send a message.

If you still not getting push notification then check did you follow all steps which are provided by a react-native module. Don't forget to add google-services.json file inside android/app folder.

It works for me.

IF you are getting notifications, then there is some issue with the server-side code.