laravel - 通过 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;
}
解决方案
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
}
}
推荐阅读
- java - HeaderHttpSessionIdResolver 配置
- amazon-web-services - 基于请求参数的 AWS S3 策略条件访问
- angular - 我在网络选项卡中收到回复,但在控制台中仍然显示错误
- python - 如何使用 selenium 选择 javascript 元素
- cmake - 如何使用 cmake 将 .obj 文件添加到依赖项?
- mysql - 是否有任何功能可以在 sql 中查找具有特殊字符的结果?
- laravel-5.8 - 文件上传时如何在 Laravel 5.8 中禁用通知
- java - 编写具有多个条件的正则表达式模式
- asp.net-core - 标签助手执行顺序
- mongodb - 如何在 MongoDB 中查找具有计数的连续文档?