diff --git a/app/build.gradle b/app/build.gradle index 88e6d91..baf2fbe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -159,12 +159,15 @@ dependencies { implementation 'com.jakewharton.rxbinding4:rxbinding:4.0.0' implementation 'com.jeremyliao:live-event-bus-x:1.7.3' + //MMKV implementation 'com.tencent:mmkv-static:1.2.14' //bugly implementation 'com.tencent.bugly:crashreport:4.1.9.2' //xcrash implementation 'com.iqiyi.xcrash:xcrash-android-lib:3.1.0' + //阿里云推送 + implementation 'com.aliyun.ams:alicloud-android-push:3.9.3' //工具类 implementation 'com.blankj:utilcodex:1.31.1' @@ -177,4 +180,6 @@ dependencies { //沉浸状态栏 implementation 'com.gitee.zackratos:UltimateBarX:0.8.0' implementation 'com.github.andywu91:XAPKInstaller:1.0.0' + //Java WebSocket + implementation "org.java-websocket:Java-WebSocket:1.5.3" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7e7c2ec..5549932 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -44,10 +44,11 @@ - + android:exported="true" + android:launchMode="singleTop" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/mir4updater/base/BaseApplication.java b/app/src/main/java/com/example/mir4updater/base/BaseApplication.java index 4f0f793..c4df0c7 100644 --- a/app/src/main/java/com/example/mir4updater/base/BaseApplication.java +++ b/app/src/main/java/com/example/mir4updater/base/BaseApplication.java @@ -5,26 +5,49 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; +import com.alibaba.sdk.android.push.CloudPushService; +import com.alibaba.sdk.android.push.CommonCallback; +import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory; import com.example.mir4updater.BuildConfig; +import com.example.mir4updater.config.CommonConfig; +import com.example.mir4updater.push.alipush.AliMessageIntentService; +import com.example.mir4updater.utils.DevicesUtils; import com.hjq.toast.Toaster; import com.tencent.mmkv.MMKV; -public class BaseApplication extends Application { +public class BaseApplication extends Application implements CommonCallback { private static final String TAG = BaseApplication.class.getSimpleName(); + CloudPushService mCloudPushService; + @Override public void onCreate() { super.onCreate(); + if (!BuildConfig.DEBUG) { catchException(); } String rootDir = MMKV.initialize(this); Log.e(TAG, "mmkv root: " + rootDir); + Log.e(TAG, "onCreate: getUuid = " + DevicesUtils.getUuid()); // CrashReport.initCrashReport(getApplicationContext(), "8db7b63e60", false); xcrash.XCrash.init(this); Toaster.init(this); + initAliyunPush(); + } + + private void initAliyunPush() { + PushServiceFactory.init(this); + mCloudPushService = PushServiceFactory.getCloudPushService(); + //收到通知不回调,logcat有日志加这行代码 + mCloudPushService.setPushIntentService(AliMessageIntentService.class); //在这里替换成您自己的service +// if (BuildConfig.DEBUG) { + //仅适用于Debug包,正式包不需要此行 + mCloudPushService.setLogLevel(CloudPushService.LOG_DEBUG); +// } + mCloudPushService.register(this, this); } private void catchException() { @@ -34,6 +57,7 @@ public class BaseApplication extends Application { public void uncaughtException(Thread t, Throwable e) { Log.e("捕获异常子线程:", Thread.currentThread().getName() + "在:" + e.getStackTrace()[0].getClassName()); + e.printStackTrace(); } } ); @@ -54,4 +78,40 @@ public class BaseApplication extends Application { } + @Override + public void onSuccess(String response) { + Log.e("AliyunPush", "init cloudchannel success"); + Log.e("AliyunPush", "init cloudchannel success DeviceId = " + mCloudPushService.getDeviceId()); + String sn = DevicesUtils.getUuid(); + mCloudPushService.bindAccount(sn, new CommonCallback() { + @Override + public void onSuccess(String s) { + Log.e("AliyunPush", "bind account " + sn + " success\n"); + } + + @Override + public void onFailed(String errorCode, String errorMsg) { + Log.e("AliyunPush", "bind account " + sn + " failed." + + "errorCode: " + errorCode + ", errorMsg:" + errorMsg); + } + }); + mCloudPushService.addAlias(sn, new CommonCallback() { + @Override + public void onSuccess(String s) { + Log.e("AliyunPush", "add alias " + sn + " success\n"); + } + + @Override + public void onFailed(String errorCode, String errorMsg) { + Log.e("AliyunPush", "add alias " + sn + " failed." + + "errorCode: " + errorCode + ", errorMsg:" + errorMsg + "\n"); + } + }); + } + + @Override + public void onFailed(String errorCode, String errorMessage) { + Log.e("AliyunPush", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage); + + } } diff --git a/app/src/main/java/com/example/mir4updater/config/CommonConfig.java b/app/src/main/java/com/example/mir4updater/config/CommonConfig.java index d5b5b07..131ac98 100644 --- a/app/src/main/java/com/example/mir4updater/config/CommonConfig.java +++ b/app/src/main/java/com/example/mir4updater/config/CommonConfig.java @@ -2,6 +2,9 @@ package com.example.mir4updater.config; public class CommonConfig { public static final String MMKV_ID = "InterProcessKV"; + public static final String DEVICE_UUID_KEY = "device_uuid"; + + public static final String MIR4_PACKAGE_NAME ="com.wemade.mir4global"; public static final String MIR4_CONTENT_PATH = "content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata%2Fcom.wemade.mir4global%2Ffiles"; diff --git a/app/src/main/java/com/example/mir4updater/push/alipush/AliMessageIntentService.java b/app/src/main/java/com/example/mir4updater/push/alipush/AliMessageIntentService.java new file mode 100644 index 0000000..427312c --- /dev/null +++ b/app/src/main/java/com/example/mir4updater/push/alipush/AliMessageIntentService.java @@ -0,0 +1,102 @@ +package com.example.mir4updater.push.alipush; + +import android.content.Context; +import android.util.Log; + +import com.alibaba.sdk.android.push.AliyunMessageIntentService; +import com.alibaba.sdk.android.push.notification.CPushMessage; + +import java.util.Map; + +/** + * Created by liyazhou on 17/8/22. + * 为避免推送广播被系统拦截的小概率事件,我们推荐用户通过IntentService处理消息互调,接入步骤: + * 1. 创建IntentService并继承AliyunMessageIntentService + * 2. 覆写相关方法,并在Manifest的注册该Service + * 3. 调用接口CloudPushService.setPushIntentService + * 详细用户可参考:https://help.aliyun.com/document_detail/30066.html#h2-2-messagereceiver-aliyunmessageintentservice + */ + +public class AliMessageIntentService extends AliyunMessageIntentService { + private static final String TAG = "AliyunMessageIntentService"; + + /** + * 推送通知的回调方法 + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + protected void onNotification(Context context, String title, String summary, Map extraMap) { + Log.i(TAG, "收到一条推送通知 : " + title + ", summary:" + summary); + } + + /** + * 推送消息的回调方法 + * + * @param context + * @param cPushMessage + */ + @Override + protected void onMessage(Context context, CPushMessage cPushMessage) { + Log.i(TAG, "收到一条推送消息 : " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent()); + String title = cPushMessage.getTitle(); + String content = cPushMessage.getContent(); + + } + + /** + * 从通知栏打开通知的扩展处理 + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + protected void onNotificationOpened(Context context, String title, String summary, String extraMap) { + Log.i(TAG, "onNotificationOpened : " + " : " + title + " : " + summary + " : " + extraMap); + } + + /** + * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) { + Log.i(TAG, "onNotificationClickedWithNoAction : " + " : " + title + " : " + summary + " : " + extraMap); + } + + /** + * 通知删除回调 + * + * @param context + * @param messageId + */ + @Override + protected void onNotificationRemoved(Context context, String messageId) { + Log.i(TAG, "onNotificationRemoved : " + messageId); + } + + /** + * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html#h3-3-4-basiccustompushnotification-api + * + * @param context + * @param title + * @param summary + * @param extraMap + * @param openType + * @param openActivity + * @param openUrl + */ + @Override + protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) { + Log.i(TAG, "onNotificationReceivedInApp : " + " : " + title + " : " + summary + " " + extraMap + " : " + openType + " : " + openActivity + " : " + openUrl); + } +} diff --git a/app/src/main/java/com/example/mir4updater/push/alipush/AliyunMessageReceiver.java b/app/src/main/java/com/example/mir4updater/push/alipush/AliyunMessageReceiver.java new file mode 100644 index 0000000..1500a0c --- /dev/null +++ b/app/src/main/java/com/example/mir4updater/push/alipush/AliyunMessageReceiver.java @@ -0,0 +1,105 @@ +package com.example.mir4updater.push.alipush; + +import android.content.Context; +import android.util.Log; + +import com.alibaba.sdk.android.push.MessageReceiver; +import com.alibaba.sdk.android.push.notification.CPushMessage; + +import java.util.Map; + +/** + * @author: 正纬 + * @since: 15/4/9 + * @version: 1.1 + * @feature: 用于接收推送的通知和消息 + */ +public class AliyunMessageReceiver extends MessageReceiver { + // 消息接收部分的LOG_TAG + public static final String TAG = "AliyunMessageReceiver"; + + /** + * 推送通知的回调方法 + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + public void onNotification(Context context, String title, String summary, Map extraMap) { + // TODO 处理推送通知 + if (null != extraMap) { + for (Map.Entry entry : extraMap.entrySet()) { + Log.i(TAG, "@Get diy param : Key=" + entry.getKey() + " , Value=" + entry.getValue()); + } + } else { + Log.i(TAG, "@收到通知 && 自定义消息为空"); + } + Log.i(TAG, "收到一条推送通知 : " + title + ", summary:" + summary); + } + + /** + * 应用处于前台时通知到达回调。注意:该方法仅对自定义样式通知有效,相关详情请参考https://help.aliyun.com/document_detail/30066.html?spm=5176.product30047.6.620.wjcC87#h3-3-4-basiccustompushnotification-api + * + * @param context + * @param title + * @param summary + * @param extraMap + * @param openType + * @param openActivity + * @param openUrl + */ + @Override + protected void onNotificationReceivedInApp(Context context, String title, String summary, Map extraMap, int openType, String openActivity, String openUrl) { + Log.i(TAG, "onNotificationReceivedInApp : " + " : " + title + " : " + summary + " " + extraMap + " : " + openType + " : " + openActivity + " : " + openUrl); + } + + /** + * 推送消息的回调方法 + * + * @param context + * @param cPushMessage + */ + @Override + public void onMessage(Context context, CPushMessage cPushMessage) { + Log.i(TAG, "收到一条推送消息 : " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent()); + } + + /** + * 从通知栏打开通知的扩展处理 + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + public void onNotificationOpened(Context context, String title, String summary, String extraMap) { + Log.i(TAG, "onNotificationOpened : " + " : " + title + " : " + summary + " : " + extraMap); + } + + /** + * 通知删除回调 + * + * @param context + * @param messageId + */ + @Override + public void onNotificationRemoved(Context context, String messageId) { + Log.i(TAG, "onNotificationRemoved : " + messageId); + } + + /** + * 无动作通知点击回调。当在后台或阿里云控制台指定的通知动作为无逻辑跳转时,通知点击回调为onNotificationClickedWithNoAction而不是onNotificationOpened + * + * @param context + * @param title + * @param summary + * @param extraMap + */ + @Override + protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) { + Log.i(TAG, "onNotificationClickedWithNoAction : " + " : " + title + " : " + summary + " : " + extraMap); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/mir4updater/utils/DevicesUtils.java b/app/src/main/java/com/example/mir4updater/utils/DevicesUtils.java new file mode 100644 index 0000000..d70f87f --- /dev/null +++ b/app/src/main/java/com/example/mir4updater/utils/DevicesUtils.java @@ -0,0 +1,22 @@ +package com.example.mir4updater.utils; + +import android.text.TextUtils; + +import com.example.mir4updater.config.CommonConfig; +import com.tencent.mmkv.MMKV; + +import java.util.UUID; + +public class DevicesUtils { + public static String getUuid() { + MMKV mmkv = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE); + String uuid = mmkv.decodeString(CommonConfig.DEVICE_UUID_KEY, ""); + if (TextUtils.isEmpty(uuid)) { + String id = UUID.randomUUID().toString().replace("-", ""); // 生成无连字符的32位UUID + mmkv.encode(CommonConfig.DEVICE_UUID_KEY, id); + return id; + } else { + return uuid; + } + } +}