zoukankan      html  css  js  c++  java
  • App集成极光推送开发流程[关键步骤]

    1.客户端集成SDK

    1.1初始化

    JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
    JPushInterface.init(this); // 初始化 JPush

    1.2设置别名及标签

    //设置别名(如果已设置,则无需重新设置),存储别名保存状态
    SharedPreferences mySharedPreferences = getSharedPreferences("test",Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = mySharedPreferences.edit();
    boolean hasSetTagAlias = mySharedPreferences.getBoolean("hasSetTagAlias",false);
    if(!hasSetTagAlias){
       JPushUtil.setTag(LoginActivity.this,mDepartment.substring(0,12));
       JPushUtil.setAlias(LoginActivity.this,userId.replaceAll("-","_"));
       editor.putBoolean("hasSetTagAlias", true);
    }

    2.服务端集成SDK

    2.1导入依赖jar包(Maven项目配置pom.xml文件即可)

    2.2构造通送内容,发起推送

    /**
         * 别名推送
         * @param alert        推送标题
         * @param ticketId    点击通知进入的工单id
         * @param alias        推送给指定的别名(以用户的id为别名)
         */
        public static void pushByAlias(String alert,String ticketId,String alias){
            JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
            // For push, all you need do is to build PushPayload object.
            PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                                                          .setAudience(Audience.all())
                                                          .setNotification(Notification.newBuilder()
                                                          .addPlatformNotification(AndroidNotification.newBuilder()
                                                                .setAlert(alert)
                                                                .addExtra("id", ticketId)
                                                                .build())
                                                          .build())
                                                          .setAudience(Audience.newBuilder()
                                                          .addAudienceTarget(AudienceTarget.alias(alias))
                                                          .build())
                                                          .build();
    
            try {
                PushResult result = jpushClient.sendPush(payload);
                logger.info("Got result - " + result);
    
            } catch (APIConnectionException e) {
                // Connection error, should retry later
                logger.error("Connection error, should retry later", e);
    
            } catch (APIRequestException e) {
                // Should review the error, and fix the request
                logger.error("Should review the error, and fix the request", e);
                logger.info("HTTP Status: " + e.getStatus());
                logger.info("Error Code: " + e.getErrorCode());
                logger.info("Error Message: " + e.getErrorMessage());
            }
        }
    /**
         * 标签推送
         * @param alert        推送标题
         * @param ticketId    点击通知进入的工单id
         * @param alias        推送给指定的别名(以用户的id为别名)
         */
        public static void pushByTag(String alert,String ticketId,String tag){
            JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
            // For push, all you need do is to build PushPayload object.
            PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.all())
                                                          .setAudience(Audience.all())
                                                          .setNotification(Notification.newBuilder()
                                                          .addPlatformNotification(AndroidNotification.newBuilder()
                                                                .setAlert(alert)
                                                                .addExtra("id", ticketId)
                                                                .build())
                                                          .build())
                                                          .setAudience(Audience.newBuilder()
                                                          .addAudienceTarget(AudienceTarget.tag(tag))
                                                          .build())
                                                          .build();
    
            try {
                PushResult result = jpushClient.sendPush(payload);
                logger.info("Got result - " + result);
    
            } catch (APIConnectionException e) {
                // Connection error, should retry later
                logger.error("Connection error, should retry later", e);
    
            } catch (APIRequestException e) {
                // Should review the error, and fix the request
                logger.error("Should review the error, and fix the request", e);
                logger.info("HTTP Status: " + e.getStatus());
                logger.info("Error Code: " + e.getErrorCode());
                logger.info("Error Message: " + e.getErrorMessage());
            }
        }
  • 相关阅读:
    学习了数据库中text类型的查找
    通过 .NET Framework 中的 XPath 和 XSLT API 方便地操作 XML 数据
    Windows服务器下用IIS Rewrite组件为IIS设置伪静态方法
    C# 文件压缩与解压(ZIP格式)
    用SharpZipLib来压缩和解压文件(转载)
    Community Server专题二:体系结构(转载)
    在ASP.NET中如何实现和利用URL重写
    Community Server专题一:概述Community Server(转载)
    C#委托之个人理解
    在ASP.NET中实现Url Rewriting
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/7727952.html
Copyright © 2011-2022 走看看