资讯专栏INFORMATION COLUMN

PHP支持友盟消息推送踩坑记录

scq000 / 1580人阅读

摘要:公司客户端选定的友盟消息推送文档很少自己踩坑不少代码引入核心文件推送广播推送消息标题推送消息内容推送单播推送消息标题推送消息内容设备的值自定义

公司客户端选定的友盟消息推送,PHP文档很少,自己踩坑不少:

代码
andkey          = "andriodkey";
        $this->andMasterSecret = "andriodsecret";
        //ios
        $this->ioskey          = "ioskey";
        $this->iosMasterSecret = "iossecret";

        $this->timestamp = strval(time());
    }

    /**
     * Android推送—广播
     * @param $title   string 推送消息标题
     * @param $content string 推送消息内容
     * @return mixed
     */
    function sendAndroidBroadcast($title, $content)
    {
        try {
            $brocast = new AndroidBroadcast();
            $brocast->setAppMasterSecret($this->andMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->andkey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $brocast->setPredefinedKeyValue("ticker", "Android broadcast ticker");
            $brocast->setPredefinedKeyValue("title", $title);
            $brocast->setPredefinedKeyValue("text", $content);
            $brocast->setPredefinedKeyValue("after_open", "go_app");
            $brocast->setPredefinedKeyValue("production_mode", "true");
            $brocast->setExtraField("test", "helloworld");
            return $brocast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * Android推送—单播
     * @param $title   string 推送消息标题
     * @param $content string 推送消息内容
     * @param $tokens  array 设备的token值
     * @return mixed
     */
    function sendAndroidUnicast($title, $content, $tokens)
    {
        try {
            $unicast = new AndroidUnicast();
            $unicast->setAppMasterSecret($this->andMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->andkey);
            $unicast->setPredefinedKeyValue("mipush", true);
            $unicast->setPredefinedKeyValue("mi_activity", "cn.weidijia.pccm.ui.activity.SplashActivity");
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $unicast->setPredefinedKeyValue("device_tokens", $tokens);
            $unicast->setPredefinedKeyValue("ticker", "Android unicast ticker");
            $unicast->setPredefinedKeyValue("title", $title);
            $unicast->setPredefinedKeyValue("text", $content);
            $unicast->setPredefinedKeyValue("after_open", "go_app");
            $unicast->setPredefinedKeyValue("production_mode", "true");
            $unicast->setExtraField("test", "helloworld");
            return $unicast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /*
     * android自定义
     * */
    function sendAndroidCustomizedcast($alias, $alias_type, $ticker, $title, $text)
    {
        try {
            $customizedcast = new AndroidCustomizedcast();
            $customizedcast->setAppMasterSecret($this->andMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->andkey);
            $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $customizedcast->setPredefinedKeyValue("alias", $alias);
            $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
            $customizedcast->setPredefinedKeyValue("ticker", $ticker);
            $customizedcast->setPredefinedKeyValue("title", $title);
            $customizedcast->setPredefinedKeyValue("text", $text);
            $customizedcast->setPredefinedKeyValue("after_open", "go_app");
            return $customizedcast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * IOS推送—广播
     * @param $title   string 推送消息标题
     * @param $content string 推送消息内容
     * @return mixed
     */
    function sendIOSBroadcast($title, $content)
    {
        try {
            $brocast = new IOSBroadcast();
            $brocast->setAppMasterSecret($this->iosMasterSecret);
            $brocast->setPredefinedKeyValue("appkey", $this->ioskey);
            $brocast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $brocast->setPredefinedKeyValue("alert", $title);
            $brocast->setPredefinedKeyValue("badge", 0);
            $brocast->setPredefinedKeyValue("sound", "chime");
            $brocast->setPredefinedKeyValue("production_mode", "false");
            $brocast->setCustomizedField("test", $content);
            return $brocast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /**
     * IOS推送—单播
     * @param $title   string 推送消息标题
     * @param $content string 推送消息内容
     * @param $tokens  array 设备的token值
     * @return mixed
     */
    function sendIOSUnicast($title, $content, $tokens)
    {
        try {
            $unicast = new IOSUnicast();
            $unicast->setAppMasterSecret($this->iosMasterSecret);
            $unicast->setPredefinedKeyValue("appkey", $this->ioskey);
            $unicast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $unicast->setPredefinedKeyValue("device_tokens", $tokens);
            $unicast->setPredefinedKeyValue("alert", $title);
            $unicast->setPredefinedKeyValue("badge", 0);
            $unicast->setPredefinedKeyValue("sound", "chime");
            $unicast->setPredefinedKeyValue("production_mode", "false");
            $unicast->setCustomizedField("test", $content);
            return $unicast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

    /*
     * IOS自定义
     * */
    function sendIOSCustomizedcast($alias, $alias_type, $ticker, $title, $text)
    {
        $alert = [
            "title"    => $title,
            "subtitle" => $ticker,
            "body"     => $text
        ];
        try {
            $customizedcast = new IOSCustomizedcast();
            $customizedcast->setAppMasterSecret($this->iosMasterSecret);
            $customizedcast->setPredefinedKeyValue("appkey", $this->ioskey);
            $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
            $customizedcast->setPredefinedKeyValue("alias", $alias);
            $customizedcast->setPredefinedKeyValue("alias_type", $alias_type);
            $customizedcast->setPredefinedKeyValue("alert", $alert);
            $customizedcast->setPredefinedKeyValue("badge", 0);
            $customizedcast->setPredefinedKeyValue("sound", "chime");
            $customizedcast->setPredefinedKeyValue("production_mode", "false");
            return $customizedcast->send();
        } catch (Exception $e) {
            print("Caught exception: " . $e->getMessage());
        }
    }

}

andriod和ios使用不同的key和secret,申请就好.下载的demo里面没有列播,网上也没有资料,客服就更别说了...

ios的自定义播送的alert要传递数组

离线推送这两个字段需要在UmengNotification.php文件内添加这两个字段,否则会报错.

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

友盟U-push参数解析

3.使用下载的官方demo一定要吧print都注释掉.

第一篇写的很乱,不好意思


文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29069.html

相关文章

  • 使用友盟消息推送中遇到的哪些问题--索引(开发者必读)

    摘要:在客户端集成相关问题消息推送是如何实现的有消息通知栏收不到友盟消息推送需要打开嘛集成友盟推送获取不到后台显示消息发送成功,设备并没有收到消息如何统计的到达点击和忽略开发者仅需要自己统计自定义消息的点击和忽略其他的统计,均已由消息推送完成。 友盟推送服务器容量如何?每天耗电、耗流量多少?开发者自己的推送如何与友盟推送系统进行通讯?用户如何关闭推送服务?集成中有哪些坑?等等等等,汇总贴,供...

    trilever 评论0 收藏0
  • Android热修复实践应用--AndFix

    摘要:一直关注的热修复的技术发展,之前做的应用也没用使用到什么热修复开源框架。在的热修复框架没有流行之前,做的应用上线后发现一个小小的,就要马上发一个新的版本。将生成的补丁放到指定服务器上。 一直关注App的热修复的技术发展,之前做的应用也没用使用到什么热修复开源框架。在App的热修复框架没有流行之前,做的应用上线后发现一个小小的Bug,就要马上发一个新的版本。我亲身经历过一周发两个版本,真...

    cgspine 评论0 收藏0
  • 运营必看!依托友盟大数据优势,各类 App “双十一”推送攻略

    摘要:友盟推送后台数据显示,当在第一时间推送重大新闻时的打开率最高,最高甚至可以超过。友盟微推送结合友盟大数据优势,统计出固定天数不活跃的用户群。 双十一刚过,相信不少开发者小伙伴还沉浸在剁手的余温中,刚刚帮开发者在双十一当天推送了 191,991,9272 条消息的友盟消息推送团队,想借着双十一的热乎劲儿,为开发者小伙伴们带来一些实用的推送技巧!一起来看一看以下三种类型的 App 在这个双...

    URLOS 评论0 收藏0
  • 国内值得关注的官方API集合

    摘要:电影豆瓣电影豆瓣电影支持电影条目,影人条目,搜索和榜单等。音乐豆瓣音乐支持音乐信息,评论信息,标签信息,搜索音乐,某个音乐中标记最多的标签,发表修改删除评论,用户对音乐的所有标签等内容。 项目地址:https://github.com/marktony/A... 本页仅收集国内部分官方API,如需查看其他版本,请点击这里。 目录 笔记 出行 词典 电商 地图 电影 即时通讯 开发者网...

    gaomysion 评论0 收藏0
  • 国内值得关注的官方API集合

    摘要:电影豆瓣电影豆瓣电影支持电影条目,影人条目,搜索和榜单等。音乐豆瓣音乐支持音乐信息,评论信息,标签信息,搜索音乐,某个音乐中标记最多的标签,发表修改删除评论,用户对音乐的所有标签等内容。 项目地址:https://github.com/marktony/A... 本页仅收集国内部分官方API,如需查看其他版本,请点击这里。 目录 笔记 出行 词典 电商 地图 电影 即时通讯 开发者网...

    miqt 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<