【问题标题】:Push notifications using Google Firebase with Phonegap使用带有 Phonegap 的 Google Firebase 推送通知
【发布时间】:2017-05-31 14:40:49
【问题描述】:

我正在尝试制作一个简单的测试应用程序来测试推送通知。

我已经安装了插件“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"}]}

【问题讨论】:

    标签: php android cordova google-cloud-messaging


    【解决方案1】:

    GCM 不再有效

    FCM 是新的 GCM

    在我的应用程序中,我使用this plugin 在 Android/iOS 上发送推送通知。

    您可以阅读this page 了解如何从服务器端发送推送通知。

    【讨论】:

    • 我使用 FCM,但使用了 GCM 插件 (phonegap-plugin-push),它现在可以正常工作了。我使用相同的设置并在上面写过,唯一不同的是我使用 phonegaps 推送模板重新安装了我的应用程序。我已经在 Firebase 控制台中为 iOS 激活了 GCM 推送。
    【解决方案2】:

    我尝试了几件事,尝试了“cordova-plugin-firebase”插件和“cordova-plugin-fcm”,但我得到了两个插件相同的错误(不记得确切的错误消息)但是问题是我只将“google-services.json”(android)添加到项目的根目录,这是我从firebase控制台获得的,而不是“GoogleService-Info.plist”(iOS)。

    但我实际上最终使用了插件“phonegap-plugin-push”。我用 phonegaps 推送模板重新创建了项目,突然它似乎工作了。

    如果您正在为 iOS 设置推送通知,如果您正在为 iOS 启用 gcm 推送,请不要忘记在 firebase 控制台中上传您的推送证书。

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      相关资源
      最近更新 更多