Create a Notification Panel
A notification itself is a panel, and a panel needs an IviFragment
and a
FrontendViewModel
for the creation.
To create notifications, you need:
TomTom IndiGO provides clients different ways for the notification creation. You can either use:
StockNotificationPanel
to create a notification;- or implement a
NotificationPanel
yourself.
The following sections explain different ways to create a notification. An example app is
provided in the examples/notifications
directory.
Create a Stock Notification Panel
StockNotificationPanel
is a notification panel which can only be created by
StockNotificationPanel
.create
. You don't need to derive
NotificationPanel
, NotificationFragment
, and
NotificationViewModel
yourself. You can just focus on providing data for the
content.
The example below creates a notification when you click on the button. It demonstrates how you can
create it by using StockNotificationPanel
.create
.
1import androidx.lifecycle.LiveData2import com.tomtom.ivi.platform.frontend.api.common.frontend.panels.NotificationPanel3import com.tomtom.ivi.platform.frontend.api.common.frontend.viewmodels.FrontendViewModel4import com.tomtom.ivi.platform.frontend.api.template.notificationpanel.NotificationViewModel5import com.tomtom.ivi.platform.frontend.api.template.notificationpanel.stock.StockNotificationPanel6import com.tomtom.tools.android.api.livedata.ImmutableLiveData7import com.tomtom.tools.android.api.resourceresolution.drawable.ResourceDrawableResolver8import com.tomtom.tools.android.api.resourceresolution.string.StaticStringResolver9import com.tomtom.tools.android.api.resourceresolution.string.StringResolver10import com.tomtom.tools.android.api.uicontrols.button.TtButton11import com.tomtom.tools.android.api.uicontrols.button.TtButtonViewModel12import com.tomtom.tools.android.api.uicontrols.imageview.ImageDescriptor1314internal class NotificationCreationViewModel(panel: NotificationCreationPanel) :15 FrontendViewModel<NotificationCreationPanel>(panel) {1617 fun onStockNotificationButtonClicked() =18 panel.addPanel(19 StockNotificationPanel.create {20 frontendContext = panel.frontendContext21 priority = NotificationPanel.Priority.HIGH22 headerViewModel = HEADER23 bodyText = BODY_TEXT24 primaryActionButtonViewModel = PRIMARY_BUTTON25 secondaryActionButtonViewModel = SECONDARY_BUTTON26 optionViewModels = NOTIFICATION_OPTIONS27 }28 )2930 private companion object {31 val HEADER = NotificationViewModel.HeaderViewModel(32 imageDescriptor = ImageDescriptor(33 ResourceDrawableResolver(R.drawable.ttivi_notification_icon_placeholder)34 ),35 title = StaticStringResolver("Stock"),36 description = StaticStringResolver("Created by StockNotificationPanel.create")37 )3839 val BODY_TEXT: LiveData<StringResolver?> =40 ImmutableLiveData(StaticStringResolver("Body text"))4142 val PRIMARY_BUTTON: LiveData<TtButtonViewModel?> = ImmutableLiveData(43 TtButtonViewModel(44 text = ImmutableLiveData(StaticStringResolver("Primary")),45 actionType = ImmutableLiveData(TtButton.ActionType.PRIMARY)46 )47 )4849 val SECONDARY_BUTTON: LiveData<TtButtonViewModel?> = ImmutableLiveData(50 TtButtonViewModel(51 text = ImmutableLiveData(StaticStringResolver("Secondary")),52 actionType = ImmutableLiveData(TtButton.ActionType.DESTRUCTIVE)53 )54 )5556 val NOTIFICATION_OPTIONS = ImmutableLiveData(57 listOf(58 NotificationViewModel.OptionViewModel(59 description = ImmutableLiveData(60 StaticStringResolver("Notification option")61 )62 )63 )64 )65 }66}
Implement your own NotificationPanel
If you need more complex logic for notifications, for example extra actions to be performed when a
notification is dismissed, you can implement NotificationPanel
yourself. The
example below is similar to the previous one, except we choose to implement
NotificationPanel
manually.
1import androidx.lifecycle.LiveData2import com.tomtom.ivi.platform.frontend.api.common.frontend.FrontendContext3import com.tomtom.ivi.platform.frontend.api.common.frontend.IviFragment4import com.tomtom.ivi.platform.frontend.api.common.frontend.panels.NotificationPanel5import com.tomtom.ivi.platform.frontend.api.template.notificationpanel.NotificationFragment6import com.tomtom.ivi.platform.frontend.api.common.frontend.viewmodels.FrontendViewModel7import com.tomtom.ivi.platform.frontend.api.template.notificationpanel.NotificationViewModel8import com.tomtom.tools.android.api.livedata.ImmutableLiveData9import com.tomtom.tools.android.api.resourceresolution.drawable.ResourceDrawableResolver10import com.tomtom.tools.android.api.resourceresolution.string.StaticStringResolver11import com.tomtom.tools.android.api.resourceresolution.string.StringResolver12import com.tomtom.tools.android.api.uicontrols.button.TtButton13import com.tomtom.tools.android.api.uicontrols.button.TtButtonViewModel14import com.tomtom.tools.android.api.uicontrols.imageview.ImageDescriptor1516internal class NotificationCreationViewModel(panel: NotificationCreationPanel) :17 FrontendViewModel<NotificationCreationPanel>(panel) {1819 fun onCustomNotificationButtonClicked() =20 panel.addPanel(ExampleNotificationPanel(panel.frontendContext))21}2223internal class ExampleNotificationPanel(24 frontendContext: FrontendContext25) : NotificationPanel(frontendContext, Priority.HIGH) {2627 override fun createInitialFragmentInitializer() =28 IviFragment.Initializer(29 NotificationFragment(ExampleNotificationViewModel::class),30 this31 )3233 override fun onDismissed() {34 super.onDismissed()3536 onDismissedAction()37 }3839 private fun onDismissedAction() {40 // Things to be executed when the panel is dismissed.41 }42}4344internal class ExampleNotificationViewModel(panel: ExampleNotificationPanel) :45 NotificationViewModel<ExampleNotificationPanel>(panel){4647 override val headerViewModel = HeaderViewModel(48 imageDescriptor = ImageDescriptor(49 ResourceDrawableResolver(R.drawable.ttivi_notification_icon_alternative_placeholder)50 ),51 title = StaticStringResolver("Custom"),52 description = StaticStringResolver("Extend from NotificationPanel")53 )5455 override val bodyText: LiveData<StringResolver?> =56 ImmutableLiveData(StaticStringResolver("Body text"))5758 override val primaryActionButtonViewModel: LiveData<TtButtonViewModel?> =59 ImmutableLiveData(60 TtButtonViewModel(61 text = ImmutableLiveData(StaticStringResolver("Primary")),62 actionType = ImmutableLiveData(TtButton.ActionType.ACCEPTANCE),63 onClick = { onPrimaryButtonClicked() }64 )65 )6667 private fun onPrimaryButtonClicked() {68 // Things to be executed when the primary button is clicked.69 }7071 override val secondaryActionButtonViewModel: LiveData<TtButtonViewModel?> = ImmutableLiveData(72 TtButtonViewModel(73 text = ImmutableLiveData(StaticStringResolver("Secondary")),74 actionType = ImmutableLiveData(TtButton.ActionType.DESTRUCTIVE),75 onClick = { onSecondaryButtonClicked() }76 )77 )7879 private fun onSecondaryButtonClicked() {80 // Things to be executed when the secondary button is clicked.81 }8283 override val optionViewModels: LiveData<List<OptionViewModel>> = ImmutableLiveData(84 listOf(85 OptionViewModel(86 imageDescriptor = ImmutableLiveData(87 ImageDescriptor(88 ResourceDrawableResolver(R.drawable.ttivi_notificationoption_icon_placeholder)89 ),90 ),91 description = ImmutableLiveData(92 StaticStringResolver("Notification option")93 ),94 )95 )96 )97}