7***@qq.com
7***@qq.com
  • 发布:2020-09-18 14:07
  • 更新:2023-02-17 17:37
  • 阅读:4072

如何解决APP离线推送(前后端)

分类:uni-app

1.技术选择:uni-app的UniPush(集成个推)、Java SpringBoot

2.前端代码:App.vue的onLaunch方法中加入

// #ifdef APP-PLUS  
    const _handlePushClick = function(message) {  
        // 自行处理消息点击事件  
        let {payload} = message;  
        console.log( message );  
    };  
    const _handlePushReceive = function(message) {  
        // 消息接收事件,手动创造本地通知信息  
        plus.push.createMessage(msg.content, message.payload, {  
            title: message.title  
        })  
    };  
    plus.push.addEventListener('click', _handlePushClick);    
    plus.push.addEventListener('receive', _handlePushReceive);    
    APPUpdate();  
// #endif

3.后端代码:

// 如果需要使用HTTPS,直接修改url即可  
//private static String url = "https://api.getui.com/apiex.htm";  
private static String url = "http://api.getui.com/apiex.htm";  
private static String appKey = "你的app key";  
private static String masterSecret = "你的app MasterSecret";  

    /**  
     * APP发送系统通知  
     * @param noticeForm 前端表单 包括title、content  
     * @return  
     */  
@PostMapping("/notice")  
public void notice(@RequestBody AppNoticeForm noticeForm) {  
        // 获取应用基本信息  
        IGtPush push = new IGtPush(url, appKey, masterSecret);  
        // 选择透传通知模板,个推离线只支持透传  
        TransmissionTemplate template = new TransmissionTemplate();  
        template.setAppId(appId);  
        template.setAppkey(appKey);  

        String payload = JSONUtil.toJsonStr(form);  
        template.setTransmissionContent(payload);    // 设置透传内容  
        template.setTransmissionType(2);    // 2代表客户端收到消息后需要自行处理  

        // 配置第三方厂商推送  
        Notify notify = new Notify();  
        notify.setTitle(form.getTitle());  
        notify.setContent(form.getContent());  
        notify.setType(GtReq.NotifyInfo.Type._intent);  
        // 设置intent 注意格式要正确(修改你的包名)  
        notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;" +  
                "launchFlags=0x14000000;" +  
                "component=你的包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" +  
                "S.title=" + form.getTitle() + ";" +  
                "S.content="+  form.getContent() + ";" +  
                "S.payload=" + payload + ";end");  
        notify.setPayload(payload);  
        template.set3rdNotifyInfo(notify);  

        List<String> appIds = new ArrayList<>();  
        appIds.add(appId);  
        AppMessage message = new AppMessage();  
        message.setData(template);  
        message.setAppIdList(appIds);  
        message.setOffline(true);  
        message.setOfflineExpireTime(8* 1000 * 3600);    // 离线有效时间 单位毫秒  
        message.setStrategyJson("{\"default\":1}");  
    // 推送给所有用户  
        IPushResult ret = push.pushMessageToApp(message);  
    }

4.在dcloud的开发者中心配置 厂商推送设置 即可完成离线推送
参考: UniPush使用指南

0 关注 分享

要回复文章请先登录注册

3***@qq.com

3***@qq.com

private static String url = "https://api.getui.com/apiex.htm" 这个从哪里获取呢
2023-02-17 17:37