|
@@ -1,94 +1,68 @@
|
|
|
-package com.sikey.veryfit.component.receiver;
|
|
|
+package com.sikey.veryfit.component.receiver
|
|
|
|
|
|
-import android.util.Log;
|
|
|
-
|
|
|
-import com.google.firebase.messaging.FirebaseMessagingService;
|
|
|
-import com.google.firebase.messaging.RemoteMessage;
|
|
|
-import com.sikey.veryfit.constant.Constant;
|
|
|
-import com.sikey.veryfit.utils.SharedPreferenceUtil;
|
|
|
-
|
|
|
-import java.util.Map;
|
|
|
+import android.util.Log
|
|
|
+import com.google.firebase.messaging.FirebaseMessagingService
|
|
|
+import com.google.firebase.messaging.RemoteMessage
|
|
|
+import com.sikey.veryfit.constant.Constant
|
|
|
+import com.sikey.veryfit.utils.SharedPreferenceUtil
|
|
|
|
|
|
/**
|
|
|
* Created by yolo.huang on 2018/11/27.
|
|
|
*/
|
|
|
-
|
|
|
-public class FCMMessagingService extends FirebaseMessagingService {
|
|
|
-
|
|
|
- private static final String TAG = "FCMService";
|
|
|
-
|
|
|
- public static final String VOICE_NUM_KEY="Voice_Num_Key";
|
|
|
- public static final String SP_PREF_NAME="sp_jpush_name";
|
|
|
-
|
|
|
- private static OnReceivedNewVoiceListener mListener;
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onMessageReceived(RemoteMessage remoteMessage) {
|
|
|
- super.onMessageReceived(remoteMessage);
|
|
|
- Log.d(TAG, "Message received: ");
|
|
|
+class FCMMessagingService : FirebaseMessagingService() {
|
|
|
+ override fun onMessageReceived(remoteMessage: RemoteMessage) {
|
|
|
+ super.onMessageReceived(remoteMessage)
|
|
|
+ Log.d(TAG, "Message received: ${remoteMessage.data}")
|
|
|
// Check if message contains a data payload.
|
|
|
- if (remoteMessage.getData().size() > 0) {
|
|
|
- Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
- receivingNotification(remoteMessage.getData());
|
|
|
-
|
|
|
+ if (remoteMessage.data.isNotEmpty()) {
|
|
|
+ Log.d(TAG, "Message data payload: " + remoteMessage.data)
|
|
|
+ receivingNotification(remoteMessage.data)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void onDeletedMessages() {
|
|
|
- super.onDeletedMessages();
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void onMessageSent(String s) {
|
|
|
- super.onMessageSent(s);
|
|
|
- }
|
|
|
+ private fun receivingNotification(map: Map<String, String>) {
|
|
|
+ val type = map["type"]!!.toInt()
|
|
|
+ when (type) {
|
|
|
+ Constant.PUSH_GROUP_VOICE, Constant.PUSH_GROUP_EMOJI, Constant.PUSH_PRIVATE_VOICE, Constant.PUSH_PRIVATE_EMOJI -> {
|
|
|
+ val childId = map["childid"]!!.toInt()
|
|
|
+ handleReceivingNewVoice(childId)
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void onSendError(String s, Exception e) {
|
|
|
- super.onSendError(s, e);
|
|
|
+ else -> {}
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private fun handleReceivingNewVoice(childId: Int) {
|
|
|
+ val key = String.format("%d%s", childId, VOICE_NUM_KEY)
|
|
|
+ val num = SharedPreferenceUtil.getIntValueFromSP(SP_PREF_NAME, key, 0)
|
|
|
+ SharedPreferenceUtil.setIntDataIntoSP(SP_PREF_NAME, key, num + 1)
|
|
|
|
|
|
- public static void setOnReceivedNewVoiceListener(OnReceivedNewVoiceListener listener)
|
|
|
- {
|
|
|
- mListener = listener;
|
|
|
+ if (mListener != null) {
|
|
|
+ mListener!!.onReceivedNewVoice()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private void receivingNotification(Map<String,String> map){
|
|
|
- int type = Integer.parseInt(map.get("type"));
|
|
|
- switch (type){
|
|
|
- case Constant.PUSH_GROUP_VOICE:
|
|
|
- case Constant.PUSH_GROUP_EMOJI:
|
|
|
- case Constant.PUSH_PRIVATE_VOICE:
|
|
|
- case Constant.PUSH_PRIVATE_EMOJI:
|
|
|
- int childId = Integer.parseInt(map.get("childid"));
|
|
|
- handleReceivingNewVoice(childId);
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
+ interface OnReceivedNewVoiceListener {
|
|
|
+ fun onReceivedNewVoice()
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ override fun onNewToken(s: String) {
|
|
|
+ super.onNewToken(s)
|
|
|
+ Log.d(TAG, "Refreshed token: $s")
|
|
|
+ SharedPreferenceUtil.setStringDataIntoSP(Constant.SP_TOKEN, Constant.TOKEN, s)
|
|
|
}
|
|
|
|
|
|
- private void handleReceivingNewVoice(int childId) {
|
|
|
- String key = String.format("%d%s", childId, VOICE_NUM_KEY);
|
|
|
- int num = SharedPreferenceUtil.getIntValueFromSP(SP_PREF_NAME, key, 0);
|
|
|
- SharedPreferenceUtil.setIntDataIntoSP(SP_PREF_NAME, key, num+1);
|
|
|
+ companion object {
|
|
|
+ const val TAG: String = "FCMService"
|
|
|
|
|
|
- if(mListener != null) {
|
|
|
- mListener.onReceivedNewVoice();
|
|
|
- }
|
|
|
- }
|
|
|
+ const val VOICE_NUM_KEY: String = "Voice_Num_Key"
|
|
|
+ const val SP_PREF_NAME: String = "sp_jpush_name"
|
|
|
|
|
|
- public interface OnReceivedNewVoiceListener {
|
|
|
- void onReceivedNewVoice();
|
|
|
- }
|
|
|
+ private var mListener: OnReceivedNewVoiceListener? = null
|
|
|
|
|
|
- @Override
|
|
|
- public void onNewToken(String s) {
|
|
|
- super.onNewToken(s);
|
|
|
- Log.d(TAG, "Refreshed token: " + s);
|
|
|
- SharedPreferenceUtil.setStringDataIntoSP(Constant.SP_TOKEN, Constant.TOKEN,s);
|
|
|
+ fun setOnReceivedNewVoiceListener(listener: OnReceivedNewVoiceListener?) {
|
|
|
+ mListener = listener
|
|
|
+ }
|
|
|
}
|
|
|
}
|