From 7c525aec79ec09365eb4d91a36154084ebfbf616 Mon Sep 17 00:00:00 2001 From: chenjuntao Date: Fri, 22 Feb 2019 15:35:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=8A=9F=E8=83=BD=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20https://gitlab.ghzhushou.com/pm/halo-app-issues/iss?= =?UTF-8?q?ues/451?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gh/base/GHUmengNotificationService.kt | 20 ++++------ .../main/java/com/gh/common/PushManager.kt | 2 + .../gh/common/util/GameRepositoryHelper.kt | 4 +- .../entity/PushNotificationEntity.kt | 7 +++- .../receiver/UmengMessageReceiver.kt | 38 +++++++++++++++++++ .../retrofit/service/ApiService.java | 10 +++++ 6 files changed, 66 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/gh/base/GHUmengNotificationService.kt b/app/src/main/java/com/gh/base/GHUmengNotificationService.kt index 6877ae239f..b9e0098b37 100644 --- a/app/src/main/java/com/gh/base/GHUmengNotificationService.kt +++ b/app/src/main/java/com/gh/base/GHUmengNotificationService.kt @@ -16,6 +16,7 @@ import com.gh.common.notifier.Notifier import com.gh.common.util.DataUtils import com.gh.common.util.EntranceUtils import com.gh.common.util.StringUtils +import com.gh.common.util.toObject import com.gh.gamecenter.R import com.gh.gamecenter.entity.PushEntity import com.gh.gamecenter.entity.PushMessageEntity @@ -45,22 +46,16 @@ class GHUmengNotificationService : UmengMessageService() { companion object { const val ACTION_UMENG = "com.gh.gamecenter.UMENG" - const val MESSAGE_FROM_SYSTEM = "message_from_system" - const val HALO_MESSAGE_DIALOG = "HALO_MESSAGE_DIALOG" - const val HALO_MESSAGE_CENTER = "HALO_MESSAGE_CENTER" - const val ANSWER = "answer" - const val FOLLOW_QUESTION = "follow_question" - const val NOTIFICATION_ID = 2015 - const val DISPLAY_TYPE_NOTIFICATION = "notification" - const val DISPLAY_TYPE_CUSTOM = "custom" + const val MESSAGE_ID = "message_id" + const val PUSH_ID = "push_id" } val notificationTags = arrayOf("GH_UMENG_TAG_1", "GH_UMENG_TAG_2", "GH_UMENG_TAG_3") @@ -71,7 +66,7 @@ class GHUmengNotificationService : UmengMessageService() { val isMessageFromSystem = intent.getBooleanExtra(MESSAGE_FROM_SYSTEM, false) try { - val pushData = gson.fromJson(message, PushEntity::class.java) + val pushData = message.toObject() pushData?.let { handlePushData(context, it, message, isMessageFromSystem) } } catch (e: Exception) { e.printStackTrace() @@ -84,10 +79,10 @@ class GHUmengNotificationService : UmengMessageService() { if (pushData.displayType == DISPLAY_TYPE_NOTIFICATION) { // 其它类型的透传信息 // 显示到通知栏 - val msg = gson.fromJson(message, PushNotificationEntity::class.java) + val msg = message.toObject() val data = msg?.extra?.data - // 系统推送,直接处理跳转 + // 系统推送(非自定义信息),直接处理跳转 if (isMessageFromSystem) { val intent = Intent() intent.setClass(context, UmengMessageReceiver::class.java) @@ -98,13 +93,14 @@ class GHUmengNotificationService : UmengMessageService() { return } - // 判断是否过滤该消息 val clickIntent = Intent() val removeIntent = Intent() clickIntent.setClass(context, UmengMessageReceiver::class.java) clickIntent.putExtra(EntranceUtils.KEY_DATA, data?.link) clickIntent.putExtra(EntranceUtils.KEY_MESSAGE, message) + clickIntent.putExtra(MESSAGE_ID, msg?.msgId) + clickIntent.putExtra(PUSH_ID, data?.pushId) clickIntent.putExtra(EntranceUtils.KEY_TYPE, TYPE_CLICK) removeIntent.setClass(context, UmengMessageReceiver::class.java) diff --git a/app/src/main/java/com/gh/common/PushManager.kt b/app/src/main/java/com/gh/common/PushManager.kt index 1b1dce926d..f0159b699e 100644 --- a/app/src/main/java/com/gh/common/PushManager.kt +++ b/app/src/main/java/com/gh/common/PushManager.kt @@ -1,5 +1,6 @@ package com.gh.common +import android.annotation.SuppressLint import android.preference.PreferenceManager import com.gh.base.GHUmengNotificationService import com.gh.common.constant.Config @@ -77,6 +78,7 @@ object PushManager { }) } + @SuppressLint("CheckResult") @JvmStatic fun getAndSetAlias() { if (deviceToken.isNullOrEmpty()) { diff --git a/app/src/main/java/com/gh/common/util/GameRepositoryHelper.kt b/app/src/main/java/com/gh/common/util/GameRepositoryHelper.kt index d307eda1c7..16d2ca3e7b 100644 --- a/app/src/main/java/com/gh/common/util/GameRepositoryHelper.kt +++ b/app/src/main/java/com/gh/common/util/GameRepositoryHelper.kt @@ -79,7 +79,9 @@ object GameRepositoryHelper { } /** - * 从游戏库相应的专题中取出一个与其它游戏都不相同的游戏,为空时即为游戏用完或不存在该相应专题 + * 从补充游戏库相应的专题中取出一个与其它游戏都不相同的游戏,为空时即为游戏用完或不存在该相应专题 + * @param collectionId 补充游戏库相应专题 ID + * @param gameIdList 该专题里已经包含的游戏 ID 列表 */ fun getOneUniqueGame(collectionId: String?, gameIdList: List): GameEntity? { collectionId?.let { diff --git a/app/src/main/java/com/gh/gamecenter/entity/PushNotificationEntity.kt b/app/src/main/java/com/gh/gamecenter/entity/PushNotificationEntity.kt index c8ae3446d1..b49ff729ff 100644 --- a/app/src/main/java/com/gh/gamecenter/entity/PushNotificationEntity.kt +++ b/app/src/main/java/com/gh/gamecenter/entity/PushNotificationEntity.kt @@ -30,8 +30,11 @@ data class PushNotificationEntity( data class Extra(var data: Data? = null) - data class Data(var condition: Condition? = null, - var link: Link? = null) { + data class Data( + @SerializedName("push_id") + var pushId: String? = "", + var condition: Condition? = null, + var link: Link? = null) { @Parcelize data class Link(var type: String? = "", var target: String? = "") : Parcelable diff --git a/app/src/main/java/com/gh/gamecenter/receiver/UmengMessageReceiver.kt b/app/src/main/java/com/gh/gamecenter/receiver/UmengMessageReceiver.kt index 333e505fdb..28cba6f24d 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/UmengMessageReceiver.kt +++ b/app/src/main/java/com/gh/gamecenter/receiver/UmengMessageReceiver.kt @@ -3,11 +3,20 @@ package com.gh.gamecenter.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import com.gh.base.GHUmengNotificationService +import com.gh.common.PushManager import com.gh.common.util.DirectUtils import com.gh.common.util.EntranceUtils import com.gh.gamecenter.entity.PushNotificationEntity +import com.gh.gamecenter.retrofit.Response +import com.gh.gamecenter.retrofit.RetrofitManager import com.umeng.message.UTrack import com.umeng.message.entity.UMessage +import io.reactivex.schedulers.Schedulers +import okhttp3.MediaType +import okhttp3.RequestBody +import okhttp3.ResponseBody +import org.json.JSONException import org.json.JSONObject class UmengMessageReceiver : BroadcastReceiver() { @@ -31,6 +40,12 @@ class UmengMessageReceiver : BroadcastReceiver() { // 记录该推送通知被点击 UTrack.getInstance(context).trackMsgClick(msgObject) + // TODO 记录点击了推送 + val msgId = intent.getStringExtra(GHUmengNotificationService.MESSAGE_ID) + val pushId = intent.getStringExtra(GHUmengNotificationService.PUSH_ID) + + postClickAction(context, msgId, pushId) + if (link == null || link.target == "system") { jumpToHaloOfficialNotificationPage(context) } else { @@ -54,6 +69,29 @@ class UmengMessageReceiver : BroadcastReceiver() { } } + private fun postClickAction(context: Context, msgId: String?, pushId: String?) { + val jsonObject = JSONObject() + try { + jsonObject.put("device_token", PushManager.deviceToken) + jsonObject.put("push_id", pushId) + jsonObject.put("msg_id", msgId) + } catch (e: JSONException) { + e.printStackTrace() + } + + val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString()) + + RetrofitManager.getInstance(context) + .api + .postUmengReceiveInfo(body) + .subscribeOn(Schedulers.io()) + .subscribe(object : Response() { + override fun onError(e: Throwable) { + e.printStackTrace() + } + }) + } + private fun jumpToHaloOfficialNotificationPage(context: Context) { DirectUtils.directToOfficialNotification(context, EntranceUtils.ENTRANCE_UMENG) } diff --git a/app/src/main/java/com/gh/gamecenter/retrofit/service/ApiService.java b/app/src/main/java/com/gh/gamecenter/retrofit/service/ApiService.java index b62c1b8e65..4cee1d764d 100644 --- a/app/src/main/java/com/gh/gamecenter/retrofit/service/ApiService.java +++ b/app/src/main/java/com/gh/gamecenter/retrofit/service/ApiService.java @@ -1490,6 +1490,9 @@ public interface ApiService { @GET("communities/{community_id}/suggested_follows") Observable> getSuggestedFollows(@Path("community_id") String communityId); + /** + * 百度ocpc激活 + */ @POST("baidu/ocpc:activate") Observable postBaiduActivationInfo(); @@ -1502,4 +1505,11 @@ public interface ApiService { */ @GET("reserve_columns") Observable> getReserveColumns(); + + /** + * 标记用户点击了该推送 + */ + @POST("./umeng:receive") + Observable postUmengReceiveInfo(@Body RequestBody body); + } \ No newline at end of file