kirant400/laravel-push-notification

该包最新版本(dev-master)没有提供许可证信息。

Laravel包,用于向移动设备发送推送通知(apns, gcm, wns, 黑莓)

dev-master 2015-06-09 02:21 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:59:52 UTC


README

使设备能够发送推送通知的包

安装

更新你的composer.json文件,将其作为依赖项包括进来。

"davibennun/laravel-push-notification": "dev-master"

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

'providers' => array(
    Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider
)

通过将PushNotification外观添加到app/config/app.php文件中的别名数组中,来别名PushNotification外观。

'aliases' => array(
    'PushNotification' => 'Davibennun\LaravelPushNotification\Facades\PushNotification'
)

配置

运行以下命令将配置文件复制到你的项目中

php artisan config:publish davibennun/laravel-push-notification

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

array(
    'appNameIOS'=>array(
		'environment' => 'development',
		'certificate' => '/path/to/certificate.pem',
		'passPhrase'  => 'password',
		'service'     => 'apns'
    ),
    'appNameAndroid'=>array(
		'environment' => 'production',
		'apiKey'      => 'yourAPIKey',
		'service'     => 'gcm'
    )
);

其中所有第一级键对应一个服务配置,每个服务都有自己的属性,例如Android有apiKey,iOS使用certificatepassPhrase。你可以设置任意数量的服务配置,每个应用一个。

别忘了将service键设置为以识别iOS('service'=>'apns')和Android('service'=>'gcm'
证书路径必须是绝对路径,因此你可以在配置文件中使用这些
//Path to the 'app' folder
'certificate'=>app_path().'/myCert.pem'

Laravel函数也适用,例如public_path()storage_path()base_path()

用法

PushNotification::app('appNameIOS')
                ->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并为其添加了一些特色。

使用建议

应该使用Laravel Queues来使用此包,这样推送不会阻塞用户,而是在后台处理,意味着更好的流程。