android_unicast_schedule.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. require_once(APP_ROOT_PATH.'system/libs/schedule.php');
  3. class android_unicast_schedule implements schedule {
  4. /**
  5. * $data 格式
  6. * array("dest"=>device_tokens,"content"=>序列化的消息配置);
  7. */
  8. public function exec($data){
  9. require_once(APP_ROOT_PATH. 'system/umeng/notification/android/AndroidUnicast.php');
  10. try {
  11. $appMasterSecret = $GLOBALS['db']->getOne("select val from ".DB_PREFIX."m_config where code = 'android_master_secret'");
  12. $appkey = $GLOBALS['db']->getOne("select val from ".DB_PREFIX."m_config where code = 'android_app_key'");
  13. $title = app_conf("SITE_NAME")?app_conf("SITE_NAME"):"直播";
  14. $listcast = new AndroidUnicast();
  15. $listcast->setAppMasterSecret($appMasterSecret);
  16. $listcast->setPredefinedKeyValue("appkey", $appkey);
  17. $listcast->setPredefinedKeyValue("timestamp", strval(time()));// 必填 时间戳,10位或者13位均可,时间戳有效期为10分钟 NOW_TIME
  18. // Set your device tokens here
  19. $listcast->setPredefinedKeyValue("device_tokens", trim($data['dest']));
  20. $listcast->setPredefinedKeyValue("ticker", $data['content']);//必填 通知栏提示文字
  21. $listcast->setPredefinedKeyValue("title", $title);// 必填 通知标题
  22. $listcast->setPredefinedKeyValue("text", $data['content']);// 必填 通知文字描述
  23. $listcast->setPredefinedKeyValue("after_open", "go_app");//"go_app": 打开应用;"go_url": 跳转到URL;"go_activity": 打开特定的activity;"go_custom": 用户自定义内容。
  24. // Set 'production_mode' to 'false' if it's a test device.
  25. // For how to register a test device, please see the developer doc.
  26. $listcast->setPredefinedKeyValue("production_mode", "true");//可选 正式/测试模式。测试模式下,只会将消息发给测试设备。
  27. // Set extra fields
  28. $listcast->setExtraField("type", $data['type']);
  29. if ($data['type']==5) {
  30. $listcast->setExtraField("url", $data['url']);
  31. }
  32. //print("Sending unicast notification, please wait...\r\n");
  33. //json_decode($data) {"ret":"SUCCESS","data":{"msg_id":"uu05362143574400482600"}}
  34. $result = $listcast->send();
  35. $res = json_decode($result,1);
  36. //print("Sent SUCCESS\r\n");
  37. if ($res['ret'] == 'SUCCESS'){
  38. $is_success = 1;
  39. }else{
  40. $is_success = 0;
  41. $message = addslashes(print_r($result,true));
  42. }
  43. } catch (Exception $e) {
  44. $is_success = 0;
  45. $message = addslashes($e->getMessage());
  46. }
  47. $result = array();
  48. $result['status'] = $is_success;
  49. $result['attemp'] = 0;
  50. $result['info'] = $message;
  51. $result['res'] = $res;
  52. return $result;
  53. }
  54. }
  55. ?>