Installation
composer require phpvital/laravel-jpush
Publish the config file:
php artisan vendor:publish --provider="Vital\Jpush\JpushServiceProvider"
Configuration
Edit .env with your JPush credentials:
JPUSH_APP_KEY=your_app_key
JPUSH_MASTER_SECRET=your_master_secret
First Use Case Send a push notification to all devices:
use Vital\Jpush\Jpush;
$jpush = app(Jpush::class);
$result = $jpush->push()
->platform('all')
->notification([
'alert' => 'Hello from Laravel!',
'title' => 'Notification Title',
])
->send();
Targeted Pushes Use registration IDs or tags for granular targeting:
$jpush->push()
->platform('android')
->registrationId('device_reg_id_123')
->notification(['alert' => 'Custom message'])
->send();
Message vs. Notification
->notification(['alert' => 'Text', 'title' => 'Title'])
->message(['msg_content' => 'Silent data'])
Custom Payload Extend with custom extras:
->notification(['alert' => 'Hello'])
->extras(['key' => 'value'])
Batch Operations Use queues for high-volume pushes:
$jpush->push()
->platform('ios')
->alias('user_alias_123')
->notification(['alert' => 'Batch message'])
->queue(); // Dispatches to queue
event(new UserLoggedIn($user));
// In listener:
$jpush->push()->alias($user->alias)->notification(['alert' => 'Welcome back!'])->send();
return response()->json(['success' => true, 'push_status' => $result]);
\Log::info('JPush result', $result);
Platform-Specific Quirks
registration_id or alias for targeting.alias or tag; ensure APNs certificates are valid.isProduction(false) in config for sandbox testing.Rate Limits JPush imposes rate limits. Batch pushes or queue them to avoid throttling.
Deprecated Methods
Avoid pushAll() (deprecated in newer JPush SDKs). Use platform('all') instead.
Environment Mismatch
Ensure .env values match your JPush console settings (e.g., JPUSH_APP_KEY for the correct app).
$result['response_code'] for errors (e.g., 200 = success, 401 = auth failure).'debug' => env('JPUSH_DEBUG', false),
Custom Responses
Override the default response handler in app/Providers/JpushServiceProvider.php:
$jpush->extend('response', function ($result) {
return collect($result)->only(['msg_id', 'is_success']);
});
Queue Drivers
Configure the queue driver in config/jpush.php:
'queue' => [
'driver' => 'database', // or 'redis', 'beanstalkd'
],
Event Listeners
Extend the package by listening to jpush.sent events:
Event::listen('jpush.sent', function ($result) {
// Custom logic (e.g., analytics)
});
Testing Use Laravel’s HTTP tests to mock JPush responses:
$this->mock(Jpush::class, function ($mock) {
$mock->shouldReceive('send')->andReturn(['response_code' => 200]);
});
How can I help you explore Laravel packages today?