nstdio / notymo
此包已被废弃且不再维护。没有建议的替代包。
最新版本(dev-master)的此包没有可用的许可信息。
Android和iOS推送通知库。
dev-master
2017-06-29 18:16 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2020-01-16 23:39:10 UTC
README
notymo 是一个库,可以帮助您使用单一接口在iOS和Android设备上发送推送通知。该库没有外部依赖。
安装
建议的安装方法是使用 composer
$ composer require nstdio/notymo: "dev-master"
或者添加
"nstdio/notymo": "dev-master"
到您的 composer.json
文件的 require
部分中。
使用
单一接口
use nstdio\notymo\Message; use nstdio\notymo\PushNotification; $push = new PushNotification(array( // If you dоn't want to use one of the services we can just skip them loading. // It's obvious that the skipped service is not necessary to configure. // 'skipApns' => true, // 'skipGcm' => true, 'apns' => array( 'live' => true, // We need to connect to APNS production server 'cert' => 'live_cert.pem' // Also we must specify a SSL certificate for sending notification to iOS devices. ), 'gcm' => array( 'apiKey' => 'api_key' // Google GCM Service API key. ), ) ); /** * If we have multiple recipients and all of them should receive same data we can create * one single instance of Message class and send messages at once. */ $msg = Message::android(); $msg->setMessage("You have a notification."); $msg->setSound("default"); $msg->setBadge(2); $msg->setCustomData(array("user_data" => array())); $msg->setToken(range(0, 10000)); /** * Just clone original message and replace old device's tokens with new once for iOS devices. */ $msg2 = $msg->cloneWith(Message::TYPE_IOS, range(10000, 20000)); $push->enqueue($msg); $push->enqueue($msg2); // Adding messages to queue $push->send(); // Send notifications.
iOS
use nstdio\notymo\APNSNotification; use nstdio\notymo\Message; $apns = new APNSNotification(true, 'live_cert.pem'); $msg = Message::ios(); $msg->setMessage("This notification sent by cron."); $msg->setSound("bang_bang"); $msg->setCustomData(array("segue" => "toSignInView")); $msg->setToken(range(0, 10000)); // $apns->enqueue($msg); // Adding messages to queue $apns->send(); // Send notifications.
Android
use nstdio\notymo\GCMNotification; use nstdio\notymo\Message; $gcm = new GCMNotification("gcm_api_key"); $msg = Message::ios(); // ... same story as in iOS example. $msg->setToken(range('A', 'Z')); $gcm->enqueue($msg); $gcm->send();
应用生命周期回调
LifeCycleCallback
方法 | 注释 | 回调签名 |
---|---|---|
void onComplete(Closure $callback) |
当所有消息发送完成后将被调用。 | void function(MessageQueue $messages) |
void onEachSent(Closure $callback) |
当每条消息发送后将被调用。 | void function(MessageInterface $message, array $response) |
void onError(Closure $callback) |
当发生错误时将被调用。注意,当发生错误且未定义此回调时,将抛出异常。 | void function(MessageInterface $message, PushNotificationException $exc) |
void detach() |
此方法没有 Closure 参数,因为它不涉及消息发送的生命周期。将此方法单独赋值用于移除回调。将在 onSent 之后立即调用。 |
- |
// ... $push->onComplete(function(MessageQueue $queue) { /** @var MessageInterface $message */ foreach ($queue as $message) { printf("Message %s not sent\n", $message->getToken()) } }); $push->onSent(function(MessageInterface $message, $response) use ($model) { $model->save(array( 'device_token' => $message->getToken(), 'is_sent' => $response['success'], )); }); $push->onError(function(MessageInterface $message, PushNotificationException $e) { printf("Error %s occurs while sending %s\n", $message->getToken(), $e->getMessage()); }); $push->send();