notgosu/laravel-push-notification

此包最新版本(3.0.4)没有提供许可证信息。

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

3.0.4 2020-03-17 12:30 UTC

This package is auto-updated.

Last update: 2024-09-17 23:20:17 UTC


README

包,用于使设备能够发送推送通知

安装

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

Laravel 5 & Lumen

"turbo124/laravel-push-notification": "dev-laravel5"

Laravel 4.*

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

通过将其添加到providers数组中注册PushNotification服务提供者。

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

app/config/app.php文件的aliases数组中添加别名PushNotification,以别名方式使用。

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

配置

通过运行以下命令将配置文件复制到你的项目中:(Lumen用户请跳过此步)

Laravel 5

php artisan vendor:publish --provider="Davibennun\LaravelPushNotification\LaravelPushNotificationServiceProvider" --tag="config"

Laravel 4.*

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指的是在配置文件中定义的服务。

###动态配置和Lumen用户你可以直接设置app配置数组:(请注意数组结构)

//iOS app
PushNotification::app(['environment' => 'development',
		'certificate' => '/path/to/certificate.pem',
		'passPhrase'  => 'password',
		'service'     => 'apns']);
//Android app		
PushNotification::app(['environment' => 'production',
		'apiKey'      => 'yourAPIKey',
		'service'     => 'gcm']);

向多个设备和可选消息

$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一起使用,这样推送就不会阻塞用户,而是在后台处理,意味着更好的流程。