标签:增加 boolean 3.4 override simple || title record 输出
如何离线打包请参看上篇随笔《AndroidStudio离线打包MUI》
官方文档:https://docs.jiguang.cn/jpush/client/Android/android_guide/
建议采用 jcenter 自动集成 的方式,手动集成对新手来说容易出错
使用jcenter自动集成的开发者,不需要在项目中添加jar和so,jcenter会自动完成依赖;在AndroidManifest.xml中不需要添加任何JPush SDK 相关的配置,jcenter会自动导入。
如果开发者需要修改组件属性,可以在本地的 AndroidManifest 中定义同名的组件并配置想要的属性,然后用 xmlns:tools 来控制本地组件覆盖 jcenter 上的组件。示例:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.tests.flavorlib.app" xmlns:tools="http://schemas.android.com/tools"> <application android:icon="@drawable/icon" android:name="com.example.jpushdemo.ExampleApplication" android:label="@string/app_name" > <service android:name="cn.jpush.android.service.PushService" android:process=":multiprocess" tools:node="replace" > …… </service> …… </application> …… </manifest>
确认android studio的 Project 根目录的主 gradle 中配置了jcenter支持。(新建project默认配置就支持)
buildscript { repositories { jcenter() } ...... } allprojets { repositories { jcenter() } }
在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。
android { ...... defaultConfig { applicationId "com.xxx.xxx" //JPush上注册的包名. ...... ndk { //选择要添加的对应cpu类型的.so库。 abiFilters ‘armeabi‘, ‘armeabi-v7a‘, ‘arm64-v8a‘ // 还可以添加 ‘x86‘, ‘x86_64‘, ‘mips‘, ‘mips64‘ } manifestPlaceholders = [ JPUSH_PKGNAME : applicationId, JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可. ] ...... } ...... } dependencies { ...... compile ‘cn.jiguang.sdk:jpush:3.1.1‘ // 此处以JPush 3.1.1 版本为例。 compile ‘cn.jiguang.sdk:jcore:1.1.9‘ // 此处以JCore 1.1.9 版本为例。 ...... }
import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.os.Looper; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; import cn.jpush.android.api.JPushInterface; import io.dcloud.common.adapter.util.Logger; public class JpushUtil { public static final String PREFS_NAME = "JPUSH_EXAMPLE"; public static final String PREFS_DAYS = "JPUSH_EXAMPLE_DAYS"; public static final String PREFS_START_TIME = "PREFS_START_TIME"; public static final String PREFS_END_TIME = "PREFS_END_TIME"; public static final String KEY_APP_KEY = "JPUSH_APPKEY"; public static boolean isEmpty(String s) { if (null == s) return true; if (s.length() == 0) return true; if (s.trim().length() == 0) return true; return false; } /** * 只能以 “+” 或者 数字开头;后面的内容只能包含 “-” 和 数字。 * */ private final static String MOBILE_NUMBER_CHARS = "^[+0-9][-0-9]{1,}$"; public static boolean isValidMobileNumber(String s) { if(TextUtils.isEmpty(s)) return true; Pattern p = Pattern.compile(MOBILE_NUMBER_CHARS); Matcher m = p.matcher(s); return m.matches(); } // 校验Tag Alias 只能是数字,英文字母和中文 public static boolean isValidTagAndAlias(String s) { Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$"); Matcher m = p.matcher(s); return m.matches(); } // 取得AppKey public static String getAppKey(Context context) { Bundle metaData = null; String appKey = null; try { ApplicationInfo ai = context.getPackageManager().getApplicationInfo( context.getPackageName(), PackageManager.GET_META_DATA); if (null != ai) metaData = ai.metaData; if (null != metaData) { appKey = metaData.getString(KEY_APP_KEY); if ((null == appKey) || appKey.length() != 24) { appKey = null; } } } catch (NameNotFoundException e) { } return appKey; } // 取得版本号 public static String GetVersion(Context context) { try { PackageInfo manager = context.getPackageManager().getPackageInfo( context.getPackageName(), 0); return manager.versionName; } catch (NameNotFoundException e) { return "Unknown"; } } public static void showToast(final String toast, final Context context) { new Thread(new Runnable() { @Override public void run() { Looper.prepare(); Toast.makeText(context, toast, Toast.LENGTH_SHORT).show(); Looper.loop(); } }).start(); } public static boolean isConnected(Context context) { ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conn.getActiveNetworkInfo(); return (info != null && info.isConnected()); } public static String getImei(Context context, String imei) { String ret = null; try { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); ret = telephonyManager.getDeviceId(); } catch (Exception e) { Logger.e(JpushUtil.class.getSimpleName(), e.getMessage()); } if (isReadableASCII(ret)){ return ret; } else { return imei; } } private static boolean isReadableASCII(CharSequence string){ if (TextUtils.isEmpty(string)) return false; try { Pattern p = Pattern.compile("[\\x20-\\x7E]+"); return p.matcher(string).matches(); } catch (Throwable e){ return true; } } public static String getDeviceId(Context context) { return JPushInterface.getUdid(context); } }
import android.os.Bundle; import cn.jpush.android.api.InstrumentedActivity; import cn.jpush.android.api.JPushInterface; /** * 初始化极光推送的相关信息 */ public class JPushInitActivity extends InstrumentedActivity { public static String APP_KEY = "";//在极光推送中注册的应用ID public static String MASTER_SECRET = "08123213666d973dkkik3bbe7fd6";//在极光推送官网注册后获得的密码(请改为你自己注册后的值) public static String REGISTRATION_ID = "";//安装APP的用户在极光推送中注册的ID @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } public void init(){ JPushInterface.init(getApplicationContext()); //获取初始化之后的环境 APP_KEY = JpushUtil.getAppKey(getApplicationContext()); REGISTRATION_ID = JPushInterface.getRegistrationID(getApplicationContext()); } public static String getJPushData(){ return "appKey:"+APP_KEY+";masterSecret:"+MASTER_SECRET+";registrationId:"+REGISTRATION_ID; } }
<!-- 用于初始化极光推送的注册数据,不显示给用户 --> <activity android:name=".jpush.JPushInitActivity" android:theme="@android:style/Theme.NoDisplay"> </activity>
mui.plusReady(function() { //调用原生Activity var Intent = plus.android.importClass("android.content.Intent"); // 获取主Activity对象的实例 var main = plus.android.runtimeMainActivity(); // 创建Intent var naviIntent = new Intent(); var ComponentName = plus.android.importClass("android.content.ComponentName"); //创建极光推送注册Activity,包名请换成你自己的包名 naviIntent.setComponent(new ComponentName(main, "com.xxx.xxx.xxx.JPushInitActivity")); main.startActivity(naviIntent); //调用java方法 $(".login_btn").click(function(){ //引入java类文件,包名请换成你自己的包名 var jPush = plus.android.importClass("com.xxx.xxx.xxx.JPushInitActivity"); //调用静态方法 var jPushData = jPush.getJPushData(); //输出返回值 alert(jPushData); //TODO 在登录时将jPushData及用户名、密码一并传给java后端
//...... }) })
//验证成功,将jPushId与用户关联起来 try { String[] split = jPushData.split(";"); for (String str : split) { if ("registrationId".equals(str.split(":")[0])) { String jPushId = str.split(":")[1]; String sql = "UPDATE BO_PBS_ORG_EXT_USER SET J_PUSH_ID = ? WHERE YHZH = ?"; Object[] params = { jPushId, loginUserModel.getUSER_ACCOUNT() }; db.update(sql, params); break; } } } catch (Exception e) { e.printStackTrace(); }
public interface JpushMessageService { /** * 发送给所有用户 * @param content * @return */ public String sendPushAll(String content); /** * 发送给userId对应的用户 * @param userId * @param content * @return */ public String senPushByUserId(String userId, String content); }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.pbs.mobile.business.service.JpushMessageService; import com.pbs.mobile.util.JPushUtil; import com.pbs.mobile.util.ReturnUtil; import com.pbs.workflow.service.DataBaseService; import com.pbs.workflow.service.UserService; import cn.jiguang.common.ClientConfig; 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.PushPayload; @Service public class JpushMessageServiceImpl implements JpushMessageService { @Resource UserService userService; @Resource DataBaseService db; private final static String appKey = "weqwewe1123123123123";//这里请填写自己注册获得的值,或者通过mui向后台传值 private final static String masterSecret = "213123123asdjfoi1293";//这里请填写自己注册获得的值,或者通过mui向后台传值 /** * 保存离线的时长。秒为单位。最多支持10天(864000秒)。 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。 * 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。 */ private static long timeToLive = 60 * 60 * 24; private static JPushClient jPushClient = null; private static final Logger logger = Logger.getLogger(JpushMessageServiceImpl.class); @Override public String sendPushAll(String content) { ClientConfig config = ClientConfig.getInstance(); config.setMaxRetryTimes(5); config.setConnectionTimeout(10 * 1000); config.setGlobalPushSetting(false, timeToLive); jPushClient = new JPushClient(masterSecret, appKey, null, config); boolean flag = false; try { PushPayload payload = JPushUtil.buildPushObject_all_all_alert(content); PushResult result = jPushClient.sendPush(payload); if (null != result) { logger.info("Get resul ---" + result); flag = true; } } catch (APIConnectionException e) { logger.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("Error response from JPush server. Should review and fix it. ", e); logger.info("HTTP Status: " + e.getStatus()); logger.info("Error Code: " + e.getErrorCode()); logger.info("Error Message: " + e.getErrorMessage()); logger.info("Msg ID: " + e.getMsgId()); } Map<String, Object> result = new HashMap<String, Object>(); if (flag) { result.put("status", "ok"); result.put("code", "0"); result.put("msg", "发送成功"); } else { result.put("status", "fail"); result.put("code", "-1"); result.put("msg", "发送失败"); } return ReturnUtil.getJsonStr(result); } @Override public String senPushByUserId(String userId, String content) { boolean flag = false; try { //在数据库中查询极光推送注册信息以及是否接受推送(可以根据自己业务省略是否接收推送的判断)
//eu.J_PUSH_ID就是我们在登录之后跟用户管理起来的registrationId
String sql = "SELECT eu.J_PUSH_ID,eu.ACCEPT_PUSH FROM PBS_ORG_USER u JOIN BO_PBS_ORG_EXT_USER eu ON u.USER_ACCOUNT = eu.YHZH AND u.ID = ?"; Object[] params = { userId }; List<Map<String, Object>> records = db.queryList(sql, params); if (records != null && records.size() > 0) { Boolean acceptPush = records.get(0).get("ACCEPT_PUSH") == null ? true : (boolean) records.get(0).get("ACCEPT_PUSH"); String jPushId = records.get(0).get("J_PUSH_ID") == null ? "" : (String) records.get(0).get("J_PUSH_ID"); if (acceptPush && StringUtils.isNotEmpty(jPushId)) { JPushClient jPushClient = new JPushClient(masterSecret, appKey); List<String> regeSterIds = new ArrayList<>(); regeSterIds.add(jPushId); try { PushPayload payload = JPushUtil.buildPushObject_all_all_regesterIds(regeSterIds, content); PushResult pushResult = jPushClient.sendPush(payload); if (null != pushResult) { logger.info("Get result ----" + pushResult); flag = true; } } catch (APIConnectionException e) { logger.error("Connection error. Should retry later. ", e); } catch (APIRequestException e) { logger.error("Error response from JPush server. Should review and fix it. ", e); logger.info("HTTP Status: " + e.getStatus()); logger.info("Error Code: " + e.getErrorCode()); logger.info("Error Message: " + e.getErrorMessage()); logger.info("Msg ID: " + e.getMsgId()); } } } } catch (Exception e) { e.printStackTrace(); } Map<String, Object> result = new HashMap<String, Object>(); if (flag) { result.put("status", "ok"); result.put("code", "0"); result.put("msg", "发送成功"); } else { result.put("status", "fail"); result.put("code", "-1"); result.put("msg", "发送失败"); } return ReturnUtil.getJsonStr(result); } }
AndroidStudio离线打包MUI集成JPush极光推送并在java后端管理推送
标签:增加 boolean 3.4 override simple || title record 输出
原文地址:https://www.cnblogs.com/LiQ0116/p/9290892.html