如何将数据库结果序列化为 FCM/Push 通知数据?
How I can serialize database results as FCM/Push notification data?
我制作了一个控制器,用于发送推送通知一些从数据库中获取的数据:
use App\Model\MyModel;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Facades\FCM;
//come code here
$data = MyModel::where('foreignh_table_id',id)->select('col1','col2','col3')->get()->toArray();
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData($data);
$fcmTopic = new Topics();
$fcmTopic->topic('lorem_ipsum');
$title = __('pn_title',['client' => 'JohnDOE']);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notification = $notificationBuilder->build();
$status = FCM::sendToTopic($topic,null,$notification,$data);
但在线:
$status = FCM::sendToTopic($topic,null,$notification,$data);
我收到以下错误:
"Field \"data\" must be a JSON array:
基于此每个推送通知必须是一个关联数组而不是项目数组。
那么如何通过 FCM 将数据库结果发送回 android 应用程序?
发回的数据必须是关联数组,这意味着数据的每一项都必须有一个非数字键。一个快速的解决方案是将结果设置为关联数组,如下所示:
use App\Model\MyModel;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Facades\FCM;
//come code here
$data = MyModel::where('foreignh_table_id',id)->select('col1','col2','col3')->get()->toArray();
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData([ 'data' => $data ]);
$fcmTopic = new Topics();
$fcmTopic->topic('lorem_ipsum');
$title = __('pn_title',['client' => 'JohnDOE']);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notification = $notificationBuilder->build();
$status = FCM::sendToTopic($topic,null,$notification,$data);
关注上线:
$dataBuilder->addData([ 'data' => $data ]);
我所做的是为提供的数据设置一个键,因此一旦序列化它将变成一个 object/associative 数组。
因此,PayloadDataBuilder::addData
中提供的任何数据都必须是关联数组。
另一种方法是将任何数字键转换为字符串。不过,我还没有测试过,也没有代码可以提供。
我制作了一个控制器,用于发送推送通知一些从数据库中获取的数据:
use App\Model\MyModel;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Facades\FCM;
//come code here
$data = MyModel::where('foreignh_table_id',id)->select('col1','col2','col3')->get()->toArray();
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData($data);
$fcmTopic = new Topics();
$fcmTopic->topic('lorem_ipsum');
$title = __('pn_title',['client' => 'JohnDOE']);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notification = $notificationBuilder->build();
$status = FCM::sendToTopic($topic,null,$notification,$data);
但在线:
$status = FCM::sendToTopic($topic,null,$notification,$data);
我收到以下错误:
"Field \"data\" must be a JSON array:
基于此
那么如何通过 FCM 将数据库结果发送回 android 应用程序?
发回的数据必须是关联数组,这意味着数据的每一项都必须有一个非数字键。一个快速的解决方案是将结果设置为关联数组,如下所示:
use App\Model\MyModel;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use LaravelFCM\Facades\FCM;
//come code here
$data = MyModel::where('foreignh_table_id',id)->select('col1','col2','col3')->get()->toArray();
$dataBuilder = new PayloadDataBuilder();
$dataBuilder->addData([ 'data' => $data ]);
$fcmTopic = new Topics();
$fcmTopic->topic('lorem_ipsum');
$title = __('pn_title',['client' => 'JohnDOE']);
$notificationBuilder = new PayloadNotificationBuilder($title);
$notification = $notificationBuilder->build();
$status = FCM::sendToTopic($topic,null,$notification,$data);
关注上线:
$dataBuilder->addData([ 'data' => $data ]);
我所做的是为提供的数据设置一个键,因此一旦序列化它将变成一个 object/associative 数组。
因此,PayloadDataBuilder::addData
中提供的任何数据都必须是关联数组。
另一种方法是将任何数字键转换为字符串。不过,我还没有测试过,也没有代码可以提供。