合并
This commit is contained in:
@ -1,85 +0,0 @@
|
||||
package com.gh.base;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.umeng.message.UmengMessageService;
|
||||
|
||||
import org.android.agoo.common.AgooConstants;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GHUmengNotificationService extends UmengMessageService {
|
||||
|
||||
public static final String ACTION_UMENG = "com.gh.gamecenter.UMENG";
|
||||
|
||||
public final String[] notificationTags = {"GH_UMENG_TAG_1", "GH_UMENG_TAG_2", "GH_UMENG_TAG_3"};
|
||||
|
||||
public static final int NOTIFICATION_ID = 2015;
|
||||
|
||||
@Override
|
||||
public void onMessage(Context context, Intent intent) {
|
||||
NotificationManager notificationManager = (NotificationManager)
|
||||
context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
String message = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
|
||||
|
||||
if (true) { // 显示到通知栏
|
||||
Intent targetIntent = new Intent(ACTION_UMENG);
|
||||
targetIntent.putExtra(EntranceUtils.KEY_DATA, message); // todo 传输数据
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 111, // todo requestCode
|
||||
targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
final Notification notification = new NotificationCompat.Builder(context, "")
|
||||
.setSmallIcon(R.drawable.logo)
|
||||
.setTicker("setTicker")
|
||||
.setContentTitle("setContentTitle")
|
||||
.setContentText("setContentText")
|
||||
.setContentIntent(pendingIntent)
|
||||
.build();
|
||||
notification.flags |= Notification.FLAG_AUTO_CANCEL;
|
||||
|
||||
if (notificationManager != null) {
|
||||
notificationManager.notify(getNotificationTag(context), NOTIFICATION_ID, notification);
|
||||
}
|
||||
} else { // 其他处理
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则:最多三条消息,以旧换新
|
||||
*
|
||||
* @return NotificationTag
|
||||
*/
|
||||
private String getNotificationTag(Context context) {
|
||||
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor edit = sp.edit();
|
||||
|
||||
Map<Long, String> timeTagMap = new HashMap<>();
|
||||
for (String tag : notificationTags) {
|
||||
long time = sp.getLong(tag, 0);
|
||||
if (time == 0) {
|
||||
edit.putLong(tag, System.currentTimeMillis()).apply();
|
||||
return tag;
|
||||
} else {
|
||||
timeTagMap.put(time, tag);
|
||||
}
|
||||
}
|
||||
|
||||
Long minTime = Collections.min(timeTagMap.keySet());
|
||||
String tag = timeTagMap.get(minTime);
|
||||
edit.putLong(tag, System.currentTimeMillis()).apply();
|
||||
return TextUtils.isEmpty(tag) ? notificationTags[0] : tag;
|
||||
}
|
||||
}
|
||||
145
app/src/main/java/com/gh/base/GHUmengNotificationService.kt
Normal file
145
app/src/main/java/com/gh/base/GHUmengNotificationService.kt
Normal file
@ -0,0 +1,145 @@
|
||||
package com.gh.base
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.preference.PreferenceManager
|
||||
import android.support.v4.app.NotificationCompat
|
||||
import android.text.TextUtils
|
||||
import android.view.View
|
||||
import com.gh.common.notifier.Notifier
|
||||
import com.gh.common.util.EntranceUtils
|
||||
import com.gh.gamecenter.R
|
||||
import com.gh.gamecenter.entity.MessageEntity
|
||||
import com.gh.gamecenter.entity.MessageUnreadEntity
|
||||
import com.gh.gamecenter.entity.PushEntity
|
||||
import com.gh.gamecenter.entity.PushNotificationEntity
|
||||
import com.gh.gamecenter.message.MessageUnreadRepository
|
||||
import com.gh.gamecenter.qa.answer.detail.AnswerDetailActivity
|
||||
import com.google.gson.Gson
|
||||
import com.umeng.message.UmengMessageService
|
||||
import org.android.agoo.common.AgooConstants
|
||||
import java.util.*
|
||||
|
||||
class GHUmengNotificationService : UmengMessageService() {
|
||||
|
||||
companion object {
|
||||
const val ACTION_UMENG = "com.gh.gamecenter.UMENG"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
val notificationTags = arrayOf("GH_UMENG_TAG_1", "GH_UMENG_TAG_2", "GH_UMENG_TAG_3")
|
||||
val gson = Gson()
|
||||
|
||||
override fun onMessage(context: Context, intent: Intent) {
|
||||
val message = intent.getStringExtra(AgooConstants.MESSAGE_BODY)
|
||||
|
||||
val pushData = gson.fromJson(message, PushEntity::class.java)
|
||||
|
||||
pushData?.let { handlePushData(context, it) }
|
||||
}
|
||||
|
||||
private fun handlePushData(context: Context, pushData: PushEntity) {
|
||||
val notificationManager = context.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
if (HALO_MESSAGE_CENTER == pushData.body?.custom) {
|
||||
// 回答了问题或者关注了问题的消息
|
||||
val msg = gson.fromJson(pushData.extra?.data, MessageEntity::class.java)
|
||||
|
||||
val type = if (ANSWER == msg?.type) {
|
||||
"回答了你的问题"
|
||||
} else {
|
||||
"回答你关注的问题"
|
||||
}
|
||||
|
||||
val displayText = msg?.userEntity?.name + type
|
||||
|
||||
if (Notifier.isActivityValid(CurrentActivityHolder.getCurrentActivity())) {
|
||||
Notifier.create(CurrentActivityHolder.getCurrentActivity())
|
||||
.setText(displayText)
|
||||
.setDuration(5000)
|
||||
.setIcon(msg?.userEntity?.icon)
|
||||
.setOnClickListener(View.OnClickListener { v ->
|
||||
val bundle = Bundle()
|
||||
bundle.putString(EntranceUtils.KEY_ANSWER_ID, msg?.answer?.id)
|
||||
bundle.putString(EntranceUtils.KEY_ENTRANCE, EntranceUtils.ENTRANCE_UMENG)
|
||||
bundle.putString(EntranceUtils.KEY_TO, AnswerDetailActivity::class.java.name)
|
||||
EntranceUtils.jumpActivity(context, bundle)
|
||||
|
||||
Notifier.hide()
|
||||
})
|
||||
.show(true)
|
||||
}
|
||||
} else if (HALO_MESSAGE_DIALOG == pushData.body?.custom) {
|
||||
// 消息中心逻辑
|
||||
// TODO 确定是否加推送时间跟查询时间的比对
|
||||
val msg = gson.fromJson(pushData.extra?.data, MessageUnreadEntity::class.java)
|
||||
MessageUnreadRepository.messageLiveData.postValue(msg)
|
||||
} else {
|
||||
// 其它类型的透传信息
|
||||
// 显示到通知栏
|
||||
val msg = gson.fromJson(pushData.extra?.data, PushNotificationEntity::class.java)
|
||||
|
||||
val targetIntent = Intent(ACTION_UMENG)
|
||||
|
||||
targetIntent.putExtra(EntranceUtils.KEY_DATA, msg?.link)
|
||||
val pendingIntent = PendingIntent.getBroadcast(context, 111, // todo requestCode
|
||||
targetIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel("Halo", "Halo", NotificationManager.IMPORTANCE_DEFAULT)
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
val notification = NotificationCompat.Builder(context, "Halo")
|
||||
.setSmallIcon(R.drawable.logo)
|
||||
.setTicker(pushData.body?.ticker)
|
||||
.setContentTitle(pushData.body?.title)
|
||||
.setContentText(pushData.body?.text)
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
notification.flags = notification.flags or Notification.FLAG_AUTO_CANCEL
|
||||
|
||||
notificationManager.notify(getNotificationTag(context), NOTIFICATION_ID, notification)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 规则:最多三条消息,以旧换新
|
||||
*
|
||||
* @return NotificationTag
|
||||
*/
|
||||
private fun getNotificationTag(context: Context): String {
|
||||
val sp = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val edit = sp.edit()
|
||||
|
||||
val timeTagMap = HashMap<Long, String>()
|
||||
for (tag in notificationTags) {
|
||||
val time = sp.getLong(tag, 0)
|
||||
if (time == 0L) {
|
||||
edit.putLong(tag, System.currentTimeMillis()).apply()
|
||||
return tag
|
||||
} else {
|
||||
timeTagMap[time] = tag
|
||||
}
|
||||
}
|
||||
|
||||
val minTime = Collections.min(timeTagMap.keys)
|
||||
val tag = timeTagMap[minTime]
|
||||
edit.putLong(tag, System.currentTimeMillis()).apply()
|
||||
return if (TextUtils.isEmpty(tag)) notificationTags[0] else tag!!
|
||||
}
|
||||
}
|
||||
132
app/src/main/java/com/gh/common/PushManager.kt
Normal file
132
app/src/main/java/com/gh/common/PushManager.kt
Normal file
@ -0,0 +1,132 @@
|
||||
package com.gh.common
|
||||
|
||||
import android.preference.PreferenceManager
|
||||
import com.gh.base.GHUmengNotificationService
|
||||
import com.gh.common.constant.Config
|
||||
import com.gh.common.exposure.meta.MetaUtil
|
||||
import com.gh.common.util.edit
|
||||
import com.gh.gamecenter.entity.AliasEntity
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager
|
||||
import com.google.gson.Gson
|
||||
import com.halo.assistant.HaloApp
|
||||
import com.lightgame.utils.Utils
|
||||
import com.umeng.commonsdk.UMConfigure
|
||||
import com.umeng.message.IUmengRegisterCallback
|
||||
import com.umeng.message.PushAgent
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.RequestBody
|
||||
import org.android.agoo.huawei.HuaWeiRegister
|
||||
import org.android.agoo.mezu.MeizuRegister
|
||||
import org.android.agoo.xiaomi.MiPushRegistar
|
||||
import org.json.JSONObject
|
||||
|
||||
object PushManager {
|
||||
|
||||
var gson = Gson()
|
||||
var deviceToken: String? = ""
|
||||
var previousAlias: AliasEntity? = null
|
||||
var application = HaloApp.getInstance().application
|
||||
|
||||
const val SP_PUSH_ALIAS = "push_alias"
|
||||
|
||||
@JvmStatic
|
||||
fun init(channel: String) {
|
||||
//初始化友盟推送
|
||||
UMConfigure.init(application,
|
||||
Config.UMENG_APPKEY, channel,
|
||||
UMConfigure.DEVICE_TYPE_PHONE,
|
||||
Config.UMENG_MESSAGE_SECRET)
|
||||
|
||||
// 注册小米通道
|
||||
MiPushRegistar.register(application, Config.MIPUSH_APPID, Config.MIPUSH_APPKEY)
|
||||
// 注册华为通道
|
||||
HuaWeiRegister.register(application)
|
||||
// 注册魅族通道
|
||||
MeizuRegister.register(application, "1001212", "86792462189846c0b8b701e7bb4d11c1")
|
||||
|
||||
//友盟推送
|
||||
val pushAgent = PushAgent.getInstance(application)
|
||||
pushAgent.onAppStart() // 开启App统计
|
||||
|
||||
//注册推送服务,每次调用register方法都会回调该接口
|
||||
pushAgent.register(object : IUmengRegisterCallback {
|
||||
override fun onSuccess(dToken: String) {
|
||||
//注册成功会返回device token
|
||||
deviceToken = dToken
|
||||
Utils.log("deviceToken::$dToken")
|
||||
}
|
||||
|
||||
override fun onFailure(s: String, s1: String) {
|
||||
Utils.log("deviceToken::" + "注册失败")
|
||||
}
|
||||
})
|
||||
|
||||
val aliasInSp = PreferenceManager.getDefaultSharedPreferences(application).getString(SP_PUSH_ALIAS, "")
|
||||
previousAlias = gson.fromJson(aliasInSp, AliasEntity::class.java)
|
||||
|
||||
if (previousAlias == null) {
|
||||
getAndSetAlias()
|
||||
}
|
||||
|
||||
// 友盟推送数据处理
|
||||
// pushAgent.setNotificationClickHandler(new GHUmengNotificationClickHandler());
|
||||
|
||||
// 完全自定义处理(透传)
|
||||
pushAgent.setPushIntentServiceClass(GHUmengNotificationService::class.java)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getAndSetAlias() {
|
||||
val meta = MetaUtil.getMeta()
|
||||
|
||||
val jsonObject = JSONObject()
|
||||
jsonObject.put("device_token", deviceToken)
|
||||
jsonObject.put("imei", meta.imei)
|
||||
jsonObject.put("android_id", meta.android_id)
|
||||
jsonObject.put("model", meta.model)
|
||||
jsonObject.put("manufacturer", meta.manufacturer)
|
||||
jsonObject.put("os", meta.os)
|
||||
jsonObject.put("os_version", meta.android_version)
|
||||
jsonObject.put("mac", meta.mac)
|
||||
|
||||
val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
|
||||
|
||||
RetrofitManager.getInstance(application).api.getAlias(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe(
|
||||
{ setAlias(it) },
|
||||
{ it.printStackTrace() }
|
||||
)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun setAlias(alias: AliasEntity) {
|
||||
val pushAgent = PushAgent.getInstance(application)
|
||||
|
||||
deleteAlias()
|
||||
previousAlias = alias
|
||||
PreferenceManager.getDefaultSharedPreferences(application).edit {
|
||||
putString(SP_PUSH_ALIAS, gson.toJson(previousAlias))
|
||||
}
|
||||
|
||||
pushAgent.setAlias(alias.alias, alias.aliasType) { b, s ->
|
||||
Utils.log("注册别名 $b + $s")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun deleteAlias() {
|
||||
val pushAgent = PushAgent.getInstance(application)
|
||||
|
||||
previousAlias?.let {
|
||||
pushAgent.deleteAlias(it.alias, it.aliasType) { b, s ->
|
||||
Utils.log("删除别名 $b + $s")
|
||||
}
|
||||
}
|
||||
PreferenceManager.getDefaultSharedPreferences(application).edit {
|
||||
putString(SP_PUSH_ALIAS, "")
|
||||
}
|
||||
previousAlias = null
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@ import com.gh.gamecenter.MessageActivity
|
||||
import com.gh.gamecenter.R
|
||||
import java.lang.ref.WeakReference
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
class Notifier private constructor() {
|
||||
|
||||
companion object {
|
||||
@ -117,13 +118,12 @@ class Notifier private constructor() {
|
||||
return notifierView
|
||||
}
|
||||
|
||||
fun setIcon(url: String): Notifier {
|
||||
notifierView?.setIcon(url)
|
||||
|
||||
fun setIcon(url: String?): Notifier {
|
||||
url?.let { notifierView?.setIcon(it) }
|
||||
return this
|
||||
}
|
||||
|
||||
fun setText(text: String): Notifier {
|
||||
fun setText(text: String?): Notifier {
|
||||
notifierView?.setText(text)
|
||||
|
||||
return this
|
||||
|
||||
@ -239,23 +239,7 @@ class NotifierView @JvmOverloads constructor(context: Context, attrs: AttributeS
|
||||
}
|
||||
|
||||
fun shrink() {
|
||||
shrinkAnimator.addListener(object : Animator.AnimatorListener {
|
||||
override fun onAnimationRepeat(animation: Animator?) {
|
||||
// Ignored.
|
||||
}
|
||||
|
||||
override fun onAnimationEnd(animation: Animator?) {
|
||||
hide()
|
||||
}
|
||||
|
||||
override fun onAnimationCancel(animation: Animator?) {
|
||||
// Ignored.
|
||||
}
|
||||
|
||||
override fun onAnimationStart(animation: Animator?) {
|
||||
// Ignored.
|
||||
}
|
||||
})
|
||||
shrinkAnimator.doOnEnd { hide() }
|
||||
shrinkAnimator.start()
|
||||
}
|
||||
|
||||
@ -284,7 +268,7 @@ class NotifierView @JvmOverloads constructor(context: Context, attrs: AttributeS
|
||||
}, 100)
|
||||
}
|
||||
|
||||
fun setText(text: String) {
|
||||
fun setText(text: String?) {
|
||||
if (!TextUtils.isEmpty(text)) {
|
||||
tvText.text = text
|
||||
tvText.measure(0, 0)
|
||||
@ -314,7 +298,6 @@ class NotifierView @JvmOverloads constructor(context: Context, attrs: AttributeS
|
||||
}
|
||||
|
||||
private fun hasSoftKeys(): Boolean {
|
||||
|
||||
if (context !is Activity) return false
|
||||
|
||||
val hasSoftwareKeys: Boolean
|
||||
|
||||
@ -10,7 +10,11 @@ public class ClassUtils {
|
||||
name = "GameDetailActivity";
|
||||
}
|
||||
try {
|
||||
return Class.forName("com.gh.gamecenter." + name);
|
||||
if (!name.contains("com.gh")) {
|
||||
return Class.forName("com.gh.gamecenter." + name);
|
||||
} else {
|
||||
return Class.forName(name);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
50
app/src/main/java/com/gh/common/util/SharedPreferences.kt
Normal file
50
app/src/main/java/com/gh/common/util/SharedPreferences.kt
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.gh.common.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.SharedPreferences
|
||||
|
||||
/**
|
||||
* Allows editing of this preference instance with a call to [apply][SharedPreferences.Editor.apply]
|
||||
* or [commit][SharedPreferences.Editor.commit] to persist the changes.
|
||||
* Default behaviour is [apply][SharedPreferences.Editor.apply].
|
||||
* ```
|
||||
* prefs.edit {
|
||||
* putString("key", value)
|
||||
* }
|
||||
* ```
|
||||
* To [commit][SharedPreferences.Editor.commit] changes:
|
||||
* ```
|
||||
* prefs.edit(commit = true) {
|
||||
* putString("key", value)
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
@SuppressLint("ApplySharedPref")
|
||||
inline fun SharedPreferences.edit(
|
||||
commit: Boolean = false,
|
||||
action: SharedPreferences.Editor.() -> Unit
|
||||
) {
|
||||
val editor = edit()
|
||||
action(editor)
|
||||
if (commit) {
|
||||
editor.commit()
|
||||
} else {
|
||||
editor.apply()
|
||||
}
|
||||
}
|
||||
@ -11,6 +11,11 @@ import com.gh.gamecenter.message.KeFuFragment;
|
||||
|
||||
public class MessageKeFuActivity extends NormalActivity {
|
||||
|
||||
@Override
|
||||
protected Intent provideNormalIntent() {
|
||||
return getTargetIntent(this, MessageKeFuActivity.class, KeFuFragment.class);
|
||||
}
|
||||
|
||||
public static Intent getIntent(Context context) {
|
||||
return getTargetIntent(context, MessageKeFuActivity.class, KeFuFragment.class);
|
||||
}
|
||||
|
||||
@ -101,12 +101,11 @@ public class SkipActivity extends BaseActivity {
|
||||
bundle.putString(KEY_PACKAGENAME, uri.getQueryParameter(KEY_PACKAGENAME));
|
||||
break;
|
||||
case HOST_ANSWER:
|
||||
bundle.putString(KEY_TO, AnswerDetailActivity.TAG);
|
||||
bundle.putString(KEY_TO, AnswerDetailActivity.class.getName());
|
||||
bundle.putString(KEY_ANSWER_ID, id);
|
||||
bundle.putString(KEY_PACKAGENAME, uri.getQueryParameter(KEY_PACKAGENAME));
|
||||
break;
|
||||
case HOST_QUESTION:
|
||||
bundle.putString(KEY_TO, QuestionsDetailActivity.TAG);
|
||||
bundle.putString(KEY_TO, QuestionsDetailActivity.class.getName());
|
||||
bundle.putString(KEY_QUESTIONS_ID, id);
|
||||
break;
|
||||
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package com.gh.gamecenter.entity
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class AliasEntity(var alias: String? = "",
|
||||
@SerializedName("alias_type")
|
||||
var aliasType: String? = "")
|
||||
30
app/src/main/java/com/gh/gamecenter/entity/PushEntity.kt
Normal file
30
app/src/main/java/com/gh/gamecenter/entity/PushEntity.kt
Normal file
@ -0,0 +1,30 @@
|
||||
package com.gh.gamecenter.entity
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class PushEntity(
|
||||
@SerializedName("display_type")
|
||||
var displayType: String = "",
|
||||
var extra: Extra? = null,
|
||||
@SerializedName("msg_id")
|
||||
var msgId: String = "",
|
||||
var body: Body? = null,
|
||||
@SerializedName("random_min")
|
||||
var randomMin: Int = 0) {
|
||||
|
||||
data class Body(
|
||||
@SerializedName("after_open")
|
||||
var afterOpen: String = "",
|
||||
@SerializedName("play_lights")
|
||||
var playLights: String = "",
|
||||
var ticker: String = "",
|
||||
@SerializedName("play_vibrate")
|
||||
var playVibrate: String = "",
|
||||
var custom: String = "",
|
||||
var text: String = "",
|
||||
var title: String = "",
|
||||
@SerializedName("paly_sound")
|
||||
var playSound: String = "")
|
||||
|
||||
data class Extra(var data: String? = "")
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.gh.gamecenter.entity
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
|
||||
data class PushNotificationEntity(var condition: Condition? = null,
|
||||
var link: Link? = null) {
|
||||
|
||||
@Parcelize
|
||||
data class Link(var type: String = "", var target: String = "") : Parcelable
|
||||
|
||||
data class Condition(
|
||||
@SerializedName("package")
|
||||
var packageName: String = "",
|
||||
var ghzs: Ghzs)
|
||||
|
||||
data class Ghzs(var channel: String = "", var version: String = "")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -8,9 +8,9 @@ import kotlinx.android.parcel.Parcelize
|
||||
* Created by khy on 2016/11/8.
|
||||
*/
|
||||
@Parcelize
|
||||
class UserEntity(var icon: String? = null,
|
||||
var name: String? = null,
|
||||
class UserEntity(var icon: String? = "",
|
||||
var name: String? = "",
|
||||
@SerializedName("_id")
|
||||
// 用于parent的user
|
||||
var id: String? = null,
|
||||
var id: String? = "",
|
||||
var auth: Auth? = null) : Parcelable
|
||||
|
||||
@ -4,11 +4,8 @@ import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.gh.base.CurrentActivityHolder;
|
||||
import com.gh.base.fragment.BaseFragment_ViewPager_Checkable;
|
||||
import com.gh.common.notifier.Notifier;
|
||||
import com.gh.common.util.CheckLoginUtils;
|
||||
import com.gh.common.util.DataUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
@ -130,18 +127,6 @@ public class MainWrapperFragment extends BaseFragment_ViewPager_Checkable {
|
||||
break;
|
||||
case INDEX_PERSONAL:
|
||||
DataUtils.onMtaEvent(getContext(), "顶级页面", "BottomBar", "我的光环");
|
||||
|
||||
if (Notifier.isActivityValid(CurrentActivityHolder.getCurrentActivity())) {
|
||||
Notifier.create(CurrentActivityHolder.getCurrentActivity())
|
||||
.setText("着魔的村民回答了你关注的问题")
|
||||
.setDuration(5000)
|
||||
.setIcon("http://gitlab.ghzhushou.com/uploads/user/avatar/46/avatar.png")
|
||||
.setOnClickListener(v -> {
|
||||
Toast.makeText(getContext(), "翻云破浪", Toast.LENGTH_SHORT).show();
|
||||
Notifier.hide();
|
||||
})
|
||||
.show(true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
package com.gh.gamecenter.message;
|
||||
|
||||
import android.arch.lifecycle.MediatorLiveData;
|
||||
|
||||
import com.gh.gamecenter.BuildConfig;
|
||||
import com.gh.gamecenter.entity.MessageUnreadEntity;
|
||||
import com.gh.gamecenter.manager.UserManager;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.service.ApiService;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import retrofit2.HttpException;
|
||||
|
||||
/**
|
||||
* Created by khy on 29/03/18.
|
||||
*/
|
||||
|
||||
public class MessageUnreadRepository {
|
||||
|
||||
private ApiService api;
|
||||
|
||||
private static MessageUnreadRepository mInstance;
|
||||
|
||||
private final MediatorLiveData<MessageUnreadEntity> mMediatorLiveData = new MediatorLiveData<>();
|
||||
|
||||
private MessageUnreadEntity mCacheUnreadData;
|
||||
|
||||
private boolean isLoading;
|
||||
|
||||
public static MessageUnreadRepository getInstance(ApiService api) {
|
||||
if (mInstance == null) {
|
||||
synchronized (MessageUnreadRepository.class) {
|
||||
if (mInstance == null) {
|
||||
mInstance = new MessageUnreadRepository(api);
|
||||
}
|
||||
}
|
||||
}
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
public MessageUnreadRepository(ApiService api) {
|
||||
this.api = api;
|
||||
loadMessageUnreadData();
|
||||
}
|
||||
|
||||
void loadMessageUnreadData() {
|
||||
|
||||
if (isLoading && UserManager.getInstance().getToken() == null) return;
|
||||
isLoading = true;
|
||||
api
|
||||
.getMessageUnread(BuildConfig.API_HOST + "messages:unread_count")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<MessageUnreadEntity>() {
|
||||
@Override
|
||||
public void onResponse(MessageUnreadEntity response) {
|
||||
isLoading = false;
|
||||
mCacheUnreadData = response;
|
||||
mMediatorLiveData.postValue(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
isLoading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
MediatorLiveData<MessageUnreadEntity> getLiveData() {
|
||||
if (mCacheUnreadData == null) loadMessageUnreadData();
|
||||
return mMediatorLiveData;
|
||||
}
|
||||
|
||||
MessageUnreadEntity getCacheUnreadData() {
|
||||
if (mCacheUnreadData == null) loadMessageUnreadData();
|
||||
return mCacheUnreadData;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package com.gh.gamecenter.message
|
||||
|
||||
import android.arch.lifecycle.MediatorLiveData
|
||||
import com.gh.gamecenter.BuildConfig
|
||||
import com.gh.gamecenter.entity.MessageUnreadEntity
|
||||
import com.gh.gamecenter.manager.UserManager
|
||||
import com.gh.gamecenter.retrofit.Response
|
||||
import com.gh.gamecenter.retrofit.service.ApiService
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import retrofit2.HttpException
|
||||
|
||||
object MessageUnreadRepository {
|
||||
|
||||
@JvmStatic
|
||||
fun getInstance(api: ApiService): MessageUnreadRepository {
|
||||
if (this.api == null) {
|
||||
this.api = api
|
||||
loadMessageUnreadData()
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
private var api: ApiService? = null
|
||||
|
||||
val messageLiveData = MediatorLiveData<MessageUnreadEntity>()
|
||||
|
||||
private var mCacheUnreadData: MessageUnreadEntity? = null
|
||||
|
||||
private var isLoading: Boolean = false
|
||||
|
||||
val liveData: MediatorLiveData<MessageUnreadEntity>
|
||||
get() {
|
||||
if (mCacheUnreadData == null) loadMessageUnreadData()
|
||||
return messageLiveData
|
||||
}
|
||||
|
||||
val cacheUnreadData: MessageUnreadEntity?
|
||||
get() {
|
||||
if (mCacheUnreadData == null) loadMessageUnreadData()
|
||||
return mCacheUnreadData
|
||||
}
|
||||
|
||||
init {
|
||||
loadMessageUnreadData()
|
||||
}
|
||||
|
||||
fun loadMessageUnreadData() {
|
||||
|
||||
if (isLoading && UserManager.getInstance().token == null) return
|
||||
isLoading = true
|
||||
|
||||
api?.run {
|
||||
getMessageUnread(BuildConfig.API_HOST + "messages:unread_count")
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(object : Response<MessageUnreadEntity>() {
|
||||
override fun onResponse(response: MessageUnreadEntity?) {
|
||||
isLoading = false
|
||||
mCacheUnreadData = response
|
||||
messageLiveData.postValue(response)
|
||||
}
|
||||
|
||||
override fun onFailure(e: HttpException?) {
|
||||
isLoading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -28,4 +28,9 @@ public class AnswerDetailActivity extends NormalActivity {
|
||||
bundle.putBoolean(EntranceUtils.KEY_SHOW_ANSWER_COMMENT, isOpenCommentManager);
|
||||
return getTargetIntent(context, AnswerDetailActivity.class, AnswerDetailFragment.class, bundle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent provideNormalIntent() {
|
||||
return getTargetIntent(this, AnswerDetailActivity.class, AnswerDetailFragment.class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,4 +29,9 @@ public class QuestionsDetailActivity extends NormalActivity {
|
||||
bundle.putBoolean(EntranceUtils.KEY_CHECK_QUESTION_CONCERN, isCheckConcern);
|
||||
return getTargetIntent(context, QuestionsDetailActivity.class, QuestionsDetailFragment.class, bundle);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Intent provideNormalIntent() {
|
||||
return getTargetIntent(this, QuestionsDetailActivity.class, QuestionsDetailFragment.class);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package com.gh.gamecenter.receiver
|
||||
|
||||
import com.umeng.message.meizu.UmengMeizuPushReceiver
|
||||
|
||||
class MeizuPushReceiver : UmengMeizuPushReceiver()
|
||||
@ -1,16 +0,0 @@
|
||||
package com.gh.gamecenter.receiver;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
|
||||
public class UmengMessageReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String data = intent.getStringExtra(EntranceUtils.KEY_DATA);
|
||||
// TODO: 20/07/18 跳转处理
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
package com.gh.gamecenter.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import com.gh.common.util.EntranceUtils
|
||||
import com.gh.gamecenter.*
|
||||
import com.gh.gamecenter.entity.PushNotificationEntity
|
||||
import com.gh.gamecenter.qa.answer.detail.AnswerDetailActivity
|
||||
import com.gh.gamecenter.qa.questions.detail.QuestionsDetailActivity
|
||||
|
||||
class UmengMessageReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val link: PushNotificationEntity.Link? = intent.getParcelableExtra(EntranceUtils.KEY_DATA)
|
||||
|
||||
if (link == null) {
|
||||
jumpToHaloOfficialNotificationPage(context)
|
||||
} else {
|
||||
jumpToSpecificPage(context, link)
|
||||
}
|
||||
}
|
||||
|
||||
private fun jumpToHaloOfficialNotificationPage(context: Context) {
|
||||
val bundle = Bundle()
|
||||
bundle.putString(EntranceUtils.KEY_ENTRANCE, EntranceUtils.ENTRANCE_UMENG)
|
||||
bundle.putString(EntranceUtils.KEY_TO, MessageKeFuActivity::class.java.simpleName)
|
||||
EntranceUtils.jumpActivity(context, bundle)
|
||||
}
|
||||
|
||||
private fun jumpToSpecificPage(context: Context, link: PushNotificationEntity.Link) {
|
||||
val bundle = Bundle()
|
||||
bundle.putString(EntranceUtils.KEY_ENTRANCE, EntranceUtils.ENTRANCE_UMENG)
|
||||
when (link.type) {
|
||||
EntranceUtils.HOST_ARTICLE -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, NewsDetailActivity::class.java.simpleName)
|
||||
bundle.putString(EntranceUtils.KEY_NEWSID, link.target)
|
||||
}
|
||||
EntranceUtils.HOST_GAME -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, GameDetailActivity::class.java.simpleName)
|
||||
bundle.putString(EntranceUtils.KEY_GAMEID, link.target)
|
||||
}
|
||||
EntranceUtils.HOST_COLUMN -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, SubjectActivity::class.java.simpleName)
|
||||
bundle.putString(EntranceUtils.KEY_ID, link.target)
|
||||
}
|
||||
EntranceUtils.HOST_QUESTION -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, QuestionsDetailActivity::class.java.name)
|
||||
bundle.putString(EntranceUtils.KEY_QUESTIONS_ID, link.target)
|
||||
}
|
||||
EntranceUtils.HOST_ANSWER -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, AnswerDetailActivity::class.java.name)
|
||||
bundle.putString(EntranceUtils.KEY_ANSWER_ID, link.target)
|
||||
}
|
||||
EntranceUtils.HOST_WEB -> {
|
||||
bundle.putString(EntranceUtils.KEY_TO, WebActivity::class.java.simpleName)
|
||||
bundle.putString(EntranceUtils.KEY_URL, link.target)
|
||||
}
|
||||
}
|
||||
EntranceUtils.jumpActivity(context, bundle)
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.gh.gamecenter.retrofit
|
||||
|
||||
import android.content.Context
|
||||
import android.text.TextUtils
|
||||
import com.gh.common.PushManager
|
||||
import com.gh.gamecenter.manager.UserManager
|
||||
import com.lightgame.config.CommonDebug
|
||||
import okhttp3.Interceptor
|
||||
@ -9,7 +10,6 @@ import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
import java.io.IOException
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 重试connection,需要add as interceptor而不是network interceptor,可以多次执行proceed
|
||||
@ -57,10 +57,16 @@ class OkHttpRetryInterceptor internal constructor(context: Context) : Intercepto
|
||||
newBuilder.header("TOKEN", UserManager.getInstance().token)
|
||||
request = newBuilder.build()
|
||||
response = doRequest(chain, request)
|
||||
|
||||
// 更新 Access Token 后更新推送别名
|
||||
PushManager.getAndSetAlias()
|
||||
}
|
||||
|
||||
override fun onLoginFailure() {
|
||||
tryCount = 4 // 只要token刷新异常直接主动退出登录
|
||||
|
||||
// 删除推送别名
|
||||
PushManager.deleteAlias()
|
||||
}
|
||||
})
|
||||
} else { // 网络错误处理
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.gh.gamecenter.retrofit.service;
|
||||
|
||||
import com.gh.gamecenter.entity.AliasEntity;
|
||||
import com.gh.gamecenter.entity.AppEntity;
|
||||
import com.gh.gamecenter.entity.CategoryEntity;
|
||||
import com.gh.gamecenter.entity.CommentEntity;
|
||||
@ -876,7 +877,7 @@ public interface ApiService {
|
||||
@GET("communities/{community_id}/columns/{column_id}/answers")
|
||||
Observable<List<AnswerEntity>> getAskSubjectColumnAnswers(@Path("community_id") String communityId
|
||||
, @Path("column_id") String columnId
|
||||
, @Query("page") int page, @Query("page_size") int pageSize);
|
||||
, @Query("page") int page);
|
||||
|
||||
// /**
|
||||
// * 获取默认社区
|
||||
@ -1161,5 +1162,12 @@ public interface ApiService {
|
||||
*/
|
||||
@POST("games/{game_id}/has-community")
|
||||
Observable<ResponseBody> checkHasCommunity(@Path("game_id") String gameId);
|
||||
|
||||
|
||||
/**
|
||||
* 获取推送别名
|
||||
*/
|
||||
@Headers({"Content-Type: application/json", "Accept: application/json"})
|
||||
@POST("umeng/alias")
|
||||
Observable<AliasEntity> getAlias(@Body RequestBody body);
|
||||
|
||||
}
|
||||
@ -8,27 +8,17 @@ import android.util.Log;
|
||||
|
||||
import com.facebook.drawee.backends.pipeline.Fresco;
|
||||
import com.gh.base.GHActivityLifecycleCallbacksImpl;
|
||||
import com.gh.base.GHUmengNotificationService;
|
||||
import com.gh.common.PushManager;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.exposure.ExposureManager;
|
||||
import com.gh.common.util.DataUtils;
|
||||
import com.gh.common.util.QiYuImageLoader;
|
||||
import com.gh.common.util.StringUtils;
|
||||
import com.gh.common.util.TokenUtils;
|
||||
import com.gh.gamecenter.Injection;
|
||||
import com.gh.gamecenter.MainActivity;
|
||||
import com.leon.channel.helper.ChannelReaderUtil;
|
||||
import com.lightgame.utils.Utils;
|
||||
import com.qiyukf.unicorn.api.StatusBarNotificationConfig;
|
||||
import com.qiyukf.unicorn.api.Unicorn;
|
||||
import com.qiyukf.unicorn.api.YSFOptions;
|
||||
import com.umeng.commonsdk.UMConfigure;
|
||||
import com.umeng.message.IUmengRegisterCallback;
|
||||
import com.umeng.message.PushAgent;
|
||||
import com.umeng.message.UTrack;
|
||||
|
||||
import org.android.agoo.huawei.HuaWeiRegister;
|
||||
import org.android.agoo.xiaomi.MiPushRegistar;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
@ -101,51 +91,7 @@ public class HaloApp extends TinkerAppLike {
|
||||
ExposureManager.INSTANCE.init(getApplication());
|
||||
|
||||
try {
|
||||
//初始化友盟推送
|
||||
UMConfigure.init(getApplication(),
|
||||
Config.UMENG_APPKEY, mChannel,
|
||||
UMConfigure.DEVICE_TYPE_PHONE,
|
||||
Config.UMENG_MESSAGE_SECRET);
|
||||
|
||||
// 注册小米推送
|
||||
MiPushRegistar.register(getApplication(), Config.MIPUSH_APPID, Config.MIPUSH_APPKEY);
|
||||
// 注册华为推送
|
||||
HuaWeiRegister.register(getApplication());
|
||||
|
||||
//友盟推送
|
||||
PushAgent pushAgent = PushAgent.getInstance(getApplication());
|
||||
pushAgent.onAppStart(); // 开启App统计
|
||||
|
||||
//注册推送服务,每次调用register方法都会回调该接口
|
||||
pushAgent.register(new IUmengRegisterCallback() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(String deviceToken) {
|
||||
//注册成功会返回device token
|
||||
Utils.log("deviceToken::" + deviceToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(String s, String s1) {
|
||||
Utils.log("deviceToken::" + "注册失败");
|
||||
}
|
||||
});
|
||||
|
||||
//设置别名 用户id和device_token的一对多的映射关系
|
||||
pushAgent.addAlias(TokenUtils.getDeviceId(getApplication()),
|
||||
"GHDID", new UTrack.ICallBack() {
|
||||
@Override
|
||||
public void onMessage(boolean b, String s) {
|
||||
Utils.log(StringUtils.buildString("ExclusiveAlias::", String.valueOf(b), "==", s));
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
// 友盟推送数据处理
|
||||
// pushAgent.setNotificationClickHandler(new GHUmengNotificationClickHandler());
|
||||
|
||||
// 完全自定义处理(透传)
|
||||
pushAgent.setPushIntentServiceClass(GHUmengNotificationService.class);
|
||||
PushManager.init(mChannel);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ import android.widget.RadioGroup;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.common.PushManager;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.util.DataUtils;
|
||||
import com.gh.common.util.DialogUtils;
|
||||
@ -278,6 +279,7 @@ public class SettingsFragment extends NormalFragment {
|
||||
loadingDialog = DialogUtils.showWaitDialog(getContext(), "退出登录中...");
|
||||
LoginUtils.logout(getContext(), () -> {
|
||||
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGOUT_TAG));
|
||||
PushManager.deleteAlias();
|
||||
if (loadingDialog != null) loadingDialog.dismiss();
|
||||
if (getActivity() != null) getActivity().finish();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user