首页 > 解决方案 > 通过 Laravel 发送 FCM 消息以在我的 android 应用程序中打开深层链接

问题描述

如何将 Firebase 云令牌发送到我的 android 应用以打开我的应用深层链接?

我实施了深度链接并且它的工作

然后配置我的 firebase FCM 和我的 laravel 向我的 android 设备发送通知。有了这个库

https://github.com/brozot/Laravel-FCM

我找不到任何发送链接的方法,但是

 function sendNotification($user_id, $type)
 {
     $message = getNotificationMessage($type);

     try {
        $fcm_tokens = ClientInfo::where('user_id', $user_id)->all();
        foreach ($fcm_tokens as $key => $fcm_token) {
        $optionBuilder = new OptionsBuilder();
        $optionBuilder->setTimeToLive(60 * 20);

        $notificationBuilder = new PayloadNotificationBuilder();
        $notificationBuilder->setBody($message)
            ->setSound('default')
            ->setClickAction('bazarshahr://customer.app/order');

        $dataBuilder = new PayloadDataBuilder();
        $dataBuilder->addData(['deeplink' => 'bazarshahr://customer.app/product/39']);

        $option = $optionBuilder->build();
        $notification = $notificationBuilder->build();
        $data = $dataBuilder->build();

        $token = $fcm_token['firebase_token'];

        $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

        $downstreamResponse->numberSuccess();
        $downstreamResponse->numberFailure();
        $downstreamResponse->numberModification();

        // return Array - you must remove all this tokens in your database
        $downstreamResponse->tokensToDelete();

        // return Array (key : oldToken, value : new token - you must change the token in your database)
        $downstreamResponse->tokensToModify();

        // return Array - you should try to resend the message to the tokens in the array
        $downstreamResponse->tokensToRetry();

        // return Array (key:token, value:error) - in production you should remove from your database the tokens
        $downstreamResponse->tokensWithError();
    }
} catch (Exception $e) {
    SystemLog::error(sprintf("[helpers.sendNotif] Can't send Nofication: %s (%d)", $e->getMessage(), $e->getCode()));
    return false;
}

return true;
}

标签: laravelfirebase-cloud-messagingdeep-linkingdeeplinkandroid-deep-link

解决方案


Fcm 文档中未提及此功能,我自己尝试了某种测试并找到了解决方案:正如我们在此处回答的那样

我们需要放置链接而不是click_action

https://fcm.googleapis.com/fcm/send
Content-Type: application/json
Authorization: key={SERVER_KEY}

{
 "to" : "{Firebase client token}",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification"
     "link": "example://my.app/products"  <<-- Here is the solution
 }
}

推荐阅读