Android 的 React Native 推送通知
React Native Push Notification for Android
过去 2 周我们一直在使用 android react-native 中的推送通知,我们还尝试使用以下 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;
}
?>
我们如何克服这个问题?
尝试以下 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;
}
?>
有些设备有时不支持推送通知,延迟接收notifications.So请与几个devices.So联系,您可以得到答案。
为了测试,创建浏览器密钥并且在创建浏览器密钥并使用该密钥时不要提供任何包名称以确保安全
Php 你提供的代码是服务端的吧?
首先检查从 FCM 服务器发送的推送通知是否在应用程序中运行?
新的云消息传递项目必须在 Firebase console 中创建一个 Firebase 项目。在此过程中,您将为项目生成配置文件和凭据。
1.Create Firebase console 中的一个 Firebase 项目,如果您还没有的话。如果您已有一个与您的移动应用关联的现有 Google 项目,请单击导入 Google 项目。否则,单击创建新项目。
2.Click 将 Firebase 添加到您的 Android 应用并按照设置步骤操作。如果您要导入现有的 Google 项目,这可能会自动发生,您只需 download the config file.
3.When 提示,输入您的应用程序包名称。输入您的应用程序使用的包名称很重要;这只能在您将应用程序添加到您的 Firebase 项目时设置。
4.At最后,你会下载一个google-services.json文件。您可以随时再次 download this file。
5.If 你还没有这样做,将它复制到你项目的模块文件夹中,通常是 app/。
完成所有步骤后,单击“增长”选项下最左侧的“通知”选项。
然后您可以点击发送您的第一条消息并开始发送推送通知。
Select单机输入FCM注册令牌点击发送消息
如果您仍然没有收到推送通知,请检查您是否遵循了 react-native 模块提供的所有步骤。
不要忘记在 android/app 文件夹中添加 google-services.json 文件。
对我有用。
如果您收到通知,则表明服务器端代码存在问题。
过去 2 周我们一直在使用 android react-native 中的推送通知,我们还尝试使用以下 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;
}
?>
我们如何克服这个问题?
尝试以下 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;
}
?>
有些设备有时不支持推送通知,延迟接收notifications.So请与几个devices.So联系,您可以得到答案。
为了测试,创建浏览器密钥并且在创建浏览器密钥并使用该密钥时不要提供任何包名称以确保安全
Php 你提供的代码是服务端的吧?
首先检查从 FCM 服务器发送的推送通知是否在应用程序中运行?
新的云消息传递项目必须在 Firebase console 中创建一个 Firebase 项目。在此过程中,您将为项目生成配置文件和凭据。
1.Create Firebase console 中的一个 Firebase 项目,如果您还没有的话。如果您已有一个与您的移动应用关联的现有 Google 项目,请单击导入 Google 项目。否则,单击创建新项目。
2.Click 将 Firebase 添加到您的 Android 应用并按照设置步骤操作。如果您要导入现有的 Google 项目,这可能会自动发生,您只需 download the config file.
3.When 提示,输入您的应用程序包名称。输入您的应用程序使用的包名称很重要;这只能在您将应用程序添加到您的 Firebase 项目时设置。
4.At最后,你会下载一个google-services.json文件。您可以随时再次 download this file。
5.If 你还没有这样做,将它复制到你项目的模块文件夹中,通常是 app/。
完成所有步骤后,单击“增长”选项下最左侧的“通知”选项。
然后您可以点击发送您的第一条消息并开始发送推送通知。
Select单机输入FCM注册令牌点击发送消息
如果您仍然没有收到推送通知,请检查您是否遵循了 react-native 模块提供的所有步骤。 不要忘记在 android/app 文件夹中添加 google-services.json 文件。
对我有用。
如果您收到通知,则表明服务器端代码存在问题。