尝试将 FCM 推送发送到 Android 和 iOS



尝试将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 上,用于包含 Apple 推送的任何呼叫。我偶尔也会,但不经常,向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);
}

我删除了 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);
    }

相关内容

  • 没有找到相关文章

最新更新