首页 > 编程知识 正文

app的消息推送怎么实现,app推送通知

时间:2023-05-04 21:05:27 阅读:274814 作者:1345

首先到个推官网注册一个账号,然后点击“个推消息推送",把需要推送的app进行登记



注意:登记时的应用标识就是appid.

应用登记成功之后会得到APPID,APPSecret,APPkey,MasterSecret



接着就需要配置sdk了,把得到的数值填入相对应的名字里面就可以了


这些做完之后然后就是代码了



     static String host = "http://sdk.open.api.igexin.com/apiex.htm";
   
    public void PushAppMember(String cid,String appId,String appKey,String masterSecret,String title,String content){
        IGtPush push = new IGtPush(host, appKey, masterSecret);
//        LinkTemplate template = linkTemplateDemo(appId,appKey,title,content);
        try {         
            //单推消息类型   
            SingleMessage message = new SingleMessage();  
                                 
            NotificationTemplate template=notificationTemplateDemo(appId,appKey,title,content);
            message.setData(template);  
            message.setOffline(true);                   //用户当前不在线时,是否离线存储,可选  
            message.setOfflineExpireTime(72 * 3600 * 1000); //离线有效时间,单位为毫秒,可选  
            message.setPushNetWorkType(0);
            
            Target target1 = new Target();  
            target1.setAppId(appId);  
            target1.setClientId(cid);  
 
            //单推  
            IPushResult ret = push.pushMessageToSingle(message, target1);  
                          
            System.out.println(ret.getResponse().toString());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
    }

    public static NotificationTemplate notificationTemplateDemo(String appId,String appKey,String title,String content) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 设置通知栏标题与内容
        template.setTitle(title);
        template.setText(content);
        // 配置通知栏图标
        template.setLogo("icon.png");
        // 配置通知栏网络图标
        template.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent("请输入您要透传的内容");
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
        return template;
    }

   注意:得到cid的方法与个推的api文档略有不同,在服务器上建一个关于cid(别名)的表,查出来要发送对方的cid,然后提交方法的时候当成参数传送过来。

  ok,以后就是个推消息推送 的经历。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。