前言:
最近使用hbuilder开发的一个app中用到了个推功能,在此期间,翻看了DCloud和百度上一些有关个推的问题,目前这种例子太少,以至于遇到问题,百度也搜不到标准答案。


这次后台用的java语言,app用的mui框架,推送目标既有android,也有ios。
首先,在hbuilder上集成个推前,先仔细参照

manifest.json

中plus.push推送SDK配置指南(下图)

hbuilder如何自动打开微信开发者工具 hbuilder开发app调用api_个推

阅读完配置指南,接下来:

1、你可以先在个推开放平台,使用平台自带的demo,安装到自己手机中,在个推官网测试推送,这个非常简单,这里不在说明。

2、新建一个带有各种H5+例子的app项目,自己测试推送。

3、在个推开放平台新建应用,自动获取到appid,appkey等,

然后配置好下图中android的应用标识,需要ios推送再配置好ios应用证书(ios注册证书的流程在DCloud中有说明),

在需要集成个推的项目中,配置好appid,appkey等(注意有些地方要一致,配置说明教程中都有,自己注意),

打包发布,安装应用。

hbuilder如何自动打开微信开发者工具 hbuilder开发app调用api_webapp_02

安装完应用后,在个推官网下载java版sdk(下图)

hbuilder如何自动打开微信开发者工具 hbuilder开发app调用api_个推_03

参照API文档>>快速入门.pdf中的流程,
使用javaSDK测试推送,各项无误的话,很简单就能推送成功(注意:流程中使用的是 对指定应用群推消息)。

最后把所有文件阅读一遍,熟悉各个功能和接口。

推送模板中有4种,各位可以根据自己情况自行选择。

消息推送方式中有8种,

博主根据业务需求选择的是对指定列表用户推送消息。

代码直接复制到后台。

前台可以把配置说明中(下图)的代码修改下,放到自己项目中。

hbuilder如何自动打开微信开发者工具 hbuilder开发app调用api_hbuilder_04

最后需要打包测试,真机运行不支持推送。

如何获取到实际的clientid(安卓)或者token(苹果),在H5+功能接口中有说明;

plus.push.getClientInfo().clientid;
plus.push.getClientInfo().token;

clientid和token也需要实际打包后获取,真机测试可能会不一致。

最后说明几点:

1、安卓下:

应用的进程被kill后,无法收到消息;如果推送时app进程没有运行,再次打开app时才能显示消息,所以需要保证其进程运行状态下测试。

2、苹果下:
应用在线时,推送的消息不会进入系统通知栏。

如果不是通过APS通道发送的,页面中需要监听“receive”事件。

如果是通过APS通道发送的,需要在在“receive”事件回调的“PushMesage”对象中获取“aps”属性的值。

应用不在线时,后台发送的推送先发送到ios的APS服务器上,其次转发到终端应用上,显示在系统通知栏。

后台代码:

import java.util.ArrayList;
import java.util.List;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.APNTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;

public class test
{
    // 定义常量, appId、appKey、masterSecret
    private static String appId = "";
    private static String appKey = "";
    private static String masterSecret = "";
    //android的clientid
    static String clientid = "";
    //ios的token
    static String token = "";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";

    @SuppressWarnings("deprecation")
    public static NotificationTemplate temp()
    {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 设置通知栏标题与内容
        template.setTitle("安卓推送标题");
        template.setText("安卓推送内容");
        // 配置通知栏图标
        template.setLogo("icon.png");
        // 配置通知栏网络图标
        template.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent("{'title':'安卓透传标题','content':'安卓透传内容','payload':'安卓透传数据'}");
        return template;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public String pushToAndroid()
    {
        // 配置返回每个用户返回用户状态,可选
        System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        // 通知透传模板
        NotificationTemplate template = temp();
        ListMessage message = new ListMessage();
        message.setData(template);
        // 设置消息离线,并设置离线时间
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 配置推送目标
        List targets = new ArrayList();
        if (clientid.indexOf(",") != -1)
        {
            String[] cid = clientid.split(",");
            for (int i = 0; i < cid.length; i++)
            {
                Target target = new Target();
                target.setAppId(appId);
                target.setClientId(cid[i]);
                targets.add(target);
            }
        }
        else
        {
            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(clientid);
            targets.add(target);
        }
        try
        {
            // taskId用于在推送时去查找对应的message
            String taskId = push.getContentId(message);
            IPushResult ret = push.pushMessageToList(taskId, targets);
            System.out.println(ret.getResponse().toString());
            return ret.getResponse().toString();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "";
        }
    }

    public String pushToIos()
    {
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        APNTemplate t = new APNTemplate();
        APNPayload apnpayload = new APNPayload();
        apnpayload.setSound("");
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        //通知文本消息标题
        alertMsg.setTitle("aaaaaa");
        //通知文本消息字符串
        alertMsg.setBody("bbbb");
        //对于标题指定执行按钮所使用的Localizable.strings,仅支持IOS8.2以上版本
        alertMsg.setTitleLocKey("ccccc");
        //指定执行按钮所使用的Localizable.strings
        alertMsg.setActionLocKey("ddddd");
        apnpayload.setAlertMsg(alertMsg);

        t.setAPNInfo(apnpayload);  
        ListMessage message = new ListMessage();
        message.setData(t);
        String contentId = push.getAPNContentId(appId, message);
        System.out.println(contentId);
        List<String> dtl = new ArrayList<String>();
        dtl.add(token);
        System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
        IPushResult ret = push.pushAPNMessageToList(appId, contentId, dtl);
        System.out.println(ret.getResponse());
        return ret.getResponse().toString();
    }
}