问题
I'm trying to send a local notification when a user reaches a specific phase in the android application built with kotlin.
Here is the code I'm using :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
activity?.let {
Mapbox.getInstance(
it,
getString(R.string.mapbox_access_token2)
)
}
val root =
inflater.inflate(R.layout.fragment_navigation, container, false)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
startTripNotification()
}
private fun startTripNotification() {
val pendingIntent = PendingIntent.getActivity(activity, 0, Intent(), 0)
val notification = Notification.Builder(activity)
.setContentTitle("test notification title")
.setContentText("test notification text")
.setSmallIcon(R.mipmap.ic_main_icon_round)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.mipmap.ic_launcher))
notification.setContentIntent(pendingIntent)
val notificationManager = activity?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notification.build())
}
There is no response at all, using Debugging I found out that the function startTripNotification
is being triggered correctly, but still no notification appears or any response, even in the logCat
nothing being added at all.
回答1:
Starting in Android 8.0 (API level 26), notifications require a notification channel. You need to create and attach a notification channel for your notification.
private fun initChannel(channelId: String, channelName: String) {
if (Build.VERSION.SDK_INT < 26) {
return
}
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
then in
private fun startTripNotification() {
initChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME)
val pendingIntent = PendingIntent.getActivity(activity, 0, Intent(), 0)
val notification = NotificationCompat.Builder(activity,NOTIFICATION_CHANNEL_ID)
.setContentTitle("test notification title")
.setContentText("test notification text")
.setSmallIcon(R.mipmap.ic_main_icon_round)
.setLargeIcon(BitmapFactory.decodeResource(this.resources, R.mipmap.ic_launcher))
notification.setContentIntent(pendingIntent)
val notificationManager = activity?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notification.build())
}
You can read more about notification channel here
来源:https://stackoverflow.com/questions/58890747/notifications-are-not-working-with-kotlin