通知不适用于Kotlin [英] Notifications are not working with kotlin

查看:86
本文介绍了通知不适用于Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在使用Kotlin构建的android应用程序中达到特定阶段时,我正在尝试发送本地通知.

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())
    }

使用Debugging根本没有响应,我发现函数 startTripNotification 被正确触发,但是即使在 logCat 完全没有添加任何内容.

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.

推荐答案

从Android 8.0(API级别26)开始,通知需要一个通知渠道.您需要为通知创建并附加一个通知渠道.

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)
    }

然后

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())
    }

您可以在此处

这篇关于通知不适用于Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆