This file contains hidden or 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
| class FakeNotificationFactoryThatShowsRealNotif( | |
| private val context: Context | |
| ) : NotificationFactory { | |
| init { | |
| createNotificationChannel() | |
| } | |
| override fun createNotification(context: Context, message: PushMessage): Notification { | |
| return NotificationCompat.Builder(context, TEST_CHANNEL_ID) |
This file contains hidden or 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
| @RunWith(AndroidJUnit4::class) | |
| class NotificationUITest { | |
| @Test | |
| fun notificationIsDisplayed() { | |
| val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) | |
| val context = ApplicationProvider.getApplicationContext<Context>() | |
| val handler = DefaultPushHandler( | |
| FakeNotificationFactoryThatShowsRealNotif(context), |
This file contains hidden or 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
| @RunWith(RobolectricTestRunner::class) | |
| class DefaultPushHandlerTest { | |
| private lateinit var context: Context | |
| private val posted = mutableListOf<Notification>() | |
| @Before | |
| fun setup() { | |
| context = ApplicationProvider.getApplicationContext() | |
| } |
This file contains hidden or 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
| 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() | |
| } | |
| } | |
| } |
This file contains hidden or 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
| class BootCompletedReceiver : BroadcastReceiver() { | |
| override fun onReceive(context: Context, intent: Intent?) { | |
| val workManager = WorkManager.getInstance(context) | |
| workManager.enqueue(...) // logic here | |
| } | |
| } |
This file contains hidden or 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
| 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)) |
This file contains hidden or 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
| 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) { |
This file contains hidden or 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
| 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"] | |
| ) | |
| } |
This file contains hidden or 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
| data class PushMessage( | |
| val title: String?, | |
| val body: String?, | |
| val deeplink: String?, | |
| val type: String? | |
| ) |
This file contains hidden or 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
| @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 | |
| } |
NewerOlder