-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More FCM infra: wire up token registration, analytics, toasts, primer…
… dialog.
- Loading branch information
1 parent
3e9bfab
commit d8597f0
Showing
16 changed files
with
223 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
v4/integration/fcm/src/main/java/exchange/dydx/trading/integration/fcm/ActivityDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package exchange.dydx.trading.integration.fcm | ||
|
||
import android.app.Activity | ||
|
||
/** | ||
* General activity delegate that allows accessing activity from Hilt components higher than | ||
* ActivityComponent. | ||
* | ||
* Feel free to move this somewhere more general if you need to use it for other stuff. | ||
*/ | ||
interface ActivityDelegate { | ||
var activity: Activity? | ||
|
||
fun takeActivity(activity: Activity) { | ||
this.activity = activity | ||
} | ||
|
||
fun dropActivity(activity: Activity) { | ||
// outgoing activity can be destroyed after incoming activity has already been created | ||
if (activity == this.activity) { | ||
this.activity = null | ||
} | ||
} | ||
} |
59 changes: 43 additions & 16 deletions
59
v4/integration/fcm/src/main/java/exchange/dydx/trading/integration/fcm/DydxFCMService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,73 @@ | ||
package exchange.dydx.trading.integration.fcm | ||
|
||
import android.Manifest | ||
import android.app.Application | ||
import android.content.pm.PackageManager | ||
import android.util.Log | ||
import androidx.core.content.ContextCompat | ||
import com.google.android.gms.tasks.OnCompleteListener | ||
import com.google.firebase.messaging.FirebaseMessaging | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
import com.google.firebase.messaging.RemoteMessage | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import exchange.dydx.dydxstatemanager.AbacusStateManager | ||
import exchange.dydx.dydxstatemanager.protocolImplementations.AbacusLocalizerImp | ||
import exchange.dydx.platformui.components.container.PlatformInfo | ||
import javax.inject.Inject | ||
|
||
@AndroidEntryPoint | ||
class DydxFCMService : FirebaseMessagingService() { | ||
|
||
@Inject | ||
internal lateinit var fcmRegistrar: FCMRegistrar | ||
@Inject internal lateinit var fcmRegistrar: FCMRegistrar | ||
|
||
@Inject internal lateinit var platformInfo: PlatformInfo | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
fcmRegistrar.registerToken() | ||
} | ||
|
||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
fcmRegistrar.registerToken(token) | ||
} | ||
|
||
override fun onMessageReceived(message: RemoteMessage) { | ||
super.onMessageReceived(message) | ||
message.notification?.let { notification -> | ||
platformInfo.show( | ||
title = notification.title, | ||
message = notification.body, | ||
) | ||
} | ||
} | ||
} | ||
|
||
class FCMRegistrar @Inject constructor( | ||
private val application: Application, | ||
private val abacusStateManager: AbacusStateManager, | ||
private val abacusLocalizerImp: AbacusLocalizerImp, | ||
) { | ||
|
||
fun registerToken() { | ||
FirebaseMessaging.getInstance().token.addOnCompleteListener( | ||
OnCompleteListener { task -> | ||
if (!task.isSuccessful) { | ||
return@OnCompleteListener | ||
} | ||
|
||
// Get new FCM registration token | ||
val token = task.result | ||
fcmRegistrar.registerToken(token) | ||
registerToken(task.result) | ||
}, | ||
) | ||
} | ||
|
||
override fun onNewToken(token: String) { | ||
super.onNewToken(token) | ||
fcmRegistrar.registerToken(token) | ||
} | ||
} | ||
|
||
internal class FCMRegistrar @Inject constructor( | ||
private val abacusStateManager: AbacusStateManager | ||
) { | ||
fun registerToken(token: String) { | ||
Log.d("FCMRegistrar", "registering token: $token") | ||
// call abacus method to register token | ||
// Only register token if permission has been granted. | ||
if (ContextCompat.checkSelfPermission(application, Manifest.permission.POST_NOTIFICATIONS) == | ||
PackageManager.PERMISSION_GRANTED | ||
) { | ||
Log.d("FCMRegistrar", "registering token: $token") | ||
abacusStateManager.registerPushToken(token, abacusLocalizerImp.language ?: "en") | ||
} | ||
} | ||
} |
Oops, something went wrong.