reliefapps/notification-bundle

此包已被弃用,不再维护。未建议替代包。

提供发送推送通知服务的Bundle

v0.1.10 2017-07-18 07:27 UTC

README

Latest Stable Version Total Downloads SensioLabsInsight

安装

步骤 1: 下载Bundle

打开命令行,进入您的项目目录,然后执行以下命令以下载此Bundle的最新稳定版本

$ composer require reliefapps/notification-bundle "0.1.4"

注意:在版本1之前,可能存在向后兼容性问题。建议强制使用此处显示的单个版本。

此命令需要您全局安装了Composer,如Composer文档中的安装章节所述。

步骤 2: 启用Bundle

然后,通过将其添加到项目app/AppKernel.php文件中注册的Bundle列表中来启用该Bundle。

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Reliefapps\NotificationBundle\ReliefappsNotificationBundle(),
        );

        // ...
    }

    // ...
}

步骤 3: 配置Bundle

在您的项目中,在文件app/config/config.yml的末尾添加以下配置

reliefapps_notification:
    android:
        server_key: **your_gcm_key**
    ios:
        push_certificate: **path_to_your_ios_certificate**
        push_passphrase: **your_passphrase_for_the_certificate**
        protocol: legacy

可选(但推荐):

为了改进iOS通知的错误管理,您需要在您的机器上对cURL启用http2支持。查看此教程。

完成此教程后,通过运行以下命令来检查cURL是否支持HTTP/2:

$ curl --http2 -I https://nghttp2.org/

如果它工作,您可以删除app/config/config.yml文件中的以下行:

            protocol: legacy

在您的app/config/config.yml文件中。

使用方法

步骤 1: 创建设备实体

首先,您需要创建自己的设备实体,该实体扩展了此Bundle的ReliefappsNotificationBundle:Device实体。

然后,通过以下内容完成您的app/config/config.yml文件:

reliefapps_notification:
    #...
    device:
        class: YourBundle\Entity\Device

步骤 2: 注册一些设备

在策略API控制器(登录,主页)中注册设备UUID和令牌

<?php

// ...
class YourController
{
    // ...
    public function YourAction()
    {
        // ...
        // Get the Device Manager
        $deviceManager = $this->get('reliefapps_notification.device.manager');

        // Create a new device
        $newDevice = $deviceManager->createDevice($uuid, $platform);
        $newDevice->setToken($token);

        // Save the device in database
        $deviceManager->udpateDevice($newDevice);

    }
}

步骤 3: 创建通知正文

NotificationBody允许您创建推送通知的内容。

<?php

use Reliefapps\NotificationBundle\Resources\Model\NotificationBody;

// ...
class YourController
{
    // ...
    public function YourAction()
    {
        // ...
        $body = new NotificationBody();
        $body ->setTitle('Notification Title')      // Title of the notification
              ->setBody('This is a notification !') // Text of the notification
              ->setBadge(42);                       // Badge on the app icon (iOS only)
    }
}

步骤 4: 发送推送通知

您已经准备好发送第一条推送通知了!

函数sendPush接收一个设备数组和通知正文,并将推送通知发送到设备!

如果令牌无效,它将在您的数据库中自动设置为null。

<?php

// ...
class YourController
{
    // ...
    public function YourAction()
    {
        // ...
        // Get the Push Manager
        $pushManager = $this->container->get('reliefapps_notification.push_manager');

        // Send a push notification to devices $device1 and $device2
        $pushManager->sendPush(Array($device1, $device2), $body);
    }
}

高级配置

上下文

上面展示的配置不允许您切换服务器(在开发环境和生产环境之间切换)或更改 apns_topic 和证书(从单个后端管理多个应用)。

为了解决这个问题,我们引入了对象 上下文。上下文是一组可以独立使用的配置。

上下文定义在您的 app/config/config.yml

reliefapps_notification:
    android:
        server_key: **prod_gcm_key**
    ios:
        push_certificate: **prod_ios_certificate**
        push_passphrase: **prod_passphrase**
        apns_topic: myapp_prod
    contexts:
        ctx_dev:
            android:
                server_key: **dev_gcm_key**
                gcm_server: android.development.googleapis.com
            ios:
                push_certificate: **dev_ios_certificate**
                push_passphrase: **dev_passphrase**
                apns_server: api.development.push.apple.com
                apns_topic: myapp_dev
        ctx_app2:
            ios:
                apns_topic: myapp2

上下文中未填写的所有字段都将使用默认配置填充。

您可以使用 PushManager 通过名称调用上下文。

有关更多信息,请查阅 Reliefapps\NotificationBundle\Resources\Utils\ContextManager 和 Reliefapps\NotificationBundle\Resources\Model\Context

<?php

// ...
class YourController
{
    // ...
    public function YourAction()
    {
        // ...

        // The third parameter ("default" by default) indicates the context
        $pushManager->sendPush(Array($device1, $device2), $body, 'ctx_app2');
    }
}

附加字段

您可能希望向要发送的通知添加一些数据。

<?php

// ...
class YourController
{
    // ...
    public function YourAction()
    {
        // ...

        $additionalFields = array(
            array("key" => "id_user", "value" => 42),
            array("key" => "linkToFollow", "value" => "https://packagist.org.cn/packages/reliefapps/notification-bundle")
        );

        $body->setAdditionalFields($additionalFields);
        $body->addAdditionalField(array("key" => "isNew", "value" => true));

        $pushManager->sendPush(Array($device1, $device2), $body);
    }
}

文档

有效载荷

实体: Reliefapps\NotificationBundle\Resources\Model\NotificationBody

描述 iOS Android
title 标题 [x] [x]
body 主文本 [x] [x]
ledColor 手机前部的LED颜色 [ ] [x]
image 用于应用的图标路径 [ ] [x]
imageType 通知图标的形状 [ ] [x]
notId 区分通知的ID [ ] [x]
actions 操作列表 [ ] [x]
badge 应用图标上的徽章数字 [x] [ ]
category iOS类别标签(在您的应用中定义) [x] [ ]

Android操作

实体: Reliefapps\NotificationBundle\Resources\Model\AndroidAction

描述 iOS Android
icon 图标(应用可绘制资源的名称) [ ] [x]
title 操作文本 [ ] [x]
callback 按钮点击时调用的函数 [ ] [x]
foreground 点击后打开应用?(默认为true) [ ] [x]
inline 使用快速回复字段?(默认为false) [ ] [x]

SensioLabsInsight