【问题标题】:Trying to send FCM push to Android and iOS尝试将 FCM 推送发送到 Android 和 iOS
【发布时间】:2018-04-17 01:41:30
【问题描述】:

尝试将 FCM 推送发送到 Android 和 iOS。 Android 发送正常,但不会发送到 iOS。添加了发送到 iOS 的新功能。现在我在 iOS 和 {"multicast_id":8264919538226996033,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"},{"message_id":"0:1523928629201044%3990eb5d3990eb5d"}]} 上得到响应序列化失败@

在 android 上用于任何包括苹果推送的调用。我也偶尔,但不经常,两次推送到 android,但从来没有两次推送到苹果。这是一个示例调用,Apple 行是导致错误的原因,即使它发送了所有内容。

$json = $push->getPush();
$firebase->sendMultiple($tokens, $json);
$firebase->sendPushNotificationApple($tokens, $payload["title"], $payload["message"], $payload);

另一个文件中的推送函数

public function getPush() {
            $res = array();
            $res['title']       = $this->title;
            $res['message']     = $this->message;
            $res['image']       = $this->image;
            $res['payload']     = $this->data;
            $res['type']        = $this->type;
            $res['timestamp']   = date("T-m-d G:i:s");
            $res['text']        = $this->message;
            $res['sound']       = "default";
            $res['badge']       = "1";
            return $res;
        }
// sending push message to multiple users by firebase registration ids
    public function sendMultiple($registration_ids, $message) {
        $fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
            'priority'=>'high',
        );

    return $this->sendPushNotification($fields);
}

// function makes curl request to firebase servers
public function sendPushNotification($fields) {

    define("FIREBASE_API_KEY", "removed");

    // Set POST variables
    $url = 'https://fcm.googleapis.com/fcm/send';

    $headers = array(
        'Authorization: key=' . FIREBASE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);

    return $result;
}

public function sendPushNotificationApple($registration_ids, $title, $text, $payload) {

    $url = "https://fcm.googleapis.com/fcm/send";
    $serverKey = 'removed';
    $notification = array('title' =>$title , 'text' => $text, 'sound' => 'default', 'badge' => '1', 'payload' => $payload);
    $arrayToSend = array('registration_ids' => $registration_ids, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
}

【问题讨论】:

  • 您在 iOS 上收到来自 GoogleFCM 仪表板的测试推送通知了吗?
  • 是的,我也收到了来自这个脚本的 iOS 通知,它只是在我的 JSON 响应中返回失败。删除 Apple 推送并仅保留 Android 推送不会返回错误。

标签: php ios firebase push-notification firebase-cloud-messaging


【解决方案1】:

我删除了 sendNotificationPushApple 行,没有更改 sendPushNotification 函数,并将 sendMultiple 函数更改为此,它解决了问题:

public function sendMultiple($registration_ids, $message) {

        $fields = array(
            'registration_ids' => $registration_ids,
            'data' => $message,
            'notification' => $message,
            'priority'=>'high',
        );

        return $this->sendPushNotification($fields);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 2020-09-17
    • 1970-01-01
    • 2016-12-31
    • 2021-12-08
    相关资源
    最近更新 更多