使用 Google Firebase 和 Phonegap 推送通知
Push notifications using Google Firebase with Phonegap
我正在尝试制作一个简单的测试应用程序来测试推送通知。
我已经安装了插件 "phonegap-plugin-push" (https://github.com/phonegap/phonegap-plugin-push),并遵循了所有安装说明。
我在设备就绪时收到注册 ID,当我使用 php 脚本向该 ID 发送通知时,它 returns 来自 google [=40= 的成功响应],但是移动设备上没有出现任何通知。
有什么错误的建议吗?
index.html
<!DOCTYPE html>
<html>
<head>
<title>Push demo</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var push = PushNotification.init({ "android": {"senderID": "my sender id"}});
push.on('registration', function(data) {
console.log(data.registrationId);
});
push.on('notification', function(data) {
console.log(data.title + " Message: " + data.message);
});
push.on('error', function(e) {
console.log(e);
});
}
</script>
</head>
<body>
<h1>Push Notification Example</h1>
</body>
</html>
pushTest.php
<?php
define('API_ACCESS_KEY', 'secret key here');
$registrationIds = array(
'long device id here'
);
$msg = array(
'message' => 'Test msg',
'title' => 'Test title',
'subtitle' => 'Test subtitle',
'vibrate' => 1,
'sound' => 1
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
PHP 回应
{"multicast_id":xxxxxx,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:xxxxxxxxxx"}]}
GCM 不再有效.
FCM 是新的 GCM.
在我的应用程序中,我使用 this plugin 在 Android/iOS 上发送推送通知。
您可以阅读 this page 了解如何从服务器端发送推送通知。
我尝试了几件事,尝试了 "cordova-plugin-firebase" 插件和 "cordova-plugin-fcm",但是我得到了一个错误,这两个插件都相同(不记得确切的错误消息)但是问题是我只将 "google-services.json" (android) 添加到项目的根目录,这是我从 firebase 控制台获得的,而不是 "GoogleService-Info.plist" (iOS).
然而我最终还是使用了插件"phonegap-plugin-push"。我用 phonegaps 推送模板重新创建了项目,突然间它似乎工作了。
如果您正在为 iOS 设置推送通知,请不要忘记在 firebase 控制台中上传您的推送证书,如果您正在为 ios 启用 gcm 推送。
我正在尝试制作一个简单的测试应用程序来测试推送通知。
我已经安装了插件 "phonegap-plugin-push" (https://github.com/phonegap/phonegap-plugin-push),并遵循了所有安装说明。
我在设备就绪时收到注册 ID,当我使用 php 脚本向该 ID 发送通知时,它 returns 来自 google [=40= 的成功响应],但是移动设备上没有出现任何通知。
有什么错误的建议吗?
index.html
<!DOCTYPE html>
<html>
<head>
<title>Push demo</title>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
var push = PushNotification.init({ "android": {"senderID": "my sender id"}});
push.on('registration', function(data) {
console.log(data.registrationId);
});
push.on('notification', function(data) {
console.log(data.title + " Message: " + data.message);
});
push.on('error', function(e) {
console.log(e);
});
}
</script>
</head>
<body>
<h1>Push Notification Example</h1>
</body>
</html>
pushTest.php
<?php
define('API_ACCESS_KEY', 'secret key here');
$registrationIds = array(
'long device id here'
);
$msg = array(
'message' => 'Test msg',
'title' => 'Test title',
'subtitle' => 'Test subtitle',
'vibrate' => 1,
'sound' => 1
);
$fields = array(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type:application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
PHP 回应
{"multicast_id":xxxxxx,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:xxxxxxxxxx"}]}
GCM 不再有效.
FCM 是新的 GCM.
在我的应用程序中,我使用 this plugin 在 Android/iOS 上发送推送通知。
您可以阅读 this page 了解如何从服务器端发送推送通知。
我尝试了几件事,尝试了 "cordova-plugin-firebase" 插件和 "cordova-plugin-fcm",但是我得到了一个错误,这两个插件都相同(不记得确切的错误消息)但是问题是我只将 "google-services.json" (android) 添加到项目的根目录,这是我从 firebase 控制台获得的,而不是 "GoogleService-Info.plist" (iOS).
然而我最终还是使用了插件"phonegap-plugin-push"。我用 phonegaps 推送模板重新创建了项目,突然间它似乎工作了。
如果您正在为 iOS 设置推送通知,请不要忘记在 firebase 控制台中上传您的推送证书,如果您正在为 ios 启用 gcm 推送。