Hbuiler,HbuilderX ios 使用个推配置

news/2024/10/17 16:21:26/

1、能够完成推送的条件

     ①、ios应用必须有开发证书,或者发布证书,能够打包成ipa包(ios打包及证书申请查看:https://blog.csdn.net/u011118071/article/details/104937328),运行在真实苹果手机上,模拟器不行。

     ②、需要在苹果开发者网址,注册应用APPID时勾选推送服务,并创建推送证书(非常重要),需要上传到个推应用配置中心

     ③、ios 端需要使用H5+,监听推送,并做后续处理。

     ④、ios只能推送透传消息(重要)

2、操作配置步骤

    ①、在个推平台注册账户,并创建应用

           个推官网网址:https://www.getui.com/?f=3&p=1

            进入后,点击应用管理

      点击创建应用

如上图,①、自己根据实际情况填写即可,②部分,android包名,对应安卓应用打包时包名。android签名,使用Hbuilder提供通用签名59:20:1C:F6:58:92:02:CB:2C:DA:B2:67:52:47:21:12即可。③、IOS bundleID即在苹果开发者中心创建AppID时创建的bundleID。

在ios推送时,还需要在苹果开发者中心创建推送证书,步骤如下

①、登录开发者网址:https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=891bd3417a7776362562d2197f89480a8547b108fd934911bcbea0110d07f757&path=%2Fdownload%2F&rv=1

②、选择之前创建的App ID

③、选择Push Notificatios ,点击Configure

④、选择创建证书,Create Certificate

 

选择证书申请文件。

     证书申请文件步骤如下:

     

生成证书请求文件

不管是申请开发 (Development) 证书还是发布 (Distribution) 证书,都需要使用证书请求 (.certSigningRequest) 文件,证书请求文件需在Mac OS上使用 “钥匙串访问” 工具生成。

在“Spltlight Search”中搜索“钥匙串”并打开 “钥匙串访问” 工具:

 


打开菜单 “钥匙串访问”->“证书助理”,选择“从证书颁发机构请求证书...”:


打开创建请求证书页面,在页面中输入用户邮件地址、常用名称,选择存储到磁盘,点击 “继续” :


文件名称为“CertificateSigningRequest.certSigningRequest”,选择保存位置,点击 “存储” 将证书请求文件保存到指定路径下,后面申请开发(Development)证书和发布(Production)证书时需要用到

到此,推送证书已配置完成。点击下载即可。

我申请的证书文件如下

进入个推平台,选择个推-消息推送>选择应用【ios供热助手】>创建推送

选择应用配置,在红框位置上传刚创建的证书,下图为我上传成功后的效果,可以修改证书。也可以测试。

这儿要说一下测试一下的作用。将打包好的ios开发包安装到苹果手机号,启动应用,

可以通过plus.push.getClientInfo()获取clientId及token,点击测试,将获取的token输入到文本框,点击测试。若设备能收到推送消息,说明设备与个推平台已建立联系。【应用打包时需要在manifest.json配置个推sdk,设置个推appid,appkey,appsecret】

测试成功后,就可以在个推平台推送消息中发消息测试了。注意IOS只能接收透传消息。

下面贴一下我应用的前端js代码

//监听推送点击事件
warmApp.push.addEventListenerClick=function(handle)
{
       plus.push.addEventListener("click", function (msg) {
       handle(msg);
       //消息类型
      // msg.payload LocalMSG:本地创建消息 否则为离线消息
}, false);
}

// 监听在线消息事件
warmApp.push.addEventListenerReceive=function(handle)
{

plus.push.addEventListener("receive", function (msg) {

handle(msg);
//msg.aps  Apple APNS message:在线APNS消息 否则 在线透传消息
});
}

为了使用方便我做了简单封装,其中handle为回调函数。

java端集成推送代码

 

PushMessageUtil类
package com.sheshu.common.pushmessage;import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.ITemplate;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
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.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.NotyPopLoadTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;/*** Created by Administrator on 2017/6/22.*/
@Component
public class PushMessageUtil {@Autowiredpublic   PushMessagePojo pushMessagePojo;public PushMessageUtil(PushMessagePojo pushMessagePojo){this.pushMessagePojo=pushMessagePojo;}/*** 指定单个用户推送消息* @return*/public  String pushToSingle(TempData tempData){IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());SingleMessage message = new SingleMessage();message.setData(getTemplate(tempData));message.setOffline(true);message.setOfflineExpireTime(1800 * 1000);Target target = new Target();target.setAppId(pushMessagePojo.getAppId());target.setClientId(tempData.getCid());IPushResult result = push.pushMessageToSingle(message, target);return   result.getResponse().get("result").toString();}/*** 群推消息* @return*/public   String pushGroup(TempData tempData){IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());List<String> appIds = new ArrayList<String>();appIds.add(pushMessagePojo.getAppId());AppMessage message = new AppMessage();message.setData(getTemplate(tempData));message.setOffline(true);message.setOfflineExpireTime(1800 * 1000);message.setAppIdList(appIds);IPushResult result = push.pushMessageToApp(message);return   result.getResponse().get("result").toString();}/*** 指定用户列表cid推送消息* @return*/public   String pushToSingleList(TempData tempData){IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());ListMessage  message = new ListMessage();message.setData(getTemplate(tempData));message.setOffline(true);message.setOfflineExpireTime(1800 * 1000);List<Target> targetList=new ArrayList<Target>();for(String cid:tempData.getCidList()){Target target = new Target();target.setAppId(pushMessagePojo.getAppId());target.setClientId(cid);targetList.add(target);}String taskId = push.getContentId(message);IPushResult result = push.pushMessageToList(taskId, targetList);return   result.getResponse().get("result").toString();}//消息模板设置函数public    ITemplate getTemplate(TempData tempData){ITemplate iTemplate=null;switch (tempData.getTemplateType()){case NotificationTemplate:NotificationTemplate notificationTemplate=new NotificationTemplate();notificationTemplate.setAppId(pushMessagePojo.getAppId());notificationTemplate.setAppkey(pushMessagePojo.getAppKey());notificationTemplate.setTitle(tempData.getTempTitle());notificationTemplate.setText(tempData.getTempText());iTemplate=notificationTemplate;break;case LinkTemplate:LinkTemplate linkTemplate = new LinkTemplate();linkTemplate.setAppId(pushMessagePojo.getAppId());linkTemplate.setAppkey(pushMessagePojo.getAppKey());linkTemplate.setTitle(tempData.getTempTitle());linkTemplate.setText(tempData.getTempText());linkTemplate.setUrl(tempData.getTempUrl());iTemplate=linkTemplate;break;case NotyPopLoadTemplate:NotyPopLoadTemplate notyPopLoadTemplate=new NotyPopLoadTemplate();//获取appIdnotyPopLoadTemplate.setAppId(pushMessagePojo.getAppId());notyPopLoadTemplate.setAppkey(pushMessagePojo.getAppKey());//设置通知栏信息notyPopLoadTemplate.setNotyTitle(tempData.getTempTitle());notyPopLoadTemplate.setNotyContent(tempData.getTempText());//配置通知栏图标notyPopLoadTemplate.setNotyIcon("");//设置是否响铃,震动,或者清除notyPopLoadTemplate.setBelled(true);notyPopLoadTemplate.setVibrationed(true);notyPopLoadTemplate.setCleared(true);//设置弹出标题与内容notyPopLoadTemplate.setPopTitle(tempData.getPopTitle());notyPopLoadTemplate.setPopContent(tempData.getPopContent());//设置弹出框显示图片notyPopLoadTemplate.setPopImage("");notyPopLoadTemplate.setPopButton1(tempData.getPopButton1());notyPopLoadTemplate.setPopButton2(tempData.getPopButton2());//设置下载标题notyPopLoadTemplate.setLoadTitle(tempData.getLoadTitle());notyPopLoadTemplate.setLoadIcon("");//设置下载地址notyPopLoadTemplate.setLoadUrl(tempData.getLoadUrl());notyPopLoadTemplate.setActived(true);iTemplate =notyPopLoadTemplate;break;case TransmissionTemplate:TransmissionTemplate transmissionTemplate=new TransmissionTemplate();transmissionTemplate.setAppId(pushMessagePojo.getAppId());transmissionTemplate.setAppkey(pushMessagePojo.getAppKey());//1强制启动应用程序,2等待程序启动transmissionTemplate.setTransmissionType(1);transmissionTemplate.setTransmissionContent(tempData.getTransmissionContent());// 设置定时展示时间// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");// iOS推送使用该字段APNPayload payload = new APNPayload();payload.setAutoBadge("0");// 设置角标,还可以实现显示数字的自动增减,如"+1"、"-1"、"1"等// 推送直接带有透传数据// 当content-available为1时,App存活(前台或后台时),消息内容会直接透传给App。在未设置其他提醒(Alert、Sound)时,可以无提示和无声音的刷新数据。// (支持 iOS 7.0 及以上版本。)payload.setContentAvailable(0);payload.setSound("default"); // 通知铃声文件名,无声设置为"com.gexin.ios.silence"payload.setAlertMsg(getDictionaryAlertMsg(tempData.getTempTitle(), tempData.getTempText())); // 字典模式使用APNPayload.DictionaryAlertMsgpayload.setCategory(tempData.getTransmissionContent());transmissionTemplate.setAPNInfo(payload);iTemplate=transmissionTemplate;break;}return  iTemplate;}/*** ios数据封装 APNPayload节点** @param title 通知标题* @param text  通知内容 :不能有转义字符,大小限制2kb* @return*/private  APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String text ) {APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();alertMsg.setBody(text);// 通知文本消息字符串alertMsg.setActionLocKey("ActionLockey");// (用于多语言支持)指定执行按钮所使用的Localizable.stringsalertMsg.setLocKey("loc-key");// (用于多语言支持)指定Localizable.strings文件中相应的keyalertMsg.addLocArg("loc-args");// 如果loc-key中使用的占位符,则在loc-args中指定各参数alertMsg.setLaunchImage("launch-image");// 指定启动界面图片名// iOS8.2以上版本支持alertMsg.setTitle(title);// 通知标题alertMsg.setTitleLocKey("TitleLocKey");// (用于多语言支持)对于标题指定执行按钮所使用的Localizable.strings,仅支持iOS8.2以上版本alertMsg.addTitleLocArg("TitleLocArg");// 标题,如果loc-key中使用的占位符,则在loc-args中指定各参数,仅支持iOS8.2以上版本return alertMsg;}}

 

PushMessagePojo类
package com.sheshu.common.pushmessage;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** Created by Administrator on 2017/6/22.*/
@Data
@Component
@ConfigurationProperties(prefix = "push")
public class PushMessagePojo {private  String appId;         // = "3DFlQTretF73wQC1hviQT3";private  String appKey;         //  = "Gob1gQ1woN8XJo6LRNbgC5";private  String masterSecret;   //  = "j6BGytOgWtAMjC6SBzcKp7";private  String url;            //  = "http://sdk.open.api.igexin.com/apiex.htm";
}

 

TempData类
package com.sheshu.common.pushmessage;
import lombok.Data;import java.util.List;/*** @Auther: chenxiaodong* @Date: 2019-05-27 8:58* @Description:*/
@Data
public class TempData {//模板抬头private String  tempTitle;//模板内容private String  tempText;//模板urlprivate  String tempUrl;//模板类型private  TemplateType templateType;//客户端appIdprivate String cid;//客户端appId列表private List<String> cidList;//是否响铃private Boolean isBelled;//是否震动private Boolean isVibrationed;//是否清除private Boolean isCleared;//通知栏图标private String notyIcon;/*********推送下载部分***********///弹出层标题private  String popTitle;//弹出层内容private  String popContent;//弹出层图片private String popImage;//弹出层按钮1private String popButton1;//弹出层按钮2private String popButton2;//下载抬头private String loadTitle;//下载图标private String loadIcon;//下载地址private String loadUrl;/*********透传消息部分***********///透传消息内容private String transmissionContent;}

 

TemplateType 类
package com.sheshu.common.pushmessage;/*** Created by Administrator on 2017/6/22.*/
public enum  TemplateType  {NotificationTemplate, LinkTemplate,TransmissionTemplate,NotyPopLoadTemplate;
}
TransmissionContent 类
package com.sheshu.common.pushmessage;import lombok.Data;/*** @Auther: chenxiaodong* @Date: 2019-05-27 10:57* @Description:*/
@Data
public class TransmissionContent {/*** 通知栏标题*/private String title;/*** 通知栏内容*/private String content;/***其他数据*/private PayloadData payloadData;public TransmissionContent(){payloadData=new PayloadData();}@Datapublic class  PayloadData{//审批,工单,消息id,可根据id直接打开详情页面private String id;//消息类型(审批,工单,消息,下载),可根据type直接打开列表private String type;//url地址private String url;//是否语音播报private String isSpeech;}}

单元测试示例

package com.sheshu.common;import com.google.gson.Gson;
import com.sheshu.common.pushmessage.*;
import org.junit.Test;import java.util.ArrayList;
import java.util.List;/*** @Auther: chenxiaodong* @Date: 2020-03-18 17:49* @Description:*/
public class PushTest {@Testpublic void pushTest(){PushMessagePojo pushMessagePojo=new PushMessagePojo();pushMessagePojo.setAppId("G6OneF4tIZ9zb5A9MdGJ99");pushMessagePojo.setAppKey("JZhbDdK6Sh9B9WrmyLbcA8");pushMessagePojo.setMasterSecret("y1mfBXfo7U9BPSgyRXjDJ");pushMessagePojo.setUrl("http://sdk.open.api.igexin.com/apiex.htm");PushMessageUtil pushMessageUtil=new PushMessageUtil(pushMessagePojo);List<String> cidList =new ArrayList<>();cidList.add("fb08f5d94c9c938f02452cbcc61c099f");cidList.add("0048224eac1bcf5697a8a8ca54f0df8d");//手机端推送TempData tempData = new TempData();//设置模板为透传模板tempData.setTemplateType(TemplateType.TransmissionTemplate);//设置cid集合tempData.setCidList(cidList);tempData.setTempTitle("您有一条审批记录,请注意查收!");tempData.setTempText("审批");TransmissionContent transmissionContent = new TransmissionContent();//设置推送标题transmissionContent.setTitle("审批");//设置推送内容transmissionContent.setContent("您有一条审批记录,请注意查收!");//设置语音播报transmissionContent.getPayloadData().setIsSpeech("true");//设置推送类型transmissionContent.getPayloadData().setType("审批");//设置id为空transmissionContent.getPayloadData().setId("1");Gson gson = new Gson();//将对象转换为jsonString json = gson.toJson(transmissionContent);//设置透传消息内容tempData.setTransmissionContent(json);//群发String result = pushMessageUtil.pushToSingleList(tempData);System.out.println(result);}
}

我代码全部贴出。以便于测试。

目前我ios及android均使用透传推送,所以主要关注如下代码

完成


http://www.ppmy.cn/news/790944.html

相关文章

Mac 配置Nginx域名转发

配置环境 MacOS Monterey 12.0.1提前运行端口88的Web项目 1.修改hosts文件 打开host文件 sudo vi /etc/hosts XaysdeMacBook-Pro:~ xay$ sudo vi /etc/hosts 添加域名映射 ## # Host Database # # localhost is used to configure the loopback interface # when the sy…

STM32的ADC模式及其应用例程介绍

STM32的ADC模式及其应用例程介绍 &#x1f4cd;ST官方相关应用笔记介绍资料&#xff1a;https://www.stmcu.com.cn/Designresource/detail/application_note/705947&#x1f4cc;相关例程资源包&#xff1a;STSW-STM32028&#xff1a;https://www.st.com/zh/embedded-software/…

一文搞懂常见的加密算法

加密算法在互联网技术领域中几乎是无处不在&#xff0c;而密码学也是网络安全的重要基础&#xff0c;这篇文章我们就一起来学习下常见的加密算法。 1 为什么要研究加密算法&#xff1f; 在技术方面&#xff0c;加密算法的研究具有重要的意义&#xff0c;主要体现在以下几个方…

excel计算数据时固定某一列或一行或某一值

只需要把要固定的对象采用绝对引用&#xff1a;加 $

SpringBoot 导出Excel 固定表头

//冻结首行sheet.createFreezePane(8, 1);//固定第1行8个列

pandas写入excel指定行_python入门--使用Pandas读取与写入Excel内容

python是最近一门很火的编程语言&#xff0c;简单易上手&#xff0c;无论从事HR、财务、软体开发、网络工程师都有很大帮助&#xff0c;我们可以通过编程语言让繁琐的工作变得轻松简单&#xff0c;今日我向大家分享一个python读取/写入excel表内容的方法(这里我跳过编辑器的介绍…

Excel·VBA按行拆分工作表

对应之前文章《ExcelVBA按列拆分工作表》&#xff0c;对Excel表格数据按照固定行数&#xff0c;将工作表拆分 工作表按行拆分为工作表 Sub 工作表按行拆分为工作表()当前工作表&#xff08;worksheet&#xff09;按固定行数拆分为多个工作表&#xff0c;保存在当前工作簿&…

Excel怎么在数据中间隔插入指定行

今天跟大家分享一下Excel怎么在数据中间隔插入指定行 1.如下图我们想要在表格每行数据之间插入表头标题。 2.首先我们选中表格区域 3.点击下图选项&#xff08;Excel工具箱&#xff0c;百度即可了解详细下载安装信息&#xff0c;本文这里就不做详细解说。&#xff09; 4.选择【…