用Prowl推送通知

Push notifications to mobile devices are a quick and inexpensive way to send short messages to people wherever they happen to be – provided their phone is switched on, of course. But to send a notification, there are usually a number of hoops you to jump through; not least, registering your device so that the sending service can target your phone. This is understandable, of course, as there is enormous potential for misuse.

将通知发送到移动设备是一种向所有人发送短信的快速,廉价的方法,当然,前提是他们的手机处于开机状态。 但是要发送通知,通常会有很多麻烦您需要跳过。 尤其重要的是,注册您的设备,以便发送服务可以定位到您的手机。 当然,这是可以理解的,因为存在滥用的巨大潜力。

Suppose, however, you want a quick and easy way to send messages to yourself or a select group of people without having to register an application with Apple and play around with various certificates and processes. You might be interested in learning about Prowl.

但是,假设您想要一种快速简便的方法来向自己或特定人群发送消息,而不必在Apple上注册应用程序并使用各种证书和流程。 您可能对学习Prowl感兴趣。

Prowl is an iOS-based Growl client, the short messaging system that’s popular on Apple Macs. You can purchase a copy for your phone and “hook it up” to all sorts of services and applications – and perhaps most intriguingly, use its API to send messages from your own applications. That’s what we’ll look at in this article.

Prowl是基于iOS的Growl客户端,这是在Apple Mac上非常流行的短消息系统。 您可以购买手机的副本,然后将其“挂钩”到各种服务和应用程序中-也许最有趣的是,使用其API从您自己的应用程序发送消息。 这就是我们将在本文中讨论的内容。

入门 (Getting Started)

The first thing you’ll need is the Prowl application on your iOS device. You can download it from the App Store, and at the time of this writing it costs $2.99 USD.

您需要的第一件事是iOS设备上的Prowl应用程序。 您可以从App Store 下载它 ,在撰写本文时,它的价格为2.99美元。

Next, you’ll need to create an account on the Prowl website. Once you’re registered, you can create API keys. An API key is an identifier used to target devices registered with your account. You can set up multiple API keys for different uses, but you’ll only need one for now. All you need to know is that if you send a message targeted at one API key, you’ll receive it on all the devices registered with your account.

接下来,您需要在Prowl网站上创建一个帐户。 注册后,您可以创建API密钥。 API密钥是用于定位在您的帐户中注册的设备的标识符。 您可以为不同的用途设置多个API密钥,但现在仅需要一个。 您需要知道的是,如果您发送针对一个API密钥的消息,那么您将在使用您的帐户注册的所有设备上收到该消息。

You also need to associate your copy of Prowl with your account. Open the app on your iOS device and enter the login information you provided when you registered with the Prowl website.

您还需要将Prowl的副本与您的帐户相关联。 在iOS设备上打开应用程序,然后输入在Prowl网站上注册时提供的登录信息。

With everything set up, let’s test it. Log in to the Prowl website and go to “Add Message”. Send a short message, which you should receive on your iOS device. You can also set the priority of the message, as well as a URL which can be opened from the notification.

完成所有设置后,让我们对其进行测试。 登录到Prowl网站,然后转到“添加消息”。 发送一条短消息,您应该在iOS设备上收到该消息。 您还可以设置消息的优先级,以及可以从通知中打开的URL。

It’s worth noting that there are a number of other ways in which you can send messages. A number of applications for various platforms are listed on the Prowl site, some of which are even web apps (like the Prowl Chrome Extension which allows you to send your current URL to your device straight from the browser). You may also have noticed that an email address is generated when you add an API key in the form of <api_key>@api.prowlapp.com. If you send mail to that address, you’ll receive it as a push notification via Prowl.

值得注意的是,还有许多其他方式可以发送消息。 Prowl网站上列出了许多用于各种平台的应用程序,其中一些甚至是Web应用程序(例如Prowl Chrome扩展程序 ,该程序可让您直接从浏览器将当前URL发送到设备)。 您可能还已经注意到,以<api_key> @ api.prowlapp.com的形式添加API密钥时会生成一个电子邮件地址。 如果您将邮件发送到该地址,则会通过Prowl以推送通知的方式接收。

Because the process of creating an API key is a bit convoluted, and the key’s format is not user-friendly, Prowl might not be the ideal choice for public applications. It’s one thing to ask someone who’s not particularly tech-savvy to enter an e-mail address or mobile number, but quite another to expect him to generate a key, then copy and paste it in a form. But if you’re sending messages to yourself, or perhaps a small team, that process probably isn’t too much of a concern.

由于创建API密钥的过程有些麻烦,并且密钥的格式也不友好,因此Prowl可能不是公共应用程序的理想选择。 要求不是特别精通技术的人输入电子邮件地址或移动电话号码是一回事,但是期望他生成密钥,然后将其复制并粘贴到表单中则是另一回事。 但是,如果您要向自己或一个小的团队发送消息,则该过程可能不必太在意。

Also, the Prowl API has a rate limit – arguably a rather generous one – 1,000 calls per hour per IP address. If you require more than this, you can create a provider (from the API keys page) and use that in your API calls.

而且,Prowl API有一个速率限制-可以说是一个相当大的限制-每个IP地址每小时1,000个呼叫。 如果您需要的还不止这些,则可以创建一个提供程序(从API密钥页面),并在您的API调用中使用它。

Assuming all is well and you are receiving messages, we can move on to the next section.

假设一切正常,并且您正在接收消息,我们可以继续进行下一部分。

构建自己的应用程序 (Building Your Own Applications)

This is where things gets interesting – using Prowl’s API to send messages from a PHP application! We’re going to use ProwlPHP, a PHP library to interface with the API – but if you want to get your hands dirty and explore the API yourself, refer to online documentation. The library is available on GitHub and Composer.

这就是事情变得有趣的地方–使用Prowl的API从PHP应用程序发送消息! 我们将使用ProwlPHP,一个与API交互PHP库–但是,如果您想动手自己动手探索API,请参阅在线文档 。 该库可在GitHub和Composer上获得。

{
    "require": {
        "xenji/ProwlPHP": ">=1.0.2"
    }
}

To send a message, you first need to instantiate the Prowl connector, optionally set a provider key, and tell the connector you’re going to make a POST request:

要发送消息,您首先需要实例化Prowl连接器,可以选择设置提供者密钥,并告诉连接器您将发出POST请求:

$prowl = new Prowl\Connector();
$prowl->setProviderKey('YOUR-PROVIDER-KEY');
$prowl->setIsPostRequest(true);

Next, you are required to add a filter. You can use a closure to define a filter for the message text, for example to sanitize user input, limit the amount of text, appending extra information, and so on.

接下来,您需要添加一个过滤器。 您可以使用闭包为消息文本定义过滤器,例如,清理用户输入,限制文本量,附加额外信息等等。

$prowl->setFilterCallback(function ($text) {
    // ... do something
    return $text;
});

Alternatively, you can create a filter in a more object-oriented fashion by extending ProwlSecurityPassthroughFilterImpl and implement its filter() method:

另外,您可以通过扩展ProwlSecurityPassthroughFilterImpl并实现其filter()方法,以更加面向对象的方式创建过滤filter()

class CustomFilter extends ProwlSecurityPassthroughFilterImpl
{
    public function filter($sContentToFilter) {
        // Do something
        return $sContentToFilter;
    }
}

$filter = new CustomFilter();
$prowl->setFilter($filter);

Now create an instance of ProwlMessage:

现在创建一个ProwlMessage实例:

$msg = new ProwlMessage();

$msg->setApplication('My Prowl Application');
$msg->addApiKey('YOUR-API-KEY');
$msg->setPriority(0);
$msg->setEvent($event);
$msg->setDescription($description);
$msg->setUrl($url);

The addApiKey() method is where you specify the recipient; you can call this multiple times to send the message to multiple recipients.

您可以在addApiKey()方法中指定收件人; 您可以多次拨打此电话,将邮件发送给多个收件人。

The priority given to setPriority() should be one of the following values:

设置为setPriority()的优先级应为以下值之一:

prowl-01

The setEvent() method sets the name of the event, and can be up to 1,024 characters. Additionally, you can set an optional description for the event with setDescription(), and an optional URL, which can be opened directly from the notification, with setUrl().

setEvent()方法设置事件的名称,最多可以包含1,024个字符。 另外,您可以使用setDescription()设置事件的可选说明,并可以使用setUrl()直接从通知中打开可选URL。

Finally, to actually send the message, call the push() method:

最后,要实际发送消息,请调用push()方法:

$response = $prowl->push($msg);

使用Prowl记录应用程序 (Application Logging using Prowl)

Let’s say you’ve built a web application which is used by a significant number of people. What happens when a critical error occurs? You probably have logging set up, and perhaps the messages are logged to a file, a database, or some sort of datastore like Redis, but you’re going to want to know about serious errors straight away. What better way then by sending a push notification!

假设您已经构建了一个Web应用程序,该应用程序被大量人使用。 发生严重错误时会发生什么? 您可能已经设置了日志记录,也许消息已记录到文件,数据库或类似Redis的某种数据存储中,但是您将要立即了解严重的错误。 有什么更好的方法,那就是发送推送通知!

Many logging implementations provide the ability to define a number of log writers and configure them to respond to different error levels. For example, you may wish to log informational messages to a file but send critical messages by email. Zend_Log is one such implementation. Although it’s technically part of Zend Framework, it can also be used in a standalone fashion provided you make its dependencies available to your application. You can download it from the Zend Framework website or install it using Composer.

许多日志记录实现提供了定义许多日志记录器并将其配置为响应不同错误级别的能力。 例如,您可能希望将参考消息记录到文件中,但通过电子邮件发送重要消息。 Zend_Log就是这样一种实现。 尽管从技术上讲,它是Zend Framework的一部分,但只要您使其依赖项可用于您的应用程序,它也可以以独立方式使用。 您可以从Zend Framework网站下载它,也可以使用Composer安装它。

{
    "require": {
        "zendframework/zend-log": "2.2.*",
        "xenji/ProwlPHP": ">=1.0.2"
    }
}

Let’s create our own implementation of Zend\Log\AbstractWriter to send certain log messages via Prowl. The class will need to:

让我们创建自己的Zend\Log\AbstractWriter实现,以通过Prowl发送某些日志消息。 该课程将需要:

  1. have a property to store the Prowl keys

    具有存储Prowl键的属性
  2. accept the keys via its constructor

    通过其构造函数接受密钥
  3. implement the doWrite() method

    实现doWrite()方法

namespace SitePoint\Log\Writer;
use Zend\Log;

class Prowl extends \Zend\Log\Writer\AbstractWriter
{
    private $apiKeys;

    public function __construct($apiKeys) {
        if (is_array($apiKeys)) {
            $this->apiKeys = $apiKeys;
        }
        else {
            $this->apiKeys = array($apiKeys);
        }

        $this->_formatter = new Log\Formatter\Simple('%message%');
    }

    protected function doWrite(array $event) {
        $event = $this->_formatter->format($event);

        $prowl = new \Prowl\Connector();
        $prowl->setFilterCallback(function ($text) {
            return $text;
        });
        $prowl->setIsPostRequest(true);

        $msg = new \Prowl\Message();
        $msg->setApplication('My Custom Logger');
        $msg->setPriority(2);
        $msg->setEvent($event);
        foreach ($this->apiKeys as $key) {
            $msg->addApiKey($key);
        }

        $prowl->push($msg);
}

A few notes about the class:

关于班级的一些注意事项:

  • I set the formatter in the constructor which will simply send the message as-is. You can set this to any format you wish, or override it later.

    我在构造函数中设置了格式化程序,它将仅按原样发送消息。 您可以将其设置为所需的任何格式,或者以后再覆盖它。
  • I’m doing everything – e.g. instantiating the connector – in the doWrite() method to keep things simple. You may wish to expand the constructor to accept the application name, a provider key, etc.

    我正在doWrite()方法中做所有事情,例如实例化连接器,以使事情变得简单。 您可能希望扩展构造函数以接受应用程序名称,提供者密钥等。

  • I’ve hard-coding the priority as 2 – emergency – however, you may want to map the log priority to the Prowl priority, bearing in mind how few of the latter there are (and you’re probably only going to use Prowl for critical messages anyway).

    我已经将优先级硬编码为2-紧急-但是,您可能需要将日志优先级映射到Prowl优先级,但要记住后者的数量很少(您可能只将Prowl用于关键消息)。

Finally, registering the writer with the logger looks like this:

最后,在记录器中注册作者是这样的:

$apiKeys = array(
    'YOUR-API-KEY',
    'ANOTHER-API-KEY'
);

$logger = new Zend\Log\Logger();

$writer = new SitePoint\Log\Writer\Prowl($apiKeys);
$writer->addFilter(
    new Zend\Log\Filter\Priority(Zend\Log\Logger::CRIT)
);
$logger->addWriter($writer);

结论 (Conclusion)

In this article, I’ve shown how you can send ad-hoc push notifications to yourself or others using Prowl. I’ve demonstrated sending critical log messages direct to your mobile phone, and there are all sorts of things you can use Prowl notifications for. Here are some more examples:

在本文中,我展示了如何使用Prowl向您自己或其他人发送临时推送通知。 我已经演示了将关键日志消息直接发送到您的手机,并且您可以使用Prowl通知进行各种操作。 这里还有更多示例:

  • Multi-factor authentication

    多因素认证
  • New email or comment notifications

    新的电子邮件或评论通知
  • Group messaging

    群组讯息
  • Security alerts (e.g. intrusion detection)

    安全警报(例如入侵检测)

Perhaps you can think of more? Let me know in the comments below!

也许您还能想到更多? 在下面的评论中让我知道!

翻译自: https://www.sitepoint.com/push-notifications-with-prowl/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值