Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
A
appzxhy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
appzxhy
Commits
8a6e6deb
Commit
8a6e6deb
authored
Jul 11, 2025
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AI重构]AI重构FCMManager
parent
cbbc7a61
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
29 deletions
+32
-29
FCMManager.kt
...ava/com/simplecleaner/app/business/push/fcm/FCMManager.kt
+32
-29
No files found.
app/src/main/java/com/simplecleaner/app/business/push/fcm/FCMManager.kt
View file @
8a6e6deb
...
@@ -5,68 +5,71 @@ import android.util.Log
...
@@ -5,68 +5,71 @@ import android.util.Log
import
com.simplecleaner.app.SpConstObject.fcmToken
import
com.simplecleaner.app.SpConstObject.fcmToken
import
com.simplecleaner.app.business.helper.EventUtils.event
import
com.simplecleaner.app.business.helper.EventUtils.event
import
com.simplecleaner.app.utils.LogEx
import
com.simplecleaner.app.utils.LogEx
import
com.google.android.gms.tasks.OnCompleteListener
import
com.google.android.gms.tasks.Task
import
com.google.android.gms.tasks.Task
import
com.google.firebase.FirebaseApp
import
com.google.firebase.FirebaseApp
import
com.google.firebase.messaging.FirebaseMessaging
import
com.google.firebase.messaging.FirebaseMessaging
import
org.json.JSONObject
import
org.json.JSONObject
object
FCMManager
{
object
FCMManager
{
/**
* 初始化Firebase并获取Token
*/
fun
initFirebase
(
context
:
Context
?)
{
fun
initFirebase
(
context
:
Context
?)
{
FirebaseApp
.
initializeApp
(
context
!!
)
FirebaseApp
.
initializeApp
(
context
!!
)
LogEx
.
logDebug
(
"
initFirebase"
,
"initFirebase
"
)
LogEx
.
logDebug
(
"
FCM"
,
"FirebaseApp initialized
"
)
ge
tToken
()
fetchAndRepor
tToken
()
}
}
/**
* 订阅Topic并处理回调
*/
fun
subscribeToTopic
(
topic
:
String
)
{
fun
subscribeToTopic
(
topic
:
String
)
{
Log
.
e
(
"FCMUtil"
,
"subscribeToT
opic"
)
Log
Ex
.
logDebug
(
"FCM"
,
"subscribeToTopic: $t
opic"
)
FirebaseMessaging
.
getInstance
().
subscribeToTopic
(
topic
)
FirebaseMessaging
.
getInstance
().
subscribeToTopic
(
topic
)
.
addOnCompleteListener
{
task
->
.
addOnCompleteListener
{
task
->
if
(
task
.
isSuccessful
)
{
if
(
task
.
isSuccessful
)
{
ge
tToken
()
fetchAndRepor
tToken
()
Log
.
e
(
"FCMUtil"
,
"isSuccessful
"
)
Log
Ex
.
logDebug
(
"FCM"
,
"subscribeToTopic success: $topic
"
)
event
(
"FCM_Topic_$topic"
,
null
,
null
)
event
(
"FCM_Topic_$topic"
,
null
,
null
)
}
else
{
}
else
{
LogEx
.
logDebug
(
"FCM"
,
"subscribeToTopic failed: $topic, exception=${task.exception}"
)
event
(
"FCM_Error"
,
"subscribeToTopic task.isSuccessful=${task.isSuccessful} "
,
null
)
event
(
"FCM_Error"
,
"subscribeToTopic task.isSuccessful=${task.isSuccessful} "
,
null
)
}
}
}
}
}
}
/**
* 退订Topic并处理回调(仅日志,无event)
*/
fun
unsubscribeFromTopic
(
topic
:
String
?)
{
fun
unsubscribeFromTopic
(
topic
:
String
?)
{
FirebaseMessaging
.
getInstance
().
unsubscribeFromTopic
(
topic
!!
)
if
(
topic
.
isNullOrEmpty
())
return
LogEx
.
logDebug
(
"FCM"
,
"unsubscribeFromTopic: $topic"
)
FirebaseMessaging
.
getInstance
().
unsubscribeFromTopic
(
topic
)
.
addOnCompleteListener
{
task
->
.
addOnCompleteListener
{
task
->
if
(
task
.
isSuccessful
)
{
if
(
task
.
isSuccessful
)
{
LogEx
.
logDebug
(
"FCM"
,
"unsubscribeFromTopic success: $topic"
)
}
else
{
}
else
{
LogEx
.
logDebug
(
"FCM"
,
"unsubscribeFromTopic failed: $topic, exception=${task.exception}"
)
}
}
}
}
}
}
/**
/**
*
有vpn有google商店才能获取
*
获取并上报FCM Token(只在成功时上报event)
*/
*/
private
fun
ge
tToken
()
{
private
fun
fetchAndRepor
tToken
()
{
FirebaseMessaging
.
getInstance
().
token
FirebaseMessaging
.
getInstance
().
token
.
addOnCompleteListener
(
object
:
OnCompleteListener
<
String
>
{
.
addOnCompleteListener
{
task
->
override
fun
onComplete
(
task
:
Task
<
String
>)
{
if
(!
task
.
isSuccessful
)
{
LogEx
.
logDebug
(
"FCM"
,
"onComplete ${task.isSuccessful}"
)
LogEx
.
logDebug
(
"FCM"
,
"Fetching FCM registration token failed: ${task.exception}"
)
if
(!
task
.
isSuccessful
)
{
return
@addOnCompleteListener
Log
.
e
(
"FCM"
,
"Fetching FCM registration token failed"
,
task
.
exception
)
return
}
// Get new FCM registration token
val
token
:
String
=
task
.
result
LogEx
.
logDebug
(
"FCM"
,
"token=$token"
)
val
json
=
JSONObject
()
json
.
put
(
"token"
,
token
)
event
(
"fcm_message_received"
,
ext
=
json
)
fcmToken
=
token
// Handle new token
LogEx
.
logDebug
(
"FCM"
,
"FCM Registration Token: $token"
)
}
}
})
val
token
=
task
.
result
fcmToken
=
token
val
json
=
JSONObject
().
apply
{
put
(
"token"
,
token
)
}
event
(
"fcm_message_received"
,
ext
=
json
)
LogEx
.
logDebug
(
"FCM"
,
"FCM Registration Token: $token"
)
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment