关于个推那点事(使用不介绍原理)

 

以浙江产的 个推为例 官网就是:http://docs.getui.com/

有安卓的 ios的 还有java的 都是写好的demo和定义好的接口,方法 非常方便 拿来即用。

若前端为mui集成框架开发的话,ios的会比较麻烦一点 因为ios有自己的消息推送通道即 apns 消息通道。这里不详细说明了。

若是原生的 基本上没啥子 大问题。

其中有java部分 以此为例:

有限前端app框架为mui框架:

代码实现也笔记简单,就是接收下后台来的 透传消息做下处理即可(其中透传的消息可以是带参数的 也可以不带参数直接传个路径);

pushParameter 是参数集合 注意此段代码应放在search 主页面下。若放在index,下会不稳定 极不稳定。能成功接收到透传消息参数的概率极低。

网上也查了下,使用其他个推 如百度推送 等 前端代码都大体差不多。

document.addEventListener( "plusready", function(){
     message = document.getElementById("message");
     // 监听在线消息事件
     plus.push.addEventListener( "receive", function( msg ) {
        var info = plus.push.getClientInfo();
        var jsonStr=msg.content;
//      if ( msg.aps ) {  // Apple APNS message
//          alert("接收到在线APNS消息:111" );
//         outSet( "接收到在线APNS消息:" );
//      } else {
//       //alert("接收到在线透传消息:"  );
//          var jsonStr=msg.content;
//             var content=JSON.parse(jsonStr)
//         //alert(content.type); alert(content.clientid);
//           outSet( "接收到在线透传消息:" );
//      }
        var pushinfo=JSON.parse(jsonStr)
        var viewId = pushinfo.ViewId;
        var viewUrl = pushinfo.viewUrl;
        var pushParameter = pushinfo.pushParameter;

        //alert(pushParameter);
        if(pushinfo.type == "1"){
            if(viewUrl != null && viewUrl != ""){
                mui.openWindow({
                    id: viewId,
                    url: viewUrl,
                    show: {
                        aniShow: "pop-in"
                    },
                    waiting: {
                        autoShow: true  
                    },
                    extras: pushParameter
                });
            }
        }
    }, false );
    // 监听点击消息事件
    plus.push.addEventListener( "click", function( msg ) {
        // 判断是从本地创建还是离线推送的消息
        switch( msg.payload ) {
            case "LocalMSG":
                outSet( "点击本地创建消息启动:" );
            break;
            default:
                outSet( "点击离线推送消息启动:");
            break;
        }
        // 提示点击的内容
        plus.ui.alert( msg.content );
        // 处理其它数据
        logoutPushMsg( msg );
    }, false );
}, false );
          

至于后台就更为简单了:

创建消息对象 调用接口 发送 ok了 例:

PushInfo pushInfo = new PushInfo();
            PushParameter pushParameter = new PushParameter();
            pushParameter.setCarId(searchCarNormalId);
            pushParameter.setCarBrandName(searchCarNormal.getCarBrandName());
            pushParameter.setCarModelName(searchCarNormal.getModelName());
            pushParameter.setCarStyleName(searchCarNormal.getStyleName());
            pushInfo.setType(PushInfo.TYPE.DETAILPAGE);//选择是否 跳转页面
            pushInfo.setViewUrl(PushInfo.VIEWURL.findNormaDetail);//页面跳转路径
            pushInfo.setTitle(PushInfo.TITLE.normaSystem);//消息标题
            pushInfo.setBody(content);//消息内容
            pushInfo.setPushParameter(pushParameter);//透传消息参数 实体对象
            pushInfo.setImge(PushInfo.IMGE.DEFAULT);//个推常规设置-显示的标题图片名
            pushInfo.setUrl(PushInfo.URL.NULL);//个推常规设置-显示的标题图片路径
            pushInfo.setClientid(buyUser.getClientid());//从 后台获取到的CID
//            pushInfo.setGoUrl("http://www.baidu.com");
            MessageSending.messageSending(mobile, content, pushInfo);//调用发送个推和短信 工具类中的方法


单启线程进行消息 发送 因为个推会用到 别人的接口 和服务 有时候会比较慢所以:

public static void messageSending(String mobile, String content,
            PushInfo pushInfo) {
        new Thread(IntermessageSending(mobile,content,pushInfo)).start();
    }
    private static Runnable IntermessageSending(String mobile, String content,
            PushInfo pushInfo) {
        SendSms.SMS(mobile,MobileShort.CHECKTYPE.NOTICE, content);
        PushMessageUtil.linkTemplate(pushInfo);
        return null;
        
    }

具体实现,这里用的都是别人的接口及方法 官网上都有api 文档说的很详细:

public static NotificationTemplate linkTemplate(PushInfo pushInfo) {
        IGtPush push = new IGtPush(host, appKey, masterSecret);
      SingleMessage message = new SingleMessage();
      NotificationTemplate template = new NotificationTemplate();
      // 设置APPID与APPKEY
      template.setAppId(appId);
      template.setAppkey(appKey);
      
      //ios apns通道消息栏通知设置
      APNPayload aPNPayload=new APNPayload(); 
      //aPNPayload.setAutoBadge("+1");//设置角标数值
      aPNPayload.setContentAvailable(1);
      aPNPayload.setSound("default");
      aPNPayload.setAlertMsg(getDictionaryAlertMsg(pushInfo));
      aPNPayload.setContentAvailable(0);
      template.setAPNInfo(aPNPayload);
  
    //透传消息设置,1,为强制启动应用, 2:等待应用启动
      template.setTransmissionType(1);
      template.setTransmissionContent(JSONObject.toJSONString(pushInfo));
      Style0 style=new Style0();
      
      // 安卓设置通知栏标题与内容
      style.setTitle(pushInfo.getTitle());
      style.setText(pushInfo.getBody());
      // 配置通知栏图标
      style.setLogo(pushInfo.getImge());
      // 配置通知栏网络图标,填写图标URL地址
      style.setLogoUrl(pushInfo.getUrl());
      // 设置通知是否响铃,震动,或者可清除
      style.setRing(true);
      style.setVibrate(true);
      style.setClearable(true);
      template.setStyle(style);
      //设置app离线消息发送缓存
      message.setOffline(true);
      // 离线有效时间,单位为毫秒,可选
      message.setOfflineExpireTime(24 * 3600 * 1000);
      message.setData(template);
      // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
      message.setPushNetWorkType(0); 
      extracted(push, message,pushInfo.getClientid());
      return template;
  }

 

转载于:https://www.cnblogs.com/xieCong/p/6888495.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值