简体   繁体   English

如何在前台服务中显示多个通知?

[英]How to show multiple notifications in Foreground Service?

I'm making a note app that has an option for pinning notes in the notifications.我正在制作一个笔记应用程序,该应用程序可以选择在通知中固定笔记。
I'm using foreground service but the problem is when I want to pin more than one note, the notification for the second one replaces the first one.我正在使用前台服务,但问题是当我想固定多个音符时,第二个通知会替换第一个。
I'm using each note's unique ID as notificationId .我使用每个笔记的唯一 ID 作为notificationId Here's my code:这是我的代码:

    class MyService : Service() {

    lateinit var note: Note

    override fun onBind(p0: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel()
    }


    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        note = intent.getParcelableExtra(NOTIFICATION_MESSAGE_EXTRA)!!
        showNotification()
        return START_STICKY
    }

    private fun showNotification() {
        val notificationIntent = Intent(this, MyService::class.java)

        val pendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
        )

        val notification = NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("Title")
            .setContentText(note.noteText)
            .setContentIntent(pendingIntent)
            .setGroup(CHANNEL_GROUP_KEY)
            .build()

        startForeground(note.id, notification)

    }


    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val notificationManager =
                getSystemService(NotificationManager::class.java)

            notificationManager.createNotificationChannel(channel)
        }
    }
}

You can done this by using startForeground(notifyId.toInt(), notificationBuilder) in onCreate method of service and then in onStartCommand use notificationManager.notify(notifyId.toInt(), notificationBuilder);您可以通过在服务的 onCreate 方法中使用 startForeground(notifyId.toInt(), notificationBuilder) 然后在 onStartCommand 中使用 notificationManager.notify(notifyId.toInt(), notificationBuilder) 来完成此操作;

Basically you need to only use startForeground once and then you need to use notification manager for showing notification.基本上你只需要使用 startForeground 一次,然后你需要使用通知管理器来显示通知。 That way you are able to show all the notifications and all are using foreground service这样你就可以显示所有通知并且都在使用前台服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 前台服务同一服务的多个通知 - Android foreground service multiple notifications for same service 具有前台服务的 Android 工作人员不显示通知 - Android worker with foreground service does not show notifications 如何从 Android 中的前台服务发送通知 - How to send notifications from a Foreground Service in Android 前台服务中的通知 - Notifications in a foreground service 取消选中“显示通知”后,服务仍在前台运行吗? - Does service still run in foreground after “show notifications” is un-checked? 奥利奥 - 前景服务不显示前景通知 - Oreo - Foreground service does not show foreground notification 用户禁用通知时的前台服务 - Foreground service when user disables notifications 当 foreground_show 设置为 false 并且我的应用程序在后台时,如何获取推送通知以显示? - How do I get push notifications to show when foreground_show is set to false and my app is in the background? 如何将*多种*前台服务类型分配给一个 Android 服务 - How to assign *multiple* foreground service types to one Android service 如何取消前台服务使用通知(滑动关闭)或清除所有通知? - How to cancel a foreground service from using the notification (swipe dismiss) or clear all notifications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM