首页 > 解决方案 > 使用 FCM 向 iOS 设备推送通知时遇到问题

问题描述

我正在开发一个应用程序,并已让 FCM 将通知发送到 Android 设备,但目前在发送到 iOS 设备时遇到问题。

我已按照此处列出的步骤执行此操作其中还包括此处列出的步骤。

这是我拥有的适用于 Android 设备的 FCM SDK 代码:

    // This assumes that you have placed the Firebase credentials in the same directory
    // as this PHP file.
    $serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/google-service-account.json');

    // get our Firebase account loaded
    $firebase = (new Firebase\Factory())
            ->withServiceAccount($serviceAccount)
        ->create();

    // create a message
    $messaging = $firebase->getMessaging();

    // Creating a notification
    $notification = Notification::fromArray([
        'title' => $title,
        'body' => $body,
    ]);

    $data = [
        'click_action' => 'FLUTTER_NOTIFICATION_CLICK',
        'fromid' => $fromid,
    ];

    // Compose the message; notification + device token
    $message = CloudMessage::withTarget('token', $deviceToken)
        ->withNotification($notification)
        ->withData($data);


    // validate the message
    try {
        $messaging->validate($message);
    } catch (InvalidMessage $e){
        print_r($e->errors());
        exit;
    }

    // send message
    $report = $messaging->send($message);

我尝试通过 Firebase 控制台发送消息并使用 FCM SDK;两者都没有导致通知出现在 iOS 设备上。控制台没有给我任何反馈,但是 SDK 给了我一个错误:

致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:POST https://fcm.googleapis.com/v1/projects/apwt-app1/messages:send导致401 Unauthorized响应:{“错误”:{“代码”:401,“消息”:“来自 APNS 或 Web 推送服务的身份验证错误”,“状态”:“ /var/www/vhosts/globalsups/composer/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 中的 UNAUTHENTICATE(截断...)

标签: firebasefirebase-cloud-messagingapple-push-notifications

解决方案


推荐阅读