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

杏奴
• 阅读 4188

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

代码

<?php

namespace Notification;

//引入核心文件
require_once('./Application/Common/Lib/notification/android/AndroidBroadcast.php');
require_once('./Application/Common/Lib/notification/android/AndroidFilecast.php');
require_once('./Application/Common/Lib/notification/android/AndroidGroupcast.php');
require_once('./Application/Common/Lib/notification/android/AndroidUnicast.php');
require_once('./Application/Common/Lib/notification/android/AndroidCustomizedcast.php');
require_once('./Application/Common/Lib/notification/android/AndroidListcast.php');
require_once('./Application/Common/Lib/notification/ios/IOSBroadcast.php');
require_once('./Application/Common/Lib/notification/ios/IOSFilecast.php');
require_once('./Application/Common/Lib/notification/ios/IOSGroupcast.php');
require_once('./Application/Common/Lib/notification/ios/IOSUnicast.php');
require_once('./Application/Common/Lib/notification/ios/IOSCustomizedcast.php');
require_once('./Application/Common/Lib/notification/ios/IOSListcast.php');


class Sms
{
    protected $appkey = NULL;
    protected $appMasterSecret = NULL;
    protected $timestamp = NULL;
    protected $validation_token = NULL;

    function __construct($key, $secret)
    {
        //android
        $this->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.xxx.xxx.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里面没有列播,网上也没有资料,客服就更别说了...

  1. ios的自定义播送的alert要传递数组
  2. 离线推送这两个字段需要在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");

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

友盟U-push参数解析

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

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


点赞
收藏
评论区
推荐文章
linbojue linbojue
1年前
超完整的Electron打包签名更新指南,这真是太酷啦!
大家好,我是多喝热水。在踩了数不清的坑之后,终于从0到1完成了一个桌面端应用,但万万没想到,最最痛苦的还不是开发过程,而是开发完成后的打包签名阶段,这真是踩坑踩麻了!!!超完整的Electron打包签名更新指南,这真是太酷啦!ok,踩坑归踩坑,收获也是不小
Easter79 Easter79
4年前
toLua踩坑
新博客:https://yinl.fun(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fyinl.fun)欢迎关注,同步更新toLua踩坑篇最近工作得用Lua实现逻辑,桥梁用的toLua,踩了很多坑,在这里记录一下。坑~toLua解析Lua属性
学python的猫 学python的猫
4年前
这些常见的坑,90%的程序猿都踩过,来看看里面有没有你的脚印?
在学习python的过程中,相信大家都有踩过不少的坑,有些坑可能踩了不止一次,感觉就像是在坑与坑之间反复横跳。那么如何避免这些坑呢?看完这篇文章,你就知道了。我们来谈谈我们学习python的过程中,最常见的七大坑:1.缩进,符号和空格不正确写代码时大家会使用缩进、对齐、空格等,这些是为了提高代码的可读性在python语言中,缩进是十分重要的比如在创建一个新
Wesley13 Wesley13
4年前
umeng push dex class conflict with alipay lib jar
友盟推送SDK为了提高设备标识的唯一性,除了UMID之外,还用到了Taobao提供的一个设备标识生成库(UTDID.jar)做双向保证。当前推送SDK兼容的UDID版本是V1.1.0。淘宝提供的设备标识生成库(UTDID.jar)已经广泛应用在阿里系的App中了,包括支付宝。部分集成过支付宝SDK的App,在集成友盟推送SDK的时候,会存在包冲突的问题
Stella981 Stella981
4年前
Groovy防PermOOM与OldOOM心得
作为Groovy重度用户,踩了新版本因为无法unloadclass导致permoom的坑,踩了classLoader.parallelLockMap不断添加新锁导致oldoom的坑。本文的意图就是记录一点埋坑心得。踩坑详情可见:https://my.oschina.net/chenxiaojie/blog/835934(https://my.o
Wesley13 Wesley13
4年前
thinkphp整合系列之友盟消息推送
上篇文章 thinkphp集成系列之phpmailer批量发送邮件(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fbaijunyao.com%2Farticle%2F69)讲过的;邮件有着零成本、内容丰富的优点;但是一个非常硬的硬伤;这家伙的及时性太差了;尤其是随着90、00
Wesley13 Wesley13
4年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Stella981 Stella981
4年前
Ecshop用户中心的收藏列表里显示商品缩略图
1)、修改includes/lib\_clips.php文件将下面代码$sql'SELECTg.goods_id,g.goods_name,g.market_price,g.shop_priceASorg_price,'.修改为$sql'SELECTg.goods_id,g.goods_nam
Caomeinico Caomeinico
4年前
(转)友盟+短视频行业研究
好买网(www.goodmai.com)2019年1月15日,字节跳动的陈林、云歌人工智能科技的王欣和北京快如科技的罗永浩分别发布了三款社交产品:主打短视频社交的多闪、主打匿名社交的马桶MT和转型为网赚模式的聊天宝。2个月后硝烟散尽,但见一地鸡毛。马桶“夭折”,聊天宝“解散”,对于短视频社交应用“多闪”,好评并不多见,“画地圈人”的质疑倒是若隐若现。友盟基
Stella981 Stella981
4年前
Node.js 中使用 ECDSA 签名遇到的坑
文/Fenying最近有个朋友问我关于Node.js下使用ECDSA的问题,主要是使用Node.js的Crypto模块无法校验网络传输过来的签名结果。在踩坑无数后,终于搞清楚了原因。坑0x00:签名输出格式在排除了证书、消息不一致的可能之后,我开始对比使用Node.js签名的结果与网络传输过来的签
Stella981 Stella981
4年前
Redis中的Scan命令踩坑记
1原本以为自己对redis命令还蛮熟悉的,各种数据模型各种基于redis的骚操作。但是最近在使用redis的scan的命令式却踩了一个坑,顿时发觉自己原来对redis的游标理解的很有限。所以记录下这个踩坑的过程,背景如下:公司因为redis服务器内存吃紧,需要删除一些无用的没有设置过期时间的key。大概有500多w的key。虽然key的数目听起来
杏奴
杏奴
Lv1
海边的日落就是这么优雅,这么精彩!
文章
11
粉丝
0
获赞
0