【发布时间】: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"
}
}
此当前代码在屏幕顶部显示具有高优先级的通知
所以问题是 - 有没有办法显示任何类似于高优先级通知并在应用程序状态被终止时从底部显示的内容?
应该和这张图差不多
【问题讨论】:
-
您是否尝试过使用底部工作表对话框片段?
-
不,但我认为当应用程序处于终止状态时,无法使用底部工作表对话框片段,如果可以让我知道,我需要在应用程序运行时使用 WorkManager 触发此操作处于被杀状态
-
当您触发此操作时,您能否更详细地描述您的用例?当设备重新启动以显示通知时,我有类似的东西,显示底部的工作表对话框片段可能是相似的。
-
所以我的应用程序应该在每天 20:00 显示通知,如果应用程序正在运行或应用程序处于终止状态。我已经设法使用触发 Work 类的 doWork 方法的 WorkManager 来做到这一点,每天 20:00,无论应用程序处于终止状态还是正在运行,它都会从手机屏幕顶部显示通知(高优先级通知)所以任务是每天 20 : 00 从底部显示相同的内容我有一张图片的链接应该如何显示????请检查链接“这是它应该看起来如何的示例图片”
标签: java android android-studio kotlin android-notifications