Skip to content

Instantly share code, notes, and snippets.

View LloydBlv's full-sized avatar
🏠
Working from home

Reza Najafi LloydBlv

🏠
Working from home
View GitHub Profile
class FakeNotificationFactoryThatShowsRealNotif(
private val context: Context
) : NotificationFactory {
init {
createNotificationChannel()
}
override fun createNotification(context: Context, message: PushMessage): Notification {
return NotificationCompat.Builder(context, TEST_CHANNEL_ID)
@RunWith(AndroidJUnit4::class)
class NotificationUITest {
@Test
fun notificationIsDisplayed() {
val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val context = ApplicationProvider.getApplicationContext<Context>()
val handler = DefaultPushHandler(
FakeNotificationFactoryThatShowsRealNotif(context),
@RunWith(RobolectricTestRunner::class)
class DefaultPushHandlerTest {
private lateinit var context: Context
private val posted = mutableListOf<Notification>()
@Before
fun setup() {
context = ApplicationProvider.getApplicationContext()
}
class BootCompletedReceiver : BroadcastReceiver() {
private val rescheduler: TaskRescheduler by inject()
override fun onReceive(context: Context, intent: Intent?) {
if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
rescheduler.reschedule()
}
}
}
class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
val workManager = WorkManager.getInstance(context)
workManager.enqueue(...) // logic here
}
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
val title = remoteMessage.data["title"]
val body = remoteMessage.data["body"]
val deeplink = remoteMessage.data["deeplink"]
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(deeplink))
val notification = NotificationCompat.Builder(this, "channel_id")
.setContentTitle(title)
.setContentText(body)
.setContentIntent(PendingIntent.getActivity(this, 0, intent, 0))
interface PushHandler {
fun handle(message: PushMessage)
}
class DefaultPushHandler @Inject constructor(
private val notificationFactory: NotificationFactory,
private val deeplinkParser: DeeplinkParser,
@ApplicationContext private val context: Context
) : PushHandler {
override fun handle(message: PushMessage) {
class PushMessageMapper @Inject constructor() {
fun map(message: RemoteMessage): PushMessage {
val data = message.data
return PushMessage(
title = data["title"],
body = data["body"],
deeplink = data["deeplink"],
type = data["type"]
)
}
data class PushMessage(
val title: String?,
val body: String?,
val deeplink: String?,
val type: String?
)
@LloydBlv
LloydBlv / delegateservice.kt
Created May 13, 2025 08:09
Delegate service business logic
@AndroidEntryPoint
class MyFirebaseMessagingService : FirebaseMessagingService() {
@Inject lateinit var pushHandler: dagger.Lazy<PushHandler>
@Inject lateinit var pushMessageMapper: dagger.Lazy<PushMessageMapper>
override fun onNewToken(token: String) {
//Communicate with your server to update the token
}