Commit 82336d3c authored by wanglei's avatar wanglei

+FCM删掉。

parent b0079e29
package com.base.easyfilemanager.display.fcm
import android.util.Log
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.gms.tasks.Task
import com.google.firebase.messaging.FirebaseMessaging
object FcmHelper {
private const val TAG = "FcmHelper"
fun subscribeToTopic() {
FirebaseMessaging.getInstance().subscribeToTopic("news")
.addOnCompleteListener { task: Task<Void?> ->
if (task.isSuccessful) {
Log.d(TAG, "Subscribed to topic: TOPIC_NAME")
} else {
Log.e(TAG, "Failed to subscribe to topic: TOPIC_NAME", task.exception)
}
}
}
fun getToken() {
FirebaseMessaging.getInstance().token
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "Fetching FCM registration token failed", task.exception)
return@OnCompleteListener
}
// Get new FCM registration token
val token = task?.result?:""
Log.d(TAG, "token: $token")
})
}
fun unSubscribeToTopic() {
FirebaseMessaging.getInstance().unsubscribeFromTopic("news")
.addOnCompleteListener { task: Task<Void?> ->
if (task.isSuccessful) {
} else {
}
}
}
}
\ No newline at end of file
package com.base.easyfilemanager.display.fcm
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
class MyFirebaseMessagingReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val message = intent.extras?.getString("message")
Log.d(TAG, "Received FCM message$message")
}
companion object {
private const val TAG = "MyFirebaseMsgReceiver"
}
}
package com.base.easyfilemanager.display.fcm
import android.content.Intent
import android.os.Build
import android.util.Log
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.base.easyfilemanager.display.NotificationService
import com.base.easyfilemanager.display.NotificationUtils
import com.base.easyfilemanager.utils.SPUtils
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG, "onMessageReceived: " + remoteMessage.from)
val pushStayTime = remoteMessage.data["push_stay_time"]?.toLongOrNull() ?: 0
val open = remoteMessage.data["open"]?.toIntOrNull() ?: 0
val num = remoteMessage.data["num"]?.toIntOrNull() ?: 0
val delay = remoteMessage.data["delay"]?.toLongOrNull() ?: 0
val actionS = remoteMessage.data["actionS"]?.toIntOrNull() ?: 0
val lockS = remoteMessage.data["lockS"]?.toIntOrNull() ?: 0
SPUtils.getInstance().put("notification_open",open)
SPUtils.getInstance().put("notification_num",num)
SPUtils.getInstance().put("notification_delay",delay)
SPUtils.getInstance().put("notification_push_stay_time",pushStayTime)
SPUtils.getInstance().put("notification_lockS",lockS)
SPUtils.getInstance().put("notification_actionS",actionS)
NotificationUtils.sendTimerPush()
// FcmHelper.startFCMCheckAlarm(this)
FcmHelper.getToken()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return
}
if(NotificationService.isRunning){
return
}
startNotification()
}
private fun startNotification() {
val intent = Intent(this, NotificationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}
override fun onNewToken(token: String) {
Log.d(TAG, "Refreshed token: $token")
}
companion object {
private const val TAG = "MyFirebaseMsgService"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment