PHP友盟推送消息踩坑及处理

2020-08-02 来源: 原万里 发布在  https://www.cnblogs.com/yuanwanli/p/12661128.html

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

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

消息推送代码:

 <?php
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidBroadcast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidFilecast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidGroupcast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidUnicast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/android/AndroidCustomizedcast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSBroadcast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSFilecast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSGroupcast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSUnicast.php');
 require_once(dirname(__FILE__) . '/' . 'umengmsg/notification/ios/IOSCustomizedcast.php');

 class Be_Libs_UmengMsg {
     protected static $appkey  = '';
     protected static $appMasterSecret = '';
     protected static $validation_token = NULL;
     protected static $device_tokens = '';
   
     public static function __init($type) {
         $configs = Be_Config::k('umeng.'.$type); //根据IOS/Android获取appkey和密钥
         self::$appkey = $configs['appkey'];
         self::$appMasterSecret = $configs['appMasterSecret'];
     }
    //Android 单播
     public static function sendAndroidUnicast($data) {
         self::__init('android');
         try {
             $unicast = new AndroidUnicast();
             $unicast->setAppMasterSecret(self::$appMasterSecret);
             $unicast->setPredefinedKeyValue("appkey",self::$appkey);
             $unicast->setPredefinedKeyValue("timestamp",strval(time()));
             $unicast->setPredefinedKeyValue("device_tokens",$data['token']);
             $unicast->setPredefinedKeyValue("ticker",$data['title']);
             $unicast->setPredefinedKeyValue("title",$data['title']);
             $unicast->setPredefinedKeyValue("text",$data['content']);
             $unicast->setPredefinedKeyValue("after_open","go_app");
             $unicast->setPredefinedKeyValue("production_mode", "false");
             $unicast->setPredefinedKeyValue("mipush",true);
             $unicast->setPredefinedKeyValue("mi_activity",'xxx');
             $unicast->setExtraField("scheme",$data['scheme']);
             $unicast->setExtraField("badge",$data['badge']);
             $unicast->send();
         } catch (Exception $e) {
             return $e->getMessage();
         }
         return true;
     }
    //Android广播
     public static function sendAndroidBroadcast($data) {
         self::__init(strtolower($data['platform']));
         try {
             $brocast = new AndroidBroadcast();
             $brocast->setAppMasterSecret(self::$appMasterSecret);
             $brocast->setPredefinedKeyValue("appkey", self::$appkey);
             $brocast->setPredefinedKeyValue("timestamp", strval(time()));
             $brocast->setPredefinedKeyValue("ticker", '测试副标题');
             $brocast->setPredefinedKeyValue("title",  '测试标题');
             $brocast->setPredefinedKeyValue("text",  '测试描述');
             $brocast->setPredefinedKeyValue("after_open", "go_url");
             $brocast->setPredefinedKeyValue("url",  'http://www.baidu.com');
             $brocast->setPredefinedKeyValue("production_mode", "false");
             $brocast->setExtraField("scheme", "xxxx");
             $brocast->send();
         } catch (Exception $e) {
             return $e->getMessage();
         }
         return true;
     }
    //IOS单播
     public static function sendIOSUnicast($data) {
         self::__init(strtolower('ios'));
         $alert = array(
             "title" => $data['title'],
             "subtitle" => $data['subtitle'],
             "body" => $data['content'],
             "url" => $data['url'],
             "scheme" => $data['scheme'],
         );
         try {
             $unicast = new IOSUnicast();
             $unicast->setAppMasterSecret(self::$appMasterSecret);
             $unicast->setPredefinedKeyValue("appkey",self::$appkey);
             $unicast->setPredefinedKeyValue("timestamp",strval(time()));
             $unicast->setPredefinedKeyValue("device_tokens",$data['token']);
             $unicast->setPredefinedKeyValue("badge",$data['badge']);
             $unicast->setPredefinedKeyValue("sound", "default");
             $unicast->setPredefinedKeyValue("alert", $alert);
             $unicast->setPredefinedKeyValue("production_mode", "false");
             $unicast->send();
         } catch (Exception $e) {
             return $e->getMessage();
         }
         return true;
     }
    //IOS广播
     public static function sendIOSBroadcast($data) {
         self::__init(strtolower($data['platform']));
         $alert = array(
             "title" => '测试标题',
             "subtitle" => '测试副标题',
             "body" => '测试描述',
             "url" => 'http://www.baidu.com',
             "scheme" => "xxxx",
         );
         try {
             $brocast = new IOSBroadcast();
             $brocast->setAppMasterSecret($this->appMasterSecret);
             $brocast->setPredefinedKeyValue("appkey", $this->appkey);
             $brocast->setPredefinedKeyValue("timestamp", strval(time()));
             $brocast->setPredefinedKeyValue("alert", $alert);
             $brocast->setPredefinedKeyValue("badge", 0);
             //$brocast->setPredefinedKeyValue("sound", "chime");
             // Set 'production_mode' to 'true' if your app is under production mode
             $brocast->setPredefinedKeyValue("production_mode", "false");
             // Set customized fields
             //$brocast->setCustomizedField("test", "helloworld");
             $brocast->send();
         } catch (Exception $e) {
             return $e->getMessage();
         }
         return true;
     }

 }

采坑纪录:

  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");

  

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

    

相关文章