AndroidNotification.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. require_once(APP_ROOT_PATH. 'system/umeng/notification/UmengNotification.php');
  3. abstract class AndroidNotification extends UmengNotification {
  4. // The array for payload, please see API doc for more information
  5. protected $androidPayload = array(
  6. "display_type" => "notification",
  7. "body" => array(
  8. "ticker" => NULL,
  9. "title" => NULL,
  10. "text" => NULL,
  11. //"icon" => "xx",
  12. //largeIcon => "xx",
  13. "play_vibrate" => "true",
  14. "play_lights" => "false",
  15. "play_sound" => "false",
  16. "after_open" => NULL,
  17. //"url" => "xx",
  18. //"activity" => "xx",
  19. //custom => "xx"
  20. ),
  21. "extra" => array(
  22. "room_id" => NULL,
  23. "type" => NULL,
  24. //"key2" => "value2"
  25. )
  26. );
  27. // Keys can be set in the payload level
  28. protected $PAYLOAD_KEYS = array("display_type");
  29. // Keys can be set in the body level
  30. protected $BODY_KEYS = array("ticker", "title", "text", "builder_id", "icon", "largeIcon", "img", "play_vibrate", "play_lights", "play_sound", "after_open", "url",
  31. "activity", "custom");
  32. function __construct() {
  33. parent::__construct();
  34. $this->data["payload"] = $this->androidPayload;
  35. }
  36. // Set key/value for $data array, for the keys which can be set please see $DATA_KEYS, $PAYLOAD_KEYS, $BODY_KEYS, $POLICY_KEYS
  37. function setPredefinedKeyValue($key, $value) {
  38. if (!is_string($key))
  39. throw new Exception("key should be a string!");
  40. if (in_array($key, $this->DATA_KEYS)) {
  41. $this->data[$key] = $value;
  42. } else if (in_array($key, $this->PAYLOAD_KEYS)) {
  43. $this->data["payload"][$key] = $value;
  44. if ($key == "display_type" && $value == "message") {
  45. $this->data["payload"]["body"]["ticker"] = "";
  46. $this->data["payload"]["body"]["title"] = "";
  47. $this->data["payload"]["body"]["text"] = "";
  48. $this->data["payload"]["body"]["after_open"] = "";
  49. if (!array_key_exists("custom", $this->data["payload"]["body"])) {
  50. $this->data["payload"]["body"]["custom"] = NULL;
  51. }
  52. }
  53. } else if (in_array($key, $this->BODY_KEYS)) {
  54. $this->data["payload"]["body"][$key] = $value;
  55. if ($key == "after_open" && $value == "go_custom" && !array_key_exists("custom", $this->data["payload"]["body"])) {
  56. $this->data["payload"]["body"]["custom"] = NULL;
  57. }
  58. } else if (in_array($key, $this->POLICY_KEYS)) {
  59. $this->data["policy"][$key] = $value;
  60. } else {
  61. if ($key == "payload" || $key == "body" || $key == "policy" || $key == "extra") {
  62. throw new Exception("You don't need to set value for ${key} , just set values for the sub keys in it.");
  63. } else {
  64. throw new Exception("Unknown key: ${key}");
  65. }
  66. }
  67. }
  68. // Set extra key/value for Android notification
  69. function setExtraField($key, $value) {
  70. if (!is_string($key))
  71. throw new Exception("key should be a string!");
  72. $this->data["payload"]["extra"][$key] = $value;
  73. }
  74. }