[英]Push notifications using Google Firebase with Phonegap
我正在嘗試制作一個簡單的測試應用程序來測試推送通知。
我已經安裝了插件“phonegap-plugin-push”( https://github.com/phonegap/phonegap-plugin-push ),並按照所有安裝說明進行操作。
我在設備上收到了注冊ID,當我使用php腳本向該ID發送通知時,它會從google api返回成功響應,但移動設備上沒有通知。
什么可能是錯的任何建議?
的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"}]}
我嘗試了幾個東西,嘗試了“cordova-plugin-firebase”插件和“cordova-plugin-fcm”,但我收到了兩個插件相同的錯誤(不記得確切的錯誤信息)但問題是那個我只在項目的根目錄中添加了“google-services.json”(android),這是我從firebase控制台獲得的,而不是“GoogleService-Info.plist”(iOS)。
但實際上我最終使用了插件“phonegap-plugin-push”。 我用phonegaps推送模板重新創建了項目,突然間它似乎工作了。
如果您要為iOS設置推送通知,請不要忘記在firebase控制台中上傳推送證書,如果您正在啟用gos push for ios。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.