App push for mingyuanyun

1.2.1 2023-06-08 08:57 UTC

This package is auto-updated.

Last update: 2024-10-24 08:27:31 UTC


README

平台支持

环境需求

  • PHP >= 5.4
  • guzzlehttp/guzzle >= 6.0.0
  • yunchuang/php-jwt >= 1.0

安装

composer require yunchuang/push

使用

use MingYuanYun\Push\Push;

$iosCertContent =<<<EOF
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
EOF;

$config = [
    'huawei' => [
        'appPkgName' => '', // 包名
        'clientId' => '',
        'clientSecret' => ''
    ],
    'huawei-v2' => [
        'appPkgName' => '',
        'clientId' => '',
        'clientSecret' => ''
    ],
    'meizu' => [
        'appPkgName' => '',
        'appId' => '',
        'appSecret' => ''
    ],
    'xiaomi' => [
        'appPkgName' => '',
        'appSecret' => ''
    ],
    'oppo' => [
        'appPkgName' => '',
        'appKey' => '',
        'masterSecret' => ''
    ],
    'vivo' => [
        'appPkgName' => '',
        'appId' => '',
        'appKey' => '',
        'appSecret' => ''
    ],
    'ios' => [
        'isSandBox' => true, // 是否调试包
        'certPath' => '', // pem格式推送证书本地绝对路径
        'password' => '123', // 推送证书密码
    ],
    'ios-token' => [
        'isSandBox' => true,
        'teamId' => 'D4GSYVE6CN', // 开发者帐号teamId
        'keyId' => '99BYW4U4SZ', // token认证keyId
        'secretContent' => $iosCertContent, // 密钥内容,有值时忽略secretFile
        'secretFile' => 'xxx.p8', // token认证密钥文件本地绝对路径
        'bundleId' => 'com.mysoft.mdev' // 应用ID
    ],
    'jiguang' => [
        'appPkgName' => '',
        'appKey' => '',
        'masterSecret' => ''
    ]
];

$push = new Push($config);
$push->setPusher(通道名);
$push->pushNotice(设备token, 推送内容, 附加信息);

通道

目前支持以下通道:

  • huawei 华为
  • huawei-v2 华为新版
  • xiaomi 小米
  • meizu 魅族
  • oppo Oppo
  • vivo Vivo
  • ios 苹果(基于推送证书认证)
  • ios-token 苹果(基于token认证)
  • jiguang 极光

设备token

通过推送插件获得,支持以数组形式传入多个。

鉴于各厂商对多推的支持不一,建议单次最多100个设备。

推送内容

由于各厂商对推送的支持不一,现抽象定义了以下公有属性:

gatewayOptions厂商扩展参数说明

考虑到各厂商均有自己特有的参数,故提供此扩展参数来提供支持。如果扩展参数与通用参数有冲突,则取扩展参数中值。 参数类型为以厂商通道为键名的数组,格式如下:

[
    "huawei": 与华为消息格式一致,下同,
    "xiaomi": xxx,
    "vivo": xxx,
    "oppo": xxx,
    "meizu": xxx,
    "ios-token": xxx
]

示例

$message = [
    'businessId' => uniqid(),
    'title' => 'This is title',
    'content' => 'This is content',
    'extra' => [
        'key1' => 'v1',
        'key2' => 2
    ],
    'gatewayOptions' => [
        'xiaomi' => [
            'extra.notify_foreground' => '1',
        ],
        'huawei' => [
            'hps' => [
                'ext' => [
                    'badgeAddNum' => '1',
                    'badgeClass' => 'com.mysoft.core.activity.LauncherActivity',
                ]
            ]
        ]
    ],
];

附加信息

标题等长度限制说明

当前库将根据以下厂商限制进行处理,请调用方根据实际情况设置标题等内容

角标说明

目前仅华为、华为新版、ios、ios-token通道支持角标。其中华为新版支持角标累加,格式为+1,其他通道将只取数字值。

推送

// 华为推送
$push->setPusher('huawei');
print $push->pushNotice(
    '0864113036098917300002377300CN01',
    $message,
    ['token' => 'CFx88jTVr6adjsh6eVOLvhtqnDlhLxb7CljykbXxu7vLsnexatUJZM1lqXHPzfnurD0gknQnIu7SRvWhAPx/zQ==']
);


// 魅族推送
$push->setPusher('meizu');
print $push->pushNotice(
    ['ULY6c596e6a7d5b714a475a60527c6b5f7f655a6d6370'],
    $message
);

// 小米推送
$push->setPusher('xiaomi');
print $push->pushNotice(
    [
        'hncl+mMTtpA8BQZ66k7Fgpwa+ezlSL8AN/g8HKzTfg64GcTeTjY1C9bdrUcs2vR+',
        '0VcFXBPNTLifGLIYK+GdDAiOFJQ+uWAzkfs7QYtfszBgqFV720C8zli7mce1mHj6'
    ],
    $message
);

// Oppo推送
$push->setPusher('oppo');
$tokenInfo = $push->getAuthToken();
$options = [
    'token' => $tokenInfo['token']
];
print $push->pushNotice(
    'CN_40557c137ac2b5c68cbb8ac52616fefd',
    $message,
    $options
);

// Vivo推送
$push->setPusher('vivo');
print $push->pushNotice(
    [
        '15513410784181118114099',
    ],
    $message
);

// 苹果基于证书推送
$push->setPusher('ios');

$isSandBox = true;
$certPath = '/cert/path';
$password = 'cert_pwd';
$apnsPush = new \Apns\Client([$certPath, $password], $isSandBox);
$options = [
    'push' => $apnsPush
];

print $push->pushNotice(
    [
        '7438f5ba512cba4dcd1613e530a960cb862bd1c7ca70eae3cfe73137583c3c0d',
        '720772a4df1938b14d2b732ee62ce4e157577f8453d6021f81156aaeca7032ae',
    ],
    $message,
    $options
);

// 苹果基于token推送
$push->setPusher('ios-token');
print $push->pushNotice(
    [
        '7438f5ba512cba4dcd1613e530a960cb862bd1c7ca70eae3cfe73137583c3c0d',
        '720772a4df1938b14d2b732ee62ce4e157577f8453d6021f81156aaeca7032ae',
    ],
    $message
);

//极光推送
$push->setPusher('jiguang');
print $push->pushNotice(
    [
        '160a3797c912f272068',
    ],
    $message
);

认证

目前华为华为新版OppoVivoios-token推送前需要获取先获取认证token,且对获取频次均有限制,故统一提供了获取token方法getAuthToken,建议缓存认证token,过期时间较返回的有效时间短,比如2小时。

此方法返回格式如下:

[
    'token' => 认证token,
    'expires' => 有效时间,单位为秒
];

其余通道将返回null

缓存的认证token请以附加信息传入。

推送角标

目前仅ios ios-token 华为 华为新版支持推送角标

其中华为新版支持角标累加,badge格式为+ 5

返回值

除苹果通道外,其余通道推送成功时均将返回推送任务ID。

调用过程中有可能抛出以下异常,请注意捕获。

  • MingYuanYun\Push\Exceptions\GatewayErrorException
  • MingYuanYun\Push\Exceptions\InvalidArgumentException
  • MingYuanYun\Push\Exceptions\ResponseException

也可捕获上述异常的父类MingYuanYun\Push\Exceptions\Exception

自定义通道

本扩展支持自定义通道。

use MingYuanYun\Push\Gateways\Gateway;

class MyGateway extends Gateway
{
    // ...
}

// 注册
$push->extend('custom', function ($config) {
    return new MyGateway($config);
}

// config中添加通道配置
$config['custom'] = [];

// 调用
$push->setPusher('custom');
print $push->pushNotice(
    '0864113036098917300002377300CN01',
    $message
);

各通道配置参照$config

各通道回执示例

建议判断如果是字符串,则进行json_decode

  • ios-token
# 成功
{
    "businessId": "5cc55570a9faf",
    "deviceToken": "7438f5ba512cba4dcd1613e530a960cb862bd1c7ca70eae3cfe73137583c3c0d",
    "taskId": "3FC1E078-93CA-33BA-EC52-8E8A70AA0EB1",
    "status": "success"
}

# 失败
{
    "businessId": "5cc550a6d05ad",
    "deviceToken": "7438f5ba512cba4dcd1613e530a960cb862bd1c7ca70eae3cfe73137583c3c0d",
    "reason": "403 Forbidden for ExpiredProviderToken",
    "status": "fail"
}
  • huawei
{
    "statuses": [
        {
            "timestamp": 1552459811754,
            "token": "0864113036098917300002377300CN01",
            "appid": "100405075",
            "biTag": "",
            "status": 0,
            "requestId": "155245981150032647501"
        }
    ]
}
  • meizu
{
    "cb": "{\"NS20190313171303747_0_11579902_1_3-1\":{\"param\":\"\",\"type\":1,\"targets\":[\"S5Q4b726f7b466c797c584d54000503555c427160754b\"]}}",
    "access_token": "c68b05216e54409d95573912fad9c0de"
}
  • xiaomi
{
    "data": "{\"scm527795524699919378t\":{\"barStatus\":\"Enable\",\"type\":1,\"targets\":\"0VcFXBPNTLifGLIYK+GdDAiOFJQ+uWAzkfs7QYtfszBgqFV720C8zli7mce1mHj6\",\"timestamp\":1552470320982},\"scm52278552470202304Mq\":{\"barStatus\":\"Enable\",\"type\":1,\"targets\":\"0VcFXBPNTLifGLIYK+GdDAiOFJQ+uWAzkfs7QYtfszBgqFV720C8zli7mce1mHj6\",\"timestamp\":1552470321182}}"
}
  • vivo
{
    "555758542050050048": {
        "param": null,
        "targets": "15513410784181118114099"
    }
}
  • oppo
[
    {
        "registrationIds": "CN_768799ad17f2b564707db038dabb14b6",
        "messageId": "5c89f5d30980ff58c6ff9914",
        "taskId": "0000",
        "eventType": "push_arrive",
        "appId": "3000604"
    }
]

推送限额说明

Oppo、Vivo推送服务开通答疑

oppo vivo

注意

  • 各厂商设备token长度不一致,目前识别出华为最长为130个字符
  • 如果项目中使用了firebase/php-jwt,需要将此库移除。由于此库不支持ios-token要求的ES256算法,故从其PR中拉取出一个新的库yunchuang/php-jwt,对应官方的5.0.0版本,用法与官方一致
  • ios-token推送要求支持HTTP/2协议,另见 参照
  • iOS两种推送形式比较
  • 对于非明源云移动应用,若有extra参数的使用场景,请与原生开发人员确认Intent对应的schema信息,具体代码为src/Gateways/Gateway.php L110