asugai/cake-php-notification-manager

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

基于MySQL和CRON的CakePHP通知管理器。

dev-master 2015-04-30 18:19 UTC

This package is not auto-updated.

Last update: 2018-12-27 11:01:31 UTC


README

基于MySQL和CRON的CakePHP通知管理器。

背景

支持邮件和推送通知的设置。

要求

安装

[手动]

[GIT子模块]

在您的应用目录中输入

git submodule add git://github.com/asugai/CakePHP-Notification-Manager.git Plugin/NotificationManager
git submodule update --init

[GIT克隆]

在您的应用目录中输入

git clone git://github.com/asugai/CakePHP-Notification-Manager.git Plugin/NotificationManager

启用插件

在您的 app/Config/bootstrap.php 文件中启用插件

CakePlugin::load('NotificationManager');

如果您已经使用 CakePlugin::loadAll();,则此步骤不是必需的。

用法

设置NotificationManager

设置 notifications

CREATE TABLE `notifications` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `model` varchar(128) DEFAULT NULL,
  `object_id_field` varchar(128) DEFAULT 'id',
  `object_id` varchar(256) DEFAULT NULL,
  `property` varchar(128) DEFAULT NULL,
  `type` enum('EMAIL','PUSH','SMS') DEFAULT NULL,
  `data` text,
  `send_on` datetime DEFAULT NULL,
  `timezone` varchar(128) DEFAULT 'UTC',
  `condition` varchar(128) DEFAULT NULL,
  `sent` tinyint(1) DEFAULT '0',
  `sent_on` datetime DEFAULT NULL,
  `errors` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

如果您在 /app/Config/bootstrap.php 中使用composer,请设置 autoloader

// Load composer autoload.
require APP . '/Vendor/autoload.php';

// Remove and re-prepend CakePHP's autoloader as composer thinks it is the most important.
// See https://github.com/composer/composer/commit/c80cb76b9b5082ecc3e5b53b1050f76bb27b127b
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);

// Load the bootstrap file to load Notification Model
CakePlugin::loadAll([
    'NotificationManager' => [
        'bootstrap' => true
    ]
]);

如果您不使用composer,请手动设置依赖项

// Load stripe
App::import('Vendor', 'stripe/lib/Stripe');

// Load the bootstrap file to load Notification Model
CakePlugin::loadAll([
    'NotificationManager' => [
        'bootstrap' => true
    ]
]);

编辑 /app/Config/bootstrap.php 文件并添加 UrbanAirshipTwilio 密钥

if (!Configure::check('UrbanAirship')) {
    Configure::write('UrbanAirship.key', '');
    Configure::write('UrbanAirship.master', '');
}

if (!Configure::check('Twilio')) {
    Configure::write('Twilio.sid', '');
    Configure::write('Twilio.token', '');
    Configure::write('Twilio.number', '');
}

在您的模型中设置通知事件。

示例 - 为新用户发送 Welcome 邮件

public $hasMany = [
    ...
    'Notification' => [
        'foreignKey' => 'object_id',
        'conditions' => [
            'Notification.model' => 'User'
        ]
    ],
    ...
];
...
public function register()
{
    ...
    $notification = [
        'model' => 'User', // name of the object model
        'object_id' => $this->id, // id of the object
        'property' => 'email', // property of the object that will be used to notify (ex. email, phone, cell)
        'type' => 'EMAIL', // Type of notification, can be EMAIL, PUSH, or SMS
        'data' => json_encode([
            'settings' => 'default', // email settings
            'subject' => 'Welcome!', // email subject
            'template' => 'welcome', // email template
            'emailFormat' => 'html', // email format
            'viewVars' => [ // email vars
                'first_name' => 'John',
                'last_name' => 'Doe',
                'email' => 'john.doe@example.com'
            ]
        ])
    ];

    try {
        $NotificationModel = new Notification();
        $NotificationModel->create();
        $NotificationModel->save($notification);  
    } catch (Exception $e) {
        // failure catch
    }
    ...
}

示例 - 向用户发送推送通知

App::uses('Notification', 'NotificationManager.Model');
...
public function pushIt()
{
    ... 
    $notification = [
        'model' => 'User',
        'object_id' => $this->id,
        'property' => 'id', // using tags right now, User IDs being used for the tags
        'type' => 'PUSH',
        'data' => json_encode([
            'notification' => 'You got a new push!', // This is the copy for the push
        ]),
    ];
    
    try {
        $NotificationModel = new Notification();
        $NotificationModel->create();
        $NotificationModel->save($notification);  
    } catch (Exception $e) {
        // failure catch
    }
    ...
}

设置一个 cronjob 在后台运行通知

$ sudo crontab -e

示例 cronjob,每分钟运行一次 NotificationManager.Notifications,将 /var/www/html/app/Console/cake 替换为您自己设置中的 cake 控制台位置

* * * * * /var/www/html/app/Console/cake NotificationManager.Notifications

待办事项

  • 添加命名空间
  • 注释!
  • 添加特定设备的推送通知
  • 设置更多错误检查
  • 添加单元测试!

致谢

本README的基本布局借鉴自 https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource

许可

版权所有 (c) 2013 Andre Sugai

在此特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的个人,在不限制条件(包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利),以及允许向软件提供者提供软件的个人,在不违反以下条件的情况下,对软件进行处理的许可

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论是由合同行为、侵权行为或其他原因引起的,无论与软件有关还是与使用软件有关或与之有关的其他方式。