我使用Laravel 5.2来创建我的网络API,以便在我的iOS应用中发送推送通知。有一些场景应该将相同的推送消息发送到多个设备。现在我迭代(foreach)通过所有设备令牌并将推送消息发送到每个设备,当有许多设备时,它真的很慢。即使有超过1000个设备,我怎样才能使它更快。
到目前为止,这是我糟糕的代码......
foreach ($devices as $device)
{
$user = $device->user;
//don't notify user if the users device is already in the array, if the user owns the post or if the user is the one who comment
if (!in_array($device->device_token, $DeviceTokenArray) && $user->id != $post->user->id /*&& $user->id != $commentUser->id*/) {
$deviceToken = $device->device_token;
$message = PushNotification::Message('Ny kommentar - '. $post->title, array(
'badge' => 1,
'sound' => 'example.aiff',
'custom' => array('data' => array(
'id' => $post->id,
))
));
PushNotification::app('appNameIOS')
->to($deviceToken)
->send($message);
array_push($DeviceTokenArray, $device->device_token);
if(!in_array($user->id, $commentUserArray))
{
$notify = new Notification();
$notify->fullname = $commentUser->firstname . " " . $commentUser->lastname;
$notify->description = $commentUser->firstname . " " . $commentUser->lastname . " " . "har kommenteret annoncen," . " " . $post->title;
$notify->photourl = $commentUser->photourl;
$notify->post_id = $post->id;
$user->notifications()->save($notify);
array_push($commentUserArray, $user->id);
}
}
}