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()); } }