zoukankan      html  css  js  c++  java
  • PHP友盟推送消息踩坑及处理

    公司的客户端的推送选用友盟推送,但是友盟的官方文档描述很少,对新手很不友好,所以特写此采坑纪录,废话不多说上代码。

    公司业务只涉及单播和广播。所以只提供了单播和广播,业务拓展的话会补充其余部分。

    消息推送代码:

      1 <?php
      2 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidBroadcast.php');
      3 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidFilecast.php');
      4 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidGroupcast.php');
      5 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidUnicast.php');
      6 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidCustomizedcast.php');
      7 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSBroadcast.php');
      8 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSFilecast.php');
      9 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSGroupcast.php');
     10 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSUnicast.php');
     11 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSCustomizedcast.php');
     12 
     13 
     14 class Be_Libs_UmengMsg {
     15     protected static $appkey  = ''; 
     16     protected static $appMasterSecret = '';
     17     protected static $validation_token = NULL;
     18     protected static $device_tokens = '';
     19   
     20     public static function __init($type) {
     21         $configs = Be_Config::k('umeng.'.$type); //根据IOS/Android获取appkey和密钥
     22         self::$appkey = $configs['appkey'];
     23         self::$appMasterSecret = $configs['appMasterSecret'];
     24     }
     25    //Android 单播
     26     public static function sendAndroidUnicast($data) {
     27         self::__init('android');
     28         try {
     29             $unicast = new AndroidUnicast();
     30             $unicast->setAppMasterSecret(self::$appMasterSecret);
     31             $unicast->setPredefinedKeyValue("appkey",self::$appkey);
     32             $unicast->setPredefinedKeyValue("timestamp",strval(time()));
     33             $unicast->setPredefinedKeyValue("device_tokens",$data['token']); 
     34             $unicast->setPredefinedKeyValue("ticker",$data['title']);
     35             $unicast->setPredefinedKeyValue("title",$data['title']);
     36             $unicast->setPredefinedKeyValue("text",$data['content']);
     37             $unicast->setPredefinedKeyValue("after_open","go_app");
     38             $unicast->setPredefinedKeyValue("production_mode", "false");
     39             $unicast->setPredefinedKeyValue("mipush",true);
     40             $unicast->setPredefinedKeyValue("mi_activity",'xxx');
     41             $unicast->setExtraField("scheme",$data['scheme']);
     42             $unicast->setExtraField("badge",$data['badge']);
     43             $unicast->send();
     44         } catch (Exception $e) {
     45             return $e->getMessage();
     46         }
     47         return true;
     48     }
     49    //Android广播
     50     public static function sendAndroidBroadcast($data) {
     51         self::__init(strtolower($data['platform']));
     52         try {
     53             $brocast = new AndroidBroadcast();
     54             $brocast->setAppMasterSecret(self::$appMasterSecret);
     55             $brocast->setPredefinedKeyValue("appkey", self::$appkey);
     56             $brocast->setPredefinedKeyValue("timestamp", strval(time()));
     57             $brocast->setPredefinedKeyValue("ticker", '测试副标题');
     58             $brocast->setPredefinedKeyValue("title",  '测试标题');
     59             $brocast->setPredefinedKeyValue("text",  '测试描述');
     60             $brocast->setPredefinedKeyValue("after_open", "go_url");
     61             $brocast->setPredefinedKeyValue("url",  'http://www.baidu.com');
     62             $brocast->setPredefinedKeyValue("production_mode", "false");
     63             $brocast->setExtraField("scheme", "xxxx");
     64             $brocast->send();
     65         } catch (Exception $e) {
     66             return $e->getMessage();
     67         }
     68         return true;
     69     }
     70    //IOS单播
     71     public static function sendIOSUnicast($data) {
     72         self::__init(strtolower('ios'));
     73         $alert = array(
     74             "title" => $data['title'],
     75             "subtitle" => $data['subtitle'],
     76             "body" => $data['content'],
     77             "url" => $data['url'],
     78             "scheme" => $data['scheme'],
     79         );
     80         try {
     81             $unicast = new IOSUnicast();
     82             $unicast->setAppMasterSecret(self::$appMasterSecret);
     83             $unicast->setPredefinedKeyValue("appkey",self::$appkey);
     84             $unicast->setPredefinedKeyValue("timestamp",strval(time()));
     85             $unicast->setPredefinedKeyValue("device_tokens",$data['token']); 
     86             $unicast->setPredefinedKeyValue("badge",$data['badge']);
     87             $unicast->setPredefinedKeyValue("sound", "default");
     88             $unicast->setPredefinedKeyValue("alert", $alert);
     89             $unicast->setPredefinedKeyValue("production_mode", "false");
     90             $unicast->send();
     91         } catch (Exception $e) {
     92             return $e->getMessage();
     93         }
     94         return true;    
     95     }
     96    //IOS广播
     97     public static function sendIOSBroadcast($data) {
     98         self::__init(strtolower($data['platform']));
     99         $alert = array(
    100             "title" => '测试标题',
    101             "subtitle" => '测试副标题',
    102             "body" => '测试描述',
    103             "url" => 'http://www.baidu.com',
    104             "scheme" => "xxxx",
    105         );
    106         try {
    107             $brocast = new IOSBroadcast();
    108             $brocast->setAppMasterSecret($this->appMasterSecret);
    109             $brocast->setPredefinedKeyValue("appkey", $this->appkey);
    110             $brocast->setPredefinedKeyValue("timestamp", strval(time()));
    111             $brocast->setPredefinedKeyValue("alert", $alert);
    112             $brocast->setPredefinedKeyValue("badge", 0);
    113             //$brocast->setPredefinedKeyValue("sound", "chime");
    114             // Set 'production_mode' to 'true' if your app is under production mode
    115             $brocast->setPredefinedKeyValue("production_mode", "false");
    116             // Set customized fields
    117             //$brocast->setCustomizedField("test", "helloworld");
    118             $brocast->send();
    119         } catch (Exception $e) {
    120             return $e->getMessage();
    121         }
    122         return true;
    123     }
    124 
    125 }

    采坑纪录:

      1.IOS传值问题

        IOS传自定义参数的方式与Android不同,需要使用alert参数,自定义参数使用数组方式。详见IOS单播。

      2.Android设置 MIUI、EMUI、Flyme系统设备离线转为系统下发 报错

    $unicast->setPredefinedKeyValue("mipush",true);
    $unicast->setPredefinedKeyValue("mi_activity",'xxx');

      直接在代码中添加这两个参数会报错。处理方法:

      需要在UmengNotification.php文件内添加两个字段,否则会报错。

      原代码:

    protected $DATA_KEYS = array("appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id", "filter", "production_mode",
            "feedback", "description", "thirdparty_id");

      改为:

    protected $DATA_KEYS = array("appkey", "timestamp", "type", "device_tokens", "alias", "alias_type", "file_id", "filter", "production_mode",
            "feedback", "description", "thirdparty_id", "mipush", "mi_activity");

      

    详情的参数不知道的小伙伴可以私聊~

        

  • 相关阅读:
    通讯总线 | 串口
    shell | 命令实用汇集
    编码 | 宏定义格式化日志信息
    shell | 脚本传参
    注释 | 代码注释原则
    GCC编译优化选项介绍
    shell | 已知进程查找命令
    网络 | ifconfig 配置 IP 掩码 网关
    Makefile | 使用eclipse软件自动生成Makefile文件
    VIO系统的IMU与相机时间偏差标定
  • 原文地址:https://www.cnblogs.com/yuanwanli/p/12661128.html
Copyright © 2011-2022 走看看