bnbwebexpertise/laravel-push-notifications

1.0.11 2017-10-28 05:35 UTC

This package is auto-updated.

Last update: 2024-09-06 18:32:21 UTC


README

使用Firebase云消息服务

安装

对于Laravel 5.4或更早版本,请在您的 config/app.php 文件中添加提供者

    'providers' => [
        Bnb\PushNotifications\PushNotificationsServiceProvider::class,
    ],

Laravel 5.5使用 Composer 自动发现功能。

配置

通过环境变量配置

为了在结果数组中返回APNs有效负载(调试用途)

PUSH_RETURN_PAYLOADS=true

苹果推送通知服务

将您的APNs证书放在项目路径中的某个位置。在您的 .env 文件中添加证书的相对路径

PUSH_APNS_CERTIFICATE=config/push/certificate.pem

如果您的证书受密码保护,您可以在 .env 文件中指定它

PUSH_APNS_PASSWORD=changeme

您还可以设置要使用的环境(默认为 production

PUSH_APNS_ENVIRONMENT=development

谷歌云消息服务

从谷歌开发者控制台获取您的API密钥并将其添加到您的 .env 文件中

PUSH_GCM_KEY=AIaeRtYiUoP-QsDfghQJK1lMWXCvBN23AZE4RT6u

通过PHP配置

如果您更喜欢通过 config PHP文件配置模块,请发布它

php artisan vendor:publish --provider=Bnb\\PushNotifications\\PushNotificationsServiceProvider --tag=config

然后设置所需的配置值

<?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,

];

运行时配置

可以通过 Notification 类的 setGcmOption($key, $value)setApnsOption($key, $value) 方法在运行时更改GCM和APNs配置。

GCM选项

APNs选项

用法

通知消息包含以下属性,每个属性都可以由设备定义覆盖

$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');

元数据

对于APNs,props自定义属性包含包括titlemessage在内的元数据键的列表。

对于GCM,元数据绑定到appdata对象。

示例

$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']]);
}

命令

您可以使用以下Artisan命令行发送测试消息

发送到Android设备

php artisan push:gcm [选项] [--] <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

示例

php artisan push:gcm "ebizwJXzS7o:APA91bEa6tnBa-ZTkSf0fnsGNvU1BLdMnSi09GQ6BkFp-p99wSyVqb0f1nZpE3UEb-w3TzlrwhRGG1YQC0SV9N4DwO17RdceUX77ahAYtWcpFMgC4Xnc3NSkQ9PSqYfeFRPDL6D_KORM" "This is a test message"

发送到iOS设备

php artisan push:apns [选项] [--] <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)

示例

php artisan push:apns "3c1c1c88428aeec68525a3e3d23c632bfef8c076c45e3af6769501b4ba493b1b" "This is a test message" "Hello World"