Open mehdibahraami opened 8 years ago
You should use a daemon queue to avoid this kind of issues.
@depsimon can you send a sample to me?
Here's how I would do it in your case
// Somewhere in your app
public function sendIOS($title, $data, $tokens)
{
$job = new SendPushNotification('appNameIOS', $title, $data, $tokens)->onQueue('push-notifications');
dispatch($job);
}
And create a new job (php artisan make:job SendPushNotification
)
// App/Jobs/SendPushNotification.php
protected $appName;
protected $title;
protected $data;
protected $tokens;
public function __construct($appName, $title, $data, $tokens)
{
$this->appName = $appName;
$this->title = $title;
$this->data = $data;
$this->tokens = $tokens;
}
public function handle()
{
$deviceCollection = PushNotification::DeviceCollection();
foreach ($this->tokens as $token) {
$deviceCollection->add(PushNotification::Device($token->token));
}
$message = PushNotification::Message($title, $data);
PushNotification::app($appName)->to($deviceCollection)->send($message);
}
Don't forget to setup your queues (it's done easily in Forge) and to import the classes when needed.
More information : https://laravel.com/docs/5.3/queues#dispatching-jobs
Also I'd recommend to use the new Notifiable trait that came with 5.3 but haven't had the chance to implement it yet.
Hi depsimon,
As we know we can send push notifications to multiple devices by passing and array of device tokens:
$deviceCollection = PushNotification::DeviceCollection(); foreach ($this->tokens as $token) { $deviceCollection->add(PushNotification::Device($token->token)); }
$message = PushNotification::Message($title, $data);
PushNotification::app($appName)->to($deviceCollection)->send($message);
}
I want to send to multiple devices in one go but with different messages. How can I do this ? Because as $message cannot be an array in the above function.
Hi, When I send Apple push notification for 25 users everything is ok, but when I send notification for 50 or more users get this errors:
sometimes: Service unavailable
and Sometimes: ERR_EMPTY_RESPONSE