FCM返回成功但Ionic V1设备未收到通知

4
I am using FIREBASE CLOUD MESSAGING service with the phonegap-plugin-push cordova plugin for my ionic product to receive push notifications from a PHP back-end.
When attempting to receive push notifications, the PHP end receives the following successful result:
Sample Push Data Payload:
{"multicast_id":8853634389214913500,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1495614850271706%39688dd8f9fd7ecd"}]}
Technology specification:
  • cordova push notification plugin version :1.9.4

  • Platform and Version: Ionic V1

  • Ionic CLI version : 2.1.13

  • Cordova version : cordova --6.4.0

  • Android platform for cordova :6.0.0

  • Android) What device vendor I have tested : Samsung, HUWAWEI, Xiaomi etc.

  • Sample Code that illustrates the problem as below

    IONIC PART:

    //Push Notification if (window.cordova) { if (!localStorage.getItem('device_token')) { var apkId = 0; var iosId = 0; var options = { android: { senderID: MY FCM SENDER ID, icon: "alert", }, ios: { alert: "true", badge: "true", sound: "true" }, windows: {} };

        //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
        });
    

    // push notification end

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 - 你解决了这个问题吗?我也成功收到响应,但是根本没有收到通知。 - tamak
我通过检查令牌大小解决了这个问题。在我的情况下,由于 SQL 字段长度的限制,我将令牌保存时修剪了一些字符串。增加令牌长度后,令牌被正确保存并正常工作!因此,请先检查您注册和发送的令牌!谢谢! - Toriqul Islam Tareq
1个回答

8

将通知添加到数组中对我有用。谢谢伙计。+1 - Sahil Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接