someline/laravel-push-notification

此包的最新版本(v1.0.1)没有提供许可证信息。

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

v1.0.1 2020-04-15 02:32 UTC

This package is auto-updated.

Last update: 2024-09-15 12:09:25 UTC


README

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

安装

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

Laravel 5

"someline/laravel-push-notification": "1.0.*"

Laravel 4.*

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

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

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

app/config/app.php 文件中,将 PushNotification 门面别名添加到别名数组中。

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

配置

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

Laravel 5

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

Laravel 4.*

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

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

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