nhiepphong/laravel-push-notification

Laravel 5 包,用于向 Android 和 iOS 设备发送推送通知。与 witty 的相同,增加了对 Laravel 5.2、5.3、5.4、5.5 的支持

1.0.1 2017-09-15 01:26 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:01:08 UTC


README

基于 https://github.com/davibennun/laravel-push-notification 分支,源自 https://github.com/larkinwhitaker/laravel-push-notification

增加了对 Laravel 5.2 => 5.5.* 的支持。

安装

安装包

composer require nhiepphong/laravel-push-notification

通过将 PushNotification 服务提供者添加到 config/app.php 文件中的 providers 数组来注册 PushNotification 服务提供者。

'providers' => array(
    'Nhiepphong\LaravelPushNotification\PushNotificationServiceProvider'
)

通过在 config/app.php 文件中的 aliases 数组中添加 PushNotification 门面来别名 PushNotification。

'aliases' => array(
	'PushNotification'      => 'Nhiepphong\LaravelPushNotification\PushNotification',
)

配置

通过运行以下命令将配置文件复制到您的项目中:

php artisan vendor:publish

这将生成一个配置文件,如下所示:

array(
    'iOS'     => [
        'environment' => env('IOS_PUSH_ENV', 'development'),
        'certificate' => env('IOS_PUSH_CERT', __DIR__ . '/ios-push-notification-certificates/development/certificate.pem'),  
        'passPhrase'  => env('IOS_PUSH_PASSWORD', '291923Job'),
        'service'     => 'apns'
    ],

    'android' => [
        'environment' => env('ANDROID_PUSH_ENV', 'development'),
        'apiKey'      => env('ANDROID_PUSH_API_KEY', 'yourAPIKey'),
        'service'     => 'gcm'
    ]
);

其中所有一级键都对应一个服务配置,每个服务都有自己的属性,例如,Android 有 apiKey,iOS 使用 certificatepassPhrase。您可以设置任意数量的服务配置,每个应用程序一个。将为您在配置文件夹中添加一个名为 'ios-push-notification-certificates' 的目录,以便存储开发和生产证书。

不要忘记将 service 键设置为以标识 iOS 'service'=>'apns' 和 Android 'service'=>'gcm'

用法

PushNotification::app('iOS')
                ->to($deviceToken)
                ->send('Hello World, i`m a push message');

其中 app 参数 appNameIOS 指的是在配置文件中定义的服务。向多个设备发送可选消息

$devices = PushNotification::DeviceCollection(array(
    PushNotification::Device('token', array('badge' => 5)),
    PushNotification::Device('token1', array('badge' => 1)),
    PushNotification::Device('token2')
));
$message = PushNotification::Message('Message Text',array(
    'badge' => 1,
    'sound' => 'example.aiff',
    
    'actionLocKey' => 'Action button title!',
    'locKey' => 'localized key',
    'locArgs' => array(
        'localized args',
        'localized args',
    ),
    'launchImage' => 'image.jpg',
    
    'custom' => array('custom data' => array(
        'we' => 'want', 'send to app'
    ))
));

collection = PushNotification::app('appNameIOS')
    ->to($devices)
    ->send($message);

// get response for each device push
foreach ($collection->pushManager as $push) {
    $response = $push->getAdapter()->getResponse();
}

// access to adapter for advanced settings
$push = PushNotification::app('appNameAndroid');
$push->adapter->setAdapterParameters(['sslverifypeer' => false]);

此包封装了 [Notification Package] 并为其添加了一些特性。


#### Usage advice
This package should be used with [Laravel Queues], so pushes dont blocks the user and are processed in the background, meaning a better flow.



[Laravel Queues]:https://laravel.net.cn/docs/queues