laraviet/laravel-push-notification

该软件包的最新版本(2.0)没有可用的许可信息。

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

2.0 2015-03-06 11:19 UTC

This package is auto-updated.

Last update: 2024-08-27 16:45:08 UTC


README

用于启用向设备发送推送通知的软件包

安装

更新您的composer.json文件,将其作为依赖项包括在内。

"laraviet/laravel-push-notification": "2.*"

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

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

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

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

配置

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

php artisan vendor:publish

这将生成一个类似以下的配置文件:

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使用此软件包,这样推送就不会阻塞用户,并在后台处理,意味着更好的流程。