栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

uni app push 已透传的形式推送到android和ios

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uni app push 已透传的形式推送到android和ios

android接收情况:

        在线:可以收到通知

        离线:在app离线后,消息有效时间内,在此打开app可收到推送消息(此时消息还是从个推服务器推送)

        离线,不在消息有效期内,走第三方厂商推送

        缺点:无

ios接收情况

        在线:不能收到通知

        离线:可以走apns,苹果自己的推送服务,可以收到消息

        缺点:无

此种方法,主要解决了ios app 在前台运行,在线时 ,收不到通知的问题,所以选择了透传

package co.yixiang;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.StartActivityTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.io.IOException;
import java.util.*;

public class UniPushTest08 {
    // STEP1:获取应用基本信息
    private static String appId = "";
    private static String appKey = "";
    private static String masterSecret = "";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";

    public static void main(String[] args) throws IOException {
        String title="我是标题";
        String content="我是内容";

        Integer type=0;
        Integer contentId=262;


        Map map=new HashMap();
        map.put("type",type);
        map.put("contentId",contentId);
        map.put("title",title);
        map.put("content",content);

        JSonObject jsonObj=new JSonObject(map);
        String s = jsonObj.toString();


        IGtPush push = new IGtPush(url, appKey, masterSecret);

        

        // STEP4:选择通知模板
        

        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionType(2);// // 透传消息设置;1:立即启动APP;2:客户端收到消息后需要自行处理,如果设置为1,对用户使用不友好,不推荐使用
        template.setTransmissionContent(s);

        Notify notify = new Notify();
        notify.setTitle("厂商推送title");
        notify.setContent("厂商推送内容");
        notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNI91FE773/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+content+";S.payload="+s+";end");
        notify.setType(GtReq.NotifyInfo.Type._intent);
        template.set3rdNotifyInfo(notify);//设置第三方通知


        APNPayload apnpayload = new APNPayload();
        apnpayload.setSound("");

        //apnpayload.setalertMsg(new APNPayload.SimplealertMsg("hello"));
        apnpayload.setalertMsg(getDictionaryalertMsg((String)map.get("title"),(String)map.get("content")));

        //传送的附加信息,用于解析
        
        Set> set = map.entrySet();
        for (Map.Entry entry:set) {
            apnpayload.addCustomMsg(entry.getKey(),entry.getValue());
        }

        template.setAPNInfo(apnpayload);

        // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
        List appIds = new ArrayList();
        appIds.add(appId);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

        // STEP6:执行推送
        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
    }

    
    private static APNPayload.DictionaryalertMsg getDictionaryalertMsg(String title,String body){
        APNPayload.DictionaryalertMsg alertMsg = new APNPayload.DictionaryalertMsg();

        alertMsg.setTitle(title);
        alertMsg.setBody(body);

        alertMsg.setActionLocKey("ActionLockey");
        //显示关闭和查看两个按钮的消息;
        alertMsg.setLocKey("LocKey");
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        // iOS8.2以上版本支持
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        return alertMsg;
    }
}
//#ifdef APP-PLUS
	plus.push.addEventListener("click", function(msg) {  
		console.log("click:"+JSON.stringify(msg));  
		console.log(msg.payload);
		
		let pinf = plus.push.getClientInfo();
		console.log("pinf:"+JSON.stringify(pinf));
	}, false);
	plus.push.addEventListener("receive", function(msg) {  
		//plus.ui.alert("recevice:"+JSON.stringify(msg))  
		console.log("receive:"+JSON.stringify(msg));
		console.log(msg.payload);
		
		let pinf = plus.push.getClientInfo();
		console.log("pinf:"+JSON.stringify(pinf));
		var platform = uni.getSystemInfoSync().platform;
		if(platform == "ios"){
			//ios平台应用在前台时,不能收到通知消息,只能走透传,在创建一条本地消息
			if (msg.type == "receive"){// 这里判断触发的来源,否则一直推送。
				var content = JSON.parse(msg.content); 
				plus.push.createMessage(content.content, JSON.stringify(msg.payload), { title: content.title });
			}
		}else if (platform == 'android'){
			plus.push.createMessage(msg.content, JSON.stringify(msg.payload), { title: msg.title });
		}
	}, false); 
	// #endif


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343699.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号