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:
我们该如何克服这个问题?
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;
}
?>
我们该如何克服这个问题?