bnbwebexpertise/laravel-push-notifications
Composer 安装命令:
composer require bnbwebexpertise/laravel-push-notifications
包简介
Apple and Google Push Notification
README 文档
README
Use Firebase Cloud Messaging
Installation
For Laravel 5.4 or older, add the provider in your config/app.php :
'providers' => [ Bnb\PushNotifications\PushNotificationsServiceProvider::class, ],
Laravel 5.5 use Composer auto-discovery feature.
Configuration
Configuration via environment
To return the APNs payloads in the results array (debugging purpose) :
PUSH_RETURN_PAYLOADS=true
Apple Push Notification Service
Put your APNs certificate somewhere inside your project path. Add the relative path to your certificate in your .env file :
PUSH_APNS_CERTIFICATE=config/push/certificate.pem
If your certificate is secured with a password you can specify it in the .env file as :
PUSH_APNS_PASSWORD=changeme
You can also set the environnement to use (default to production) :
PUSH_APNS_ENVIRONMENT=development
Google Cloud Messaging
Grap you API key from the Google Developer Console and add it to your .env file :
PUSH_GCM_KEY=AIaeRtYiUoP-QsDfghQJK1lMWXCvBN23AZE4RT6u
Configuration via PHP
If you prefer to configure the module from a config PHP file, publish it :
php artisan vendor:publish --provider=Bnb\\PushNotifications\\PushNotificationsServiceProvider --tag=config
Then set the desired configuration values :
<?php return [ 'apns' => [ 'environment' => 'production', 'certificate' => __DIR__ . '/push/certificate.pem', 'password' => 'changeme', ], 'gcm' => [ 'key' => 'AIaeRtYiUoP-QsDfghQJK1lMWXCvBN23AZE4RT6u', ], // the size of the chunk batch loop 'chunk' => 100, // set to true to return the APNs payloads in the results array 'payloads' => false, ];
Configuration at runtime
The GCM and APNs configuration can be changed at runtime via the setGcmOption($key, $value) and setApnsOption($key, $value) methods of the Notification class.
GCM options
| Key | Default | Description |
|---|---|---|
| key | config('push.gcm.key') |
The GCM server API key |
APNs options
| Key | Default | Description |
|---|---|---|
| certificate | base_path(config('push.apns.certificate')) |
The APNs certificate path on the server filesystem |
| password | config('push.apns.password') |
The APNs certificate password |
| environment | config('push.apns.environment') |
The APNs environnement |
Usage
A notification message holds the following properties where each one can be overridden by the device definition :
| Name | Type | Description |
|---|---|---|
| title | string | The title displayed in the notification bar |
| message | message | The message displayed in the notification bar (platform dependent) |
| badge | int | The badge number displayed in the notification bar |
| sound | string | The sound to play when the notification is received (platform dependent) |
| ttl | int | Number of seconds after which the message is expired by the network |
| metadata | array | Key/Value pairs of custom data |
$notification = new Notification('title', 'message'); $notification ->badge(5) ->sound('sound') ->ttl(1234) ->metadata('key1', 'value1') ->metadata('key2', 'value2');
$device = Device::apns('a-token', 'a-unique-local-id'); $device ->title('deviceTitle') ->message('deviceMessage') ->badge(10) ->sound('deviceSound') ->ttl(4321) ->metadata('key1', 'deviceValue1') ->metadata('deviceKey2', 'value2');
Metadata
For APNs the props custom property holds the list of the metadata keys including title and message.
For GCM the metadata are bound to the appdata object.
Example
$notification = new \Bnb\PushNotifications\Notification('Hello World !', 'This is a test message'); $notification->metadata('custom-id', 1234); $notification->push(\Bnb\PushNotifications\Device::gcm('test-token')->badge(3)->metadata('device-key','demoGcm')); $notification->push(\Bnb\PushNotifications\Device::apns('test-token')->badge(2)->metadata('device-key','demoApns')); $results = $notification->send(); // $results['errors'] // Contains the list of failed devices // $results['updates'] // Contains the list of updated token devices (GCM) // $results['payloads'] // Contains the messages payloads (APNs only) if config('push.payloads') is set to true foreach($results['errors'] as $data) { DbDevice::where('token', $data->token) ->delete(); } foreach($results['updates'] as $data) { DbDevice::where('token', $data['device']->token) ->update(['token' => $data['token']]); }
Commands
You can use the following Artisan command lines to send test messages :
Send to Android devices
php artisan push:gcm [options] [--] <token> <message> [<title>]
Arguments:
token the device token
message the notification message
title (optional) the notification title
Options:
--sender-id[=SENDER-ID] The GCM sender ID
Example :
php artisan push:gcm "ebizwJXzS7o:APA91bEa6tnBa-ZTkSf0fnsGNvU1BLdMnSi09GQ6BkFp-p99wSyVqb0f1nZpE3UEb-w3TzlrwhRGG1YQC0SV9N4DwO17RdceUX77ahAYtWcpFMgC4Xnc3NSkQ9PSqYfeFRPDL6D_KORM" "This is a test message"
Send to iOS devices
php artisan push:apns [options] [--] <token> <message> [<title>]
Arguments:
token the device token
message the notification message
title (optional) the notification title
Options:
--certificate[=CERTIFICATE] The Apple certificate path
--password[=PASSWORD] The Apple certificate password
--environment[=ENVIRONMENT] The Apple push environment (production or development)
Example :
php artisan push:apns "3c1c1c88428aeec68525a3e3d23c632bfef8c076c45e3af6769501b4ba493b1b" "This is a test message" "Hello World"
bnbwebexpertise/laravel-push-notifications 适用场景与选型建议
bnbwebexpertise/laravel-push-notifications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.89k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「apple」 「notification」 「laravel」 「push」 「gcm」 「apns」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bnbwebexpertise/laravel-push-notifications 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnbwebexpertise/laravel-push-notifications 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bnbwebexpertise/laravel-push-notifications 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
A simple library to decode and parse Apple Sign In client tokens.
Apple Push Notification & Feedback Provider
Throttle notifications on a per-channel basis
Extensible library for building notifications and sending them via different delivery channels.
Simple javascript toast notifications
统计信息
- 总下载量: 5.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-11