如何離線打包請參看上篇隨筆《AndroidStudio離線打包MUI》javascript
官方文檔:https://docs.jiguang.cn/jpush/client/Android/android_guide/html
建議採用 jcenter 自動集成 的方式,手動集成對新手來講容易出錯java
使用jcenter自動集成的開發者,不須要在項目中添加jar和so,jcenter會自動完成依賴;在AndroidManifest.xml中不須要添加任何JPush SDK 相關的配置,jcenter會自動導入。node
若是開發者須要修改組件屬性,能夠在本地的 AndroidManifest 中定義同名的組件並配置想要的屬性,而後用 xmlns:tools 來控制本地組件覆蓋 jcenter 上的組件。示例:android
<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默認配置就支持)git
buildscript { repositories { jcenter() } ...... } allprojets { repositories { jcenter() } }
在 module 的 gradle 中添加依賴和AndroidManifest的替換變量。github
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 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); } }