AndroidStudio离线打包MUI集成JPush极光推送并在java后端管理推送

时间:2021-03-26 15:52:20

1.AndroidStudio离线打包MUI

   如何离线打包请参看上篇随笔《AndroidStudio离线打包MUI

 

2.集成极光推送

  官方文档:https://docs.jiguang.cn/jpush/client/Android/android_guide/

  建议采用 jcenter 自动集成 的方式,手动集成对新手来说容易出错

  使用jcenter自动集成的开发者,不需要在项目中添加jar和so,jcenter会自动完成依赖;在AndroidManifest.xml中不需要添加任何JPush SDK 相关的配置,jcenter会自动导入。

  • 如果开发者需要修改组件属性,可以在本地的 AndroidManifest 中定义同名的组件并配置想要的属性,然后用 xmlns:tools 来控制本地组件覆盖 jcenter 上的组件。示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <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默认配置就支持)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    buildscript {
         repositories {
             jcenter()
         }
         ......
    }
     
    allprojets {
         repositories {
             jcenter()
         }
    }
  • 在 module 的 gradle 中添加依赖和AndroidManifest的替换变量。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    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 版本为例。
         ......
    }

 

3.Android项目中增加注册、获取极光推送相关信息的代码

  3.1.在AS中增加JPushUtil工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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);
     }
}

  

  3.2.增加JPushInitActivity类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
     }
 
}

  

  3.3.在AndroidManifest.xml中注册JPushInitActivity

1
2
3
4
5
<!-- 用于初始化极光推送的注册数据,不显示给用户  -->
        <activity
            android:name= ".jpush.JPushInitActivity"
            android:theme= "@android:style/Theme.NoDisplay" >
        </activity>

  

  3.4.在mui.js中创建Activity并完成注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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后端<br>          //......
     })
  })

  

4.java端代码

 

   下载极光推送的jar包并引入到项目: https://github.com/jpush/jpush-api-java-client/releases

 

  4.1 接收登录信息,将registrationId与用户绑定,请根据自己的业务去进行关联

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    //验证成功,将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();
}

 

  4.2 新增service接口JpushMessageService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public  interface  JpushMessageService {
 
     /**
      * 发送给所有用户
      * @param content
      * @return
      */
     public String sendPushAll(String content);
 
     /**
      * 发送给userId对应的用户
      * @param userId
      * @param content
      * @return
      */
     public String senPushByUserId(String userId, String content);
 
}

  

 

  4. 3 新增service接口实现类JpushMessageServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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  {
             //在数据库中查询极光推送注册信息以及是否接受推送(可以根据自己业务省略是否接收推送的判断)<br>              //eu.J_PUSH_ID就是我们在登录之后跟用户管理起来的registrationId
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
             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);
     }
 
}

  

 

每天学习一点点,不知不觉成大神~