zoukankan      html  css  js  c++  java
  • app推送-极光推送

    会不会有小伙伴一脸懵逼,问我什么是App推送?

    先甩一个熟悉的界面。这些就是推送。

    file

    这些推送有什么作用呢,我百度百科了一下好处

    做过app开发的都应该接触到app推送。现在安卓推送 app只要是后台进程被杀死用户是是接受不到的。ios推送是没有问题的,因为ios只有一个厂家使用的系统 而安卓虽然也是只是一个系统但是但是被很多厂商修改了底层。这个时候如果你的老板给你一个需求让你做app推送,保证app推送用户百分百接收到,那么你就可以辞职了。你和安卓一块辞职。是不是想想都可怕。原来我们公司是这么做的,当app被锁屏的时候留一个一像素的点,保证app随时存活,但是现在不可以了 只要清理了,app就会被杀死。
    

    file

    这个时候小伙伴就有人问我了,这个就没办法解决了么???????????
    

    file

    现在可以集成各大厂商都自己研发的推送方式,比如小米推送、华为推送。他们自己推送肯定在自己的手机能收到,还有就是只要你有钱什么都能解决。。。。。

    file

      哈哈哈哈 对的 你只要给各大厂商钱就可以开启白名单通道,如果你是安卓你就可以看到,即使你把某些app进程给杀死了但是呢,依然还有烦人的推送,据听说腾讯是超牛逼的,这话怎么讲,腾讯微信是肯定在白名单的不用想。但是呢,人家有自己开发的推送方式,只要腾讯有一个app是活着的,他就能把所有的腾讯产品拉起来,(这个时候小伙伴会问我什么是拉起来,我解释一下拉起来的含义就是能让某些app自己启动)是不是不可思议哈哈哈哈。今天我们要说的是极光推送,只要全部app都是极光推送,你们的app就是活着的,But极光是靠这个收费的,具体怎么收费我给你们甩一个官网。 
    

    点我进极光官网--小程序用户直接百度极光推送

    给大家说一个好消息 工信部已经联合了国内多家厂商要做统一推送服务,如果那一天真的到来那么对于广大国内开发者将会受益巨大。官网网址:http://chinaupa.com/ 
    

    file

      我给大家的解决方案可以参考一下:
      目前解决推送问题通用的解决办法是采用混合推送机制,对于有提供系统推送的厂商走厂商推送,其余的走第三方                       推送。比如极光
    系统级别的推送就是根据手机型号进行推送,判断用户手机型号进行分别进行sdk的切换。
    对于没有提供系统级推送的厂商那么只能自己通过保活机制维持一个常驻后台进程来实现。不过厂商为了系统纯净对后台进程做了严格限制,导致目前的进程保活机制面临几乎无解的问题。
    最后我们一起期待工信部的推送尽快与大家见面。。
    

    今天说一下app极光推送后端逻辑

    老规矩先扔极光推送文档的地址。 点我快速进入极光推送文档地址 极光不仅能推送还能看到app多少日活,有的投资人不看你们app后台的数据,就看极光友盟之类的第三方插件的数据 file

    我们从demo看起,这是手动推送的界面。很简洁。 file 但是呢,我们身为高级程序员怎么可能用这种方法。 file

    好嘞这个没有什么流程就给请求极光就可以了。甩一个工具类。

    pom文件 如果是maven项目直接引入 如果是web项目需要从官网下载jar包
    
      <!-- 极光推送 -->
    	  <dependency>
    		  <groupId>cn.jpush.api</groupId>
    		  <artifactId>jpush-client</artifactId>
    		  <version>3.3.7</version>
    	  </dependency>
    	 
    
    package cn.cnbuilder.util;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import cn.jiguang.common.resp.APIConnectionException;
    import cn.jiguang.common.resp.APIRequestException;
    import cn.jpush.api.JPushClient;
    import cn.jpush.api.push.PushResult;
    import cn.jpush.api.push.model.Message;
    import cn.jpush.api.push.model.Options;
    import cn.jpush.api.push.model.Platform;
    import cn.jpush.api.push.model.PushPayload;
    import cn.jpush.api.push.model.audience.Audience;
    import cn.jpush.api.push.model.notification.AndroidNotification;
    import cn.jpush.api.push.model.notification.IosAlert;
    import cn.jpush.api.push.model.notification.IosNotification;
    import cn.jpush.api.push.model.notification.Notification;
    
    /** 
     * 说明:极光推送工具类
     * @version
     * KingYiFan
     */
    @Controller
    @RequestMapping(value="/jpush")
    public class JPushClientUtil{
    	//极光后台管理系统申请
    	private static final String appKey = "xxxxx";
    	private static final String masterSecret = "xxxxx";
    	public static final int MAX = Integer.MAX_VALUE;
    	public static final int MIN = MAX / 2;
    	 private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
    	
    	/**
    	 * 保存离线的时长。秒为单位。最多支持10天(864000秒)。 
    	 * 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。
    	 * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。
    	 */
    	private static long timeToLive = 60 * 60 * 24;
    	public static void main(String[] args) {
    		String msgTitle = "测试推送,你是KingYiFan,哈哈哈!!!";
    		String msgContent = "看到信息了么,看到就推送成功了!";
    		String userid="用户别名";
    		JPushClientUtil.sendToAll("最新文章",msgTitle, "WZ", userid);
    	}
    	/**
    	 * 保持 sendNo 的唯一性是有必要的
    	 */
    	public static int getRandomSendNo() {
    		return (int) (MIN + Math.random() * (MAX - MIN));
    	}
        /**
         * 推送给设备标识参数的用户
         * @param registrationId 设备标识
         * @param notification_title 通知内容标题
         * @param msg_title 消息内容标题
         * @param msg_content 消息内容
         * @param extrasparam 扩展字段
         * @return 0推送失败,1推送成功
         */
        public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam,String extrasparam2) {
            int result = 0;
            try {
                PushPayload pushPayload= JPushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam,extrasparam2);
                System.out.println(pushPayload);
                PushResult pushResult=jPushClient.sendPush(pushPayload);
                System.out.println(pushResult);
                if(pushResult.getResponseCode()==200){
                    result=1;
                }
            } catch (APIConnectionException e) {
                e.printStackTrace();
     
            } catch (APIRequestException e) {
                e.printStackTrace();
            }
     
             return result;
        }
     
        /**
         * 发送给所有安卓用户
         * @param notification_title 通知内容标题
         * @param msg_title 消息内容标题
         * @param msg_content 消息内容
         * @param extrasparam 扩展字段
         * @return 0推送失败,1推送成功
         */
        public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
            int result = 0;
            try {
                PushPayload pushPayload= JPushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
                System.out.println(pushPayload);
                PushResult pushResult=jPushClient.sendPush(pushPayload);
                System.out.println(pushResult);
                if(pushResult.getResponseCode()==200){
                    result=1;
                }
            } catch (Exception e) {
     
                e.printStackTrace();
            }
     
             return result;
        }
     
        /**
         * 发送给所有IOS用户
         * @param notification_title 通知内容标题
         * @param msg_title 消息内容标题
         * @param msg_content 消息内容
         * @param extrasparam 扩展字段
         * @return 0推送失败,1推送成功
         */
        public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
            int result = 0;
            try {
                PushPayload pushPayload= JPushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
                System.out.println(pushPayload);
                PushResult pushResult=jPushClient.sendPush(pushPayload);
                System.out.println(pushResult);
                if(pushResult.getResponseCode()==200){
                    result=1;
                }
            } catch (Exception e) {
     
                e.printStackTrace();
            }
     
             return result;
        }
     
        /**
         * 发送给所有用户
         * @param notification_title 通知内容标题
         * @param msg_title 消息内容标题
         * @param msg_content 消息内容
         * @param extrasparam 扩展字段
         * @return 0推送失败,1推送成功
         */
        public static int sendToAll( String title, String content,String extrasparam, String extrasparam2) {
            int result = 0;
            try {
                PushPayload pushPayload= JPushClientUtil.buildPushObject_android_and_ios(title,content,extrasparam,extrasparam2);
                System.out.println(pushPayload);
                PushResult pushResult=jPushClient.sendPush(pushPayload);
                System.out.println(pushResult);
                if(pushResult.getResponseCode()==200){
                    result=1;
                }
            } catch (Exception e) {
     
                e.printStackTrace();
            }
     
            return result;
        }
     
     
     
        public static PushPayload buildPushObject_android_and_ios(String title, String content, String extrasparam, String extrasparam2) {
        	    IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody(title, null,content).build();
        		return PushPayload.newBuilder()
                    .setPlatform(Platform.android_ios())
                    .setAudience(Audience.all())
                    .setNotification(Notification.newBuilder()
                            .setAlert(title)
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setAlert(content)
                                    .setTitle(title)
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("type",extrasparam)
                                    .addExtra("id",extrasparam2)
                                    .build()
                            )
                            .addPlatformNotification(IosNotification.newBuilder()
                                    //传一个IosAlert对象,指定apns title、title、subtitle等
                                    .setAlert(iosAlert)
                                    //直接传alert
                                    //此项是指定此推送的badge自动加1
                                    .incrBadge(1)
                                    //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                    // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                    .setSound("sound.caf")
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("type",extrasparam)
                                    .addExtra("id",extrasparam2)
                                    //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                    // .setContentAvailable(true)
     
                                    .build()
                            )
                            .build()
                    )
                    //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                    // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                    // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                    .setMessage(Message.newBuilder()
                            .setMsgContent(content)
                            .setTitle(title)
                            .addExtra("type",extrasparam)
                            .addExtra("id",extrasparam2)
                            .build())
     
                    .setOptions(Options.newBuilder()
                            //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                            .setApnsProduction(true)
                            //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                            .setSendno(getRandomSendNo())
                            //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                            .setTimeToLive(timeToLive)
                            .build()
                    )
                    .build();
        }
     
        private static PushPayload buildPushObject_all_registrationId_alertWithTitle(String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam,String extrasparam2) {
     
            System.out.println("----------buildPushObject_all_all_alert");
            //创建一个IosAlert对象,可指定APNs的alert、title等字段
            //IosAlert iosAlert =  IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
            return PushPayload.newBuilder()
                    //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                    .setPlatform(Platform.all())
                    //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                    .setAudience(Audience.alias(registrationId))
                    //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                    .setNotification(Notification.newBuilder()
                            //指定当前推送的android通知
                            .addPlatformNotification(AndroidNotification.newBuilder()
     
                                    .setAlert(notification_title)
                                    .setTitle(notification_title)
                                    
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("type",extrasparam)
                                    .addExtra("id",extrasparam2)
                                    .build())
                            //指定当前推送的iOS通知
                            .addPlatformNotification(IosNotification.newBuilder()
                                    //传一个IosAlert对象,指定apns title、title、subtitle等
                                    .setAlert(msg_title)
                                    //直接传alert
                                    //此项是指定此推送的badge自动加1
                                    .incrBadge(1)
                                    //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                    // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                    .setSound("default")
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("type",extrasparam)
                                    .addExtra("id",extrasparam2)
                                    //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                    //取消此注释,消息推送时ios将无法在锁屏情况接收
                                    // .setContentAvailable(true)
     
                                    .build())
     
     
                            .build())
                    //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                    // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                    // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                    .setMessage(Message.newBuilder()
     
                            .setMsgContent(msg_content)
     
                            .setTitle(msg_title)
     
                            .addExtra("type",extrasparam)
                             .addExtra("id",extrasparam2)
     
                            .build())
     
                    .setOptions(Options.newBuilder()
                            //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                            .setApnsProduction(true)
                            //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                            .setSendno(getRandomSendNo())
                            //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
                            .setTimeToLive(timeToLive)
     
                            .build())
     
                    .build();
    	    }
    	 
    	    private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
    	        System.out.println("----------buildPushObject_android_registrationId_alertWithTitle");
    	        return PushPayload.newBuilder()
                    //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                    .setPlatform(Platform.android())
                    //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                    .setAudience(Audience.all())
                    //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                    .setNotification(Notification.newBuilder()
                            //指定当前推送的android通知
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setAlert(notification_title)
                                    .setTitle(notification_title)
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("androidNotification extras key",extrasparam)
                                    .build())
                            .build()
                    )
                    //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                    // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                    // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                    .setMessage(Message.newBuilder()
                            .setMsgContent(msg_content)
                            .setTitle(msg_title)
                            .addExtra("message extras key",extrasparam)
                            .build())
     
                    .setOptions(Options.newBuilder()
                            //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                            .setApnsProduction(true)
                            //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                            .setSendno(getRandomSendNo())
                            //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                            .setTimeToLive(timeToLive)
                            .build())
                    .build();
    	    }
    	 
    	    private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
    	        System.out.println("----------buildPushObject_ios_registrationId_alertWithTitle");
    	        return PushPayload.newBuilder()
                    //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
                    .setPlatform(Platform.ios())
                    //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
                    .setAudience(Audience.all())
                    //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
                    .setNotification(Notification.newBuilder()
                            //指定当前推送的android通知
                            .addPlatformNotification(IosNotification.newBuilder()
                                    //传一个IosAlert对象,指定apns title、title、subtitle等
                                    .setAlert(notification_title)
                                    //直接传alert
                                    //此项是指定此推送的badge自动加1
                                    .incrBadge(1)
                                    //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                    // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                    .setSound("sound.caf")
                                    //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                    .addExtra("iosNotification extras key",extrasparam)
                                    //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                   // .setContentAvailable(true)
     
                                    .build())
                            .build()
                    )
                    //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                    // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                    // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                    .setMessage(Message.newBuilder()
                            .setMsgContent(msg_content)
                            .setTitle(msg_title)
                            .addExtra("message extras key",extrasparam)
                            .build())
     
                    .setOptions(Options.newBuilder()
                            //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                            .setApnsProduction(true)
                            //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                            .setSendno(getRandomSendNo())
                            //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                            .setTimeToLive(timeToLive)
                            .build())
                    .build();
    	    }
    }
    
    

    这就是极光推送教程,哪里不懂可以私信我哦!给大家留一个问题,推送你们是否有收到过带声音的推送。我最近在研究,欢迎一起学习。


    鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。 微信:

    支付宝:


    感谢一路支持我的人。。。。。
    
    Love me and hold me
    QQ:69673804(16年老号)
    EMAIL:69673804@qq.com
    友链交换
    如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。
    

    网站名称:KingYiFan’S Blog
    网站地址:http://blog.cnbuilder.cn
    网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
    网站Logo/头像:[头像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)
    
  • 相关阅读:
    《浪潮之巅阅读笔记》4
    第十三周学习进度总结
    Android 自学之选项卡TabHost
    Android 自学之星级评分条RatingBar
    Android 自学之拖动条SeekBar
    Android 自学之进度条ProgressBar
    Android 自学之日期DatePicker、时间TimePicker选择器
    Android 自学之列表选择框Spinner
    Android 自学之自动完成文本框 AutoCompleteTextView
    Android 自学之基本界面组件(下)
  • 原文地址:https://www.cnblogs.com/kingyifan/p/11721451.html
Copyright © 2011-2022 走看看