当您刷新本地通知时,一段时间没有启动通知

发布于 2025-01-23 18:48:22 字数 3884 浏览 2 评论 0原文

当我在本地通知到来后刷新时,应用程序通知不会出现在屏幕上一段时间。这样做的原因是什么?我读过一些东西,当刷新通知时,它会向系统发出警告,以防止在一定时间段内抬起通知,但是我该如何预防呢?除此之外,我没有任何问题在我的应用中获得通知。


fun getChatChannels(): MutableList<NotificationChannel> {

    val chatChannels = mutableListOf<NotificationChannel>()

    val chatChannel = NotificationChannel(
        IM_CHANNEL_ID,
        Application.getString(R.string.chat_channel_name),
        NotificationManager.IMPORTANCE_HIGH
    )

    chatChannel.setShowBadge(false)
    chatChannel.group = MESSAGE_GROUP_ID

    chatChannels.add(chatChannel)

    return chatChannels }



    private fun getChannelList(): MutableList<NotificationChannel> {

    val channelList = mutableListOf<NotificationChannel>()
    channelList.addAll(getChatChannels())

    return channelList
    }


    fun createGroupsAndChannels() {

    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        val notificationManager =
            Application.instance.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.createNotificationChannelGroups(getGroupList())
        notificationManager.createNotificationChannels(getChannelList())
    }
    Log.d(TAG, "Channels and groups created")
     }
      fun showChatNotification(destinationAddress: String, messageId: String, message: String) {
     ThreadManager.createUITask {
            val name = if (domainContact != null) {
                "${domainContact.firstName} ${domainContact.lastName}"
            } else {
                destinationAddress
            }

            val notificationIntent = Intent(Application.instance, MainActivity::class.java)
            notificationIntent.putExtra(
                Application.getString(R.string.key_conversation_participant),
                destinationAddress
            )
            notificationIntent.flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

            val pendingIntent = PendingIntent.getActivity(
                Application.instance,
                destinationAddress.hashCode(),
                notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val notificationId = UUID.randomUUID().hashCode()

            val builder = NotificationCompat.Builder(Application.instance, IM_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification_message)
                .setColor(ContextCompat.getColor(Application.instance, R.color.colorPrimary))
                .setContentTitle(name)
                .setContentText(message)
                .setStyle(
                    NotificationCompat.BigTextStyle() // Expandable-Collapsible notification
                        .bigText(message)
                )
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(pendingIntent)
                .setVisibility(VISIBILITY_PUBLIC)
                .setAutoCancel(true)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                builder.addAction(getChatNotificationReplyAction(destinationAddress, notificationId, messageId))
            }

            chatNotifications.add(mapOf(destinationAddress to notificationId))
            createNotifications(builder, notificationId)

            Log.d(TAG, "Chat notification created")
        } 


  private fun createNotifications(builder: NotificationCompat.Builder, notificationId: Int) {

        with(NotificationManagerCompat.from(Application.instance)) {
            notify(notificationId, builder.build())
        }
    }

When I swipe up after the local notification comes, the application notifications don't appear on the screen for a while. What could be the reason for this? I've read something that when heads up notifications are swiped up, it sends a warning to the system to prevent heads up notifications for a certain period of time, but how can I prevent this? I don't have any problems getting notifications in my app other than that.


fun getChatChannels(): MutableList<NotificationChannel> {

    val chatChannels = mutableListOf<NotificationChannel>()

    val chatChannel = NotificationChannel(
        IM_CHANNEL_ID,
        Application.getString(R.string.chat_channel_name),
        NotificationManager.IMPORTANCE_HIGH
    )

    chatChannel.setShowBadge(false)
    chatChannel.group = MESSAGE_GROUP_ID

    chatChannels.add(chatChannel)

    return chatChannels }



    private fun getChannelList(): MutableList<NotificationChannel> {

    val channelList = mutableListOf<NotificationChannel>()
    channelList.addAll(getChatChannels())

    return channelList
    }


    fun createGroupsAndChannels() {

    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        val notificationManager =
            Application.instance.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.createNotificationChannelGroups(getGroupList())
        notificationManager.createNotificationChannels(getChannelList())
    }
    Log.d(TAG, "Channels and groups created")
     }
      fun showChatNotification(destinationAddress: String, messageId: String, message: String) {
     ThreadManager.createUITask {
            val name = if (domainContact != null) {
                "${domainContact.firstName} ${domainContact.lastName}"
            } else {
                destinationAddress
            }

            val notificationIntent = Intent(Application.instance, MainActivity::class.java)
            notificationIntent.putExtra(
                Application.getString(R.string.key_conversation_participant),
                destinationAddress
            )
            notificationIntent.flags = Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

            val pendingIntent = PendingIntent.getActivity(
                Application.instance,
                destinationAddress.hashCode(),
                notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val notificationId = UUID.randomUUID().hashCode()

            val builder = NotificationCompat.Builder(Application.instance, IM_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification_message)
                .setColor(ContextCompat.getColor(Application.instance, R.color.colorPrimary))
                .setContentTitle(name)
                .setContentText(message)
                .setStyle(
                    NotificationCompat.BigTextStyle() // Expandable-Collapsible notification
                        .bigText(message)
                )
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(pendingIntent)
                .setVisibility(VISIBILITY_PUBLIC)
                .setAutoCancel(true)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                builder.addAction(getChatNotificationReplyAction(destinationAddress, notificationId, messageId))
            }

            chatNotifications.add(mapOf(destinationAddress to notificationId))
            createNotifications(builder, notificationId)

            Log.d(TAG, "Chat notification created")
        } 


  private fun createNotifications(builder: NotificationCompat.Builder, notificationId: Int) {

        with(NotificationManagerCompat.from(Application.instance)) {
            notify(notificationId, builder.build())
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文