【问题标题】:Is it possible to make notification appear from bottom side while the app is in killed state?当应用程序处于终止状态时,是否可以从底部显示通知?
【发布时间】:2021-08-25 09:50:21
【问题描述】:

问题 - 所以事情就是这样,当应用程序处于终止状态时,尝试从应用程序的底部显示通知,或者它可以是任何可以像具有非常高优先级但应该从底部显示的通知一样的东西,并且应该用WorkManager来完成,让它每天在指定的时间出现

做了很多研究,如您所见,找不到任何可以解决我的小问题的相关信息。

我已经尝试过 - 在 Worker 类的 doWork 方法中显示一个snackBar

class NotifyWork(context: Context, params: WorkerParameters) : Worker(context, params) {
    
    override fun doWork(): Result {
        
        // val mySnackbar = Snackbar()
        
        val id = inputData.getLong(NOTIFICATION_ID, 0).toInt()
        sendNotification(id)
        return success()
    }

    private fun sendNotification(id: Int) {
        val intent = Intent(applicationContext, MainActivity::class.java)
        intent.flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_CLEAR_TASK
        intent.putExtra(NOTIFICATION_ID, id)

        val notificationManager =
            applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        val notificationLayoutExpanded =
            RemoteViews(applicationContext.packageName, R.layout.layout_notification)
        val bitmap = applicationContext.vectorToBitmap(R.mipmap.ic_launcher)
        val pendingIntent = getActivity(applicationContext, 0, intent, 0)
        
        val notification = NotificationCompat.Builder(applicationContext, NOTIFICATION_CHANNEL)
            .setLargeIcon(bitmap)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setAutoCancel(true)
            .setPriority(PRIORITY_MAX)
            .setCustomContentView(notificationLayoutExpanded)
            .setOngoing(true)
            .setFullScreenIntent(pendingIntent, true)
            .setContentIntent(pendingIntent)
        notification.priority = PRIORITY_MAX

        if (SDK_INT >= O) {
            notification.setChannelId(NOTIFICATION_CHANNEL)

            val ringtoneManager = getDefaultUri(TYPE_NOTIFICATION)
            val audioAttributes = AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
                .setContentType(CONTENT_TYPE_SONIFICATION).build()

            val channel =
                NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, IMPORTANCE_HIGH)

            channel.enableLights(true)
            channel.lightColor = RED
            channel.enableVibration(true)
            channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
            channel.setSound(ringtoneManager, audioAttributes)
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(id, notification.build())
    }

    companion object {
        const val NOTIFICATION_ID = "appName_notification_id"
        const val NOTIFICATION_NAME = "appName"
        const val NOTIFICATION_CHANNEL = "appName_channel_01"
        const val NOTIFICATION_WORK = "appName_notification_work"
    }
}

此当前代码在屏幕顶部显示具有高优先级的通知

所以问题是 - 有没有办法显示任何类似于高优先级通知并在应用程序状态被终止时从底部显示的内容?

应该和这张图差不多

This is the sample picture of how it should look

【问题讨论】:

  • 您是否尝试过使用底部工作表对话框片段?
  • 不,但我认为当应用程序处于终止状态时,无法使用底部工作表对话框片段,如果可以让我知道,我需要在应用程序运行时使用 WorkManager 触发此操作处于被杀状态
  • 当您触发此操作时,您能否更详细地描述您的用例?当设备重新启动以显示通知时,我有类似的东西,显示底部的工作表对话框片段可能是相似的。
  • 所以我的应用程序应该在每天 20:00 显示通知,如果应用程序正在运行或应用程序处于终止状态。我已经设法使用触发 Work 类的 doWork 方法的 WorkManager 来做到这一点,每天 20:00,无论应用程序处于终止状态还是正在运行,它都会从手机屏幕顶部显示通知(高优先级通知)所以任务是每天 20 : 00 从底部显示相同的内容我有一张图片的链接应该如何显示????请检查链接“这是它应该看起来如何的示例图片”

标签: java android android-studio kotlin android-notifications


【解决方案1】:

我认为您需要的是 android 中的 bottomSheet,它从底部显示。 当应用程序被杀死时,会调用 onDestroy(){check cmets},因此您可以在其中调用底部表。 并确保它不会被应用程序杀死: https://stackoverflow.com/a/52258125/15552614 看看这个。

【讨论】:

  • 来自Android Developers 网站:注意:不要指望这个方法被称为保存数据的地方!例如,如果一个活动在内容提供者中编辑数据,那么这些编辑应该在 onPause() 或 onSaveInstanceState(Bundle) 中提交,而不是在此处提交。。所以onDestroy() 不能保证被调用,因此不是放置此类代码的可靠位置。使用服务会更合适,因为杀死应用程序也会停止服务并且可以捕获此事件。
  • 感谢您的回答!我描述的有点错误,所以我的意思是我想让通知看起来像东西,而应用程序已经处于终止状态正如您所知,即使应用程序按计划处于终止状态,也会从顶部显示具有高优先级的通知,如果你有任何想法请告诉我
猜你喜欢
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
相关资源
最近更新 更多