项目作者: nstdio

项目描述 :
Send iOS and Andriod push notifications.
高级语言: PHP
项目地址: git://github.com/nstdio/notymo.git
创建时间: 2016-10-01T10:52:25Z
项目社区:https://github.com/nstdio/notymo

开源协议:

下载


notymo Build Status Scrutinizer Code Quality Code Coverage

The notymo is a library which can help you to send push notifications on iOS and Andriod devices using single interface. The Library has no external dependencies.

Installation

The suggested installation method is via composer:

  1. $ composer require nstdio/notymo: "dev-master"

or add

  1. "nstdio/notymo": "dev-master"

to the require section of your composer.json file.

Usage

Single Interface

  1. use nstdio\notymo\Message;
  2. use nstdio\notymo\PushNotification;
  3. $push = new PushNotification(array(
  4. // If you dоn't want to use one of the services we can just skip them loading.
  5. // It's obvious that the skipped service is not necessary to configure.
  6. // 'skipApns' => true,
  7. // 'skipGcm' => true,
  8. 'apns' => array(
  9. 'live' => true, // We need to connect to APNS production server
  10. 'cert' => 'live_cert.pem' // Also we must specify a SSL certificate for sending notification to iOS devices.
  11. ),
  12. 'gcm' => array(
  13. 'apiKey' => 'api_key' // Google GCM Service API key.
  14. ),
  15. )
  16. );
  17. /**
  18. * If we have multiple recipients and all of them should receive same data we can create
  19. * one single instance of Message class and send messages at once.
  20. */
  21. $msg = Message::android();
  22. $msg->setMessage("You have a notification.");
  23. $msg->setSound("default");
  24. $msg->setBadge(2);
  25. $msg->setCustomData(array("user_data" => array()));
  26. $msg->setToken(range(0, 10000));
  27. /**
  28. * Just clone original message and replace old device's tokens with new once for iOS devices.
  29. */
  30. $msg2 = $msg->cloneWith(Message::TYPE_IOS, range(10000, 20000));
  31. $push->enqueue($msg);
  32. $push->enqueue($msg2); // Adding messages to queue
  33. $push->send(); // Send notifications.

iOS

  1. use nstdio\notymo\APNSNotification;
  2. use nstdio\notymo\Message;
  3. $apns = new APNSNotification(true, 'live_cert.pem');
  4. $msg = Message::ios();
  5. $msg->setMessage("This notification sent by cron.");
  6. $msg->setSound("bang_bang");
  7. $msg->setCustomData(array("segue" => "toSignInView"));
  8. $msg->setToken(range(0, 10000)); //
  9. $apns->enqueue($msg); // Adding messages to queue
  10. $apns->send(); // Send notifications.

Android

  1. use nstdio\notymo\GCMNotification;
  2. use nstdio\notymo\Message;
  3. $gcm = new GCMNotification("gcm_api_key");
  4. $msg = Message::ios();
  5. // ... same story as in iOS example.
  6. $msg->setToken(range('A', 'Z'));
  7. $gcm->enqueue($msg);
  8. $gcm->send();

Applying lifecycle callbacks

LifeCycleCallback

Method Comment Callback signature
void onComplete(Closure $callback) Will be called when all messages are sent. void function(MessageQueue $messages)
void onEachSent(Closure $callback) Will be called when the every message was sent. void function(MessageInterface $message, array $response)
void onError(Closure $callback) Will be called when error occurs. Note that when error occured and this callback is not defined, an exception will be thrown. void function(MessageInterface $message, PushNotificationException $exc)
void detach() This method has no Closure argument because it is not involved in the message sending lifecycle. The single assignment of this method to remove callbacks. Will be called immediately after onSent. -
  1. // ...
  2. $push->onComplete(function(MessageQueue $queue) {
  3. /** @var MessageInterface $message */
  4. foreach ($queue as $message) {
  5. printf("Message %s not sent\n", $message->getToken())
  6. }
  7. });
  8. $push->onSent(function(MessageInterface $message, $response) use ($model) {
  9. $model->save(array(
  10. 'device_token' => $message->getToken(),
  11. 'is_sent' => $response['success'],
  12. ));
  13. });
  14. $push->onError(function(MessageInterface $message, PushNotificationException $e) {
  15. printf("Error %s occurs while sending %s\n", $message->getToken(), $e->getMessage());
  16. });
  17. $push->send();