davibennun / laravel-push-notification

Laravel package to enable sending push notifications to devices
1.23k stars 259 forks source link

Multiple APNS Notification #139

Open mehdibahraami opened 8 years ago

mehdibahraami commented 8 years ago

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

public function sendIOS($title, $data, $tokens)
    {
        ini_set('memory_limit', '-1');
        $targetDevices = [];
        foreach($tokens as $token) {
            $devices = PushNotification::Device($token->token);
            array_push($targetDevices, $devices);
        }
        $deviceCollection = PushNotification::DeviceCollection(($targetDevices));
        $message = PushNotification::Message($title, $data);
        PushNotification::app('appNameIOS')->to($deviceCollection)->send($message);
    }
depsimon commented 8 years ago

You should use a daemon queue to avoid this kind of issues.

mehdibahraami commented 8 years ago

@depsimon can you send a sample to me?

depsimon commented 8 years ago

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.

jitu60 commented 8 years ago

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.