有没有办法更改Android Foreground Notification的行为?重要性/弹出/声音

时间:2019-05-23 14:34:31

标签: android android-notifications foreground-service

我创建了一个前台服务,该服务在MainActivity启动时启动,并且我需要即时更改通知的行为。重新创建NotificationChannel没有帮助。

该应用需要每X次检查一次新消息。应用启动时,由于我们使用的是前台服务,因此我们必须显示一条通知。该启动不应具有带有声音的弹出通知(可以通过将重要性设置为LOW来实现,但是在这种情况下,这不是可接受的解决方案)。启动前台服务后,它需要检查是否有新消息。如果有,它将弹出通知并发出声音,文本应更新为(您有新消息)。如果没有新消息,则应将其静静地放在带有文本的通知区域中(没有新消息)。如果重要性设置为低,则不会有声音/弹出窗口。我们真的不想使用2条通知。

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    //Create Notification Channel
    createChannel(applicationContext)

    //Start Foreground
    startForeground(NOTIFICATION_ID, createNotification(applicationContext, applicationContext.getString(R.string.notification_message_empty)))

    timer.scheduleAtFixedRate(
        object : java.util.TimerTask() {

            override fun run() {
                updateNotification()
            }

        }, (1000 * REPEATING_CYCLE_SECONDS).toLong(), (1000 * REPEATING_CYCLE_SECONDS).toLong()
    )

    return Service.START_STICKY
}

private fun createChannel(context: Context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager.deleteNotificationChannel(CHANNEL_ID)

        val importance = NotificationManager.IMPORTANCE_HIGH

        val notificationChannel = NotificationChannel(CHANNEL_ID, InnosmartLerakodas.applicationContext().getString(R.string.app_name), importance)

        notificationChannel.enableVibration(true)
        notificationChannel.setShowBadge(true)
        notificationChannel.enableLights(true)
        notificationChannel.lightColor = ContextCompat.getColor(applicationContext, R.color.colorAccent)
        notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
        notificationManager.createNotificationChannel(notificationChannel)
    }

}

private fun createNotification(context: Context, message: String): Notification {

    val notifyIntent = Intent(context, MainActivity::class.java)
    notifyIntent.flags = Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT

    val title = applicationContext.getString(R.string.notification_title)

    notifyIntent.putExtra("title", title)
    notifyIntent.putExtra("message", message)
    notifyIntent.putExtra("notification", true)

    val pendingIntent = PendingIntent.getActivity(context, System.currentTimeMillis().toInt(), notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT)

    lateinit var mNotification: Notification

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        mNotification = Notification.Builder(context, CHANNEL_ID)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setStyle(
                Notification.BigTextStyle()
                    .bigText(message)
            )
            .setContentText(message).build()
    } else {

        mNotification = Notification.Builder(context)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_notification)
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentTitle(title)
            .setStyle(
                Notification.BigTextStyle()
                    .bigText(message)
            )
            .setContentText(message).build()

    }

    return mNotification
}

private fun updateNotification() {
    val text = getCurrentTime()

    val notification = createNotification(applicationContext, text)

    val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    mNotificationManager.notify(NOTIFICATION_ID, notification)
}

预期结果:第一次启动时,通知不会弹出并且不会发出声音。仅显示在通知列表中,并带有文本“没有新消息”。 X时间后,如果有任何新消息,应用程序应弹出带有声音的通知,但不要在通知列表中创建第二个通知。单击它后,它将打开MainActivity,并且应将其自身恢复为“没有新消息”,而不会弹出并且没有声音。

实际结果:要么弹出所有通知并发出声音,要么不发出声音。

0 个答案:

没有答案