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

公司客户端选定的友盟消息推送,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.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里面没有列播,网上也没有资料,客服就更别说了...

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

clipboard.png

友盟U-push参数解析

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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值