daim2k5/laravel-push-notification

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

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

1.0.4 2017-03-22 09:52 UTC

This package is not auto-updated.

Last update: 2024-09-29 01:34:15 UTC


README

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

安装

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

Laravel 5 & Lumen

"daim2k5/laravel-push-notification": "1.*"

通过将其添加到 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"

这将生成一个类似这样的配置文件

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