【问题标题】:FCM give success but Notification is not received by device in Ionic V1FCM 成功,但 Ionic V1 中的设备未收到通知
【发布时间】:2017-10-24 13:01:08
【问题描述】:

我将 FIREBASE CLOUD MESSAGING 服务与我的 ionic 产品和 phonegap-plugin-push cordova 插件一起使用,以从 PHP BACK END 获取推送通知。

当我尝试获取推送通知时,php 端将获得如下成功的结果。

示例推送数据负载

{"multicast_id":8853634389214913500,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1495614850271706%39688dd8f9fd7ecd"}]}

技术规范:

  • cordova 推送通知插件版本:1.9.4

  • 平台和版本:Ionic V1

  • Ionic CLI 版本:2.1.13

  • Cordova 版本:cordova --6.4.0

  • 适用于cordova的Android平台:6.0.0

  • Android) 我测试过的设备供应商:三星、华为、小米 等等。

  • 说明问题的示例代码如下

    离子部分:

    //推送通知 如果(window.cordova){ if (!localStorage.getItem('device_token')) { var apkId = 0; 变量 iosId = 0; 变量选项 = { 安卓: { senderID:我的 FCM 发件人 ID, 图标:“警报”, }, IOS:{ 警报:“真实”, 徽章:“真实”, 声音:“真实” }, 窗口:{} };

        //localStorage.getItem('gcmRegId')
        // initialize
        $cordovaPushV5.initialize(options).then(function () {
            // start listening for new notifications
            $cordovaPushV5.onNotification();
            // start listening for errors
            $cordovaPushV5.onError();
    
    
            // register to get registrationId
            $cordovaPushV5.register().then(function (data) {
                //alert("GCM"+data);
                // if Android device.
                if (ionic.Platform.isAndroid()) {
                    apkId = data;
                }
                // if ios device.
                if (ionic.Platform.isIOS()) {
                    iosId = data;
                }
                // Updating member details with apkId or iosId
                var pushParams = {
                    'app_token': Config.appToken,
                    'device_uiu_token': device.uuid,
                    'apk_token': apkId,
                    'ios_token': iosId
                }
                $http.post(Config.apiUrl + "member/save_token", pushParams)
                    .success(function (data) {
                        if (data.status == 200) {
                            localStorage.setItem("device_token", device.uuid);
    
                        }
                        /* else{
                         alert("Sorry!Error occurs!");
                         } */
                    });
            })
            // Updating end.
        });
    
        // triggered every time notification received
        $rootScope.$on('$cordovaPushV5:notificationReceived', function (event, data) {
            alert("recieved" + JSON.stringify(data));
            // data.message,
            // data.title,
            // data.count,
            // data.sound,
            // data.image,
            // data.additionalData
        });
    
        // triggered every time error occurs
        $rootScope.$on('$cordovaPushV5:errorOcurred', function (event, e) {
            alert('push ERROR' + e.message);
            // e.message
        });
    

    //推送通知结束

PHP 部分:

$push_title = $this->input->post('push_title');
$push_msg = $this->input->post('push_msg');
$members = $this->members_model->get_members();
$apk_tokens = array();
$ios_tokens = array();
foreach ($members as $member) {
    if ($member['apk_token'] != 0 || $member['apk_token'] != "") {
        array_push($apk_tokens, $member['apk_token']);
    }
    if ($member['ios_token'] != 0 || $member['ios_token'] != "") {
        array_push($ios_tokens, $member['ios_token']);
    }
}
//Sending the push notification using GCM.
$msg = array(
    'message' => $push_msg,
    'title' => $push_title,
    'vibrate' => 1,
    'sound' => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon',
);

$fields = array
(
    'registration_ids' => $apk_tokens,
    'data' => $msg,
    'priority' => 'high'
);

$headers = array
(
    'Authorization: MY FCM SERVER KEY',
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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;

提前致谢!

【问题讨论】:

  • @KENdi - 你解决过这个问题吗?我的回复也成功了,但根本没有收到通知。
  • 我已经通过检查令牌大小解决了。对于我的情况,由于 sql 字段长度,我通过修剪更多字符串来保存令牌。增加该令牌后正确保存 n 工作正常!因此,请先检查您注册的 n 发送令牌!谢谢!

标签: php cordova ionic-framework firebase-cloud-messaging phonegap-pushplugin


【解决方案1】:

如果您想在锁定屏幕上显示通知,请在$fields 中使用notification。该对象需要titlebody 元素。

https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol

$fields = array
(
    'registration_ids' => $apk_tokens,
    'data' => $msg,
    'priority' => 'high',
    'notification' => array(
        'title' => 'This is title',
        'body' => 'This is body'
    )
);

我没有尝试此代码,但 node.js SDK 的相同问题已通过这种方式修复。

【讨论】:

  • 在数组中添加通知对我有用。谢了哥们。 +1
猜你喜欢
  • 2020-04-25
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2018-10-09
  • 2017-09-15
  • 1970-01-01
  • 2021-09-08
  • 2021-10-30
相关资源
最近更新 更多