当应用程序位于前台时,在离开通知活动之前提醒用户

问题描述 投票:0回答:1

我在 Android 上使用 FCM 进行推送通知,一切正常,我只是有一个需要实现的要求,但我不确定这是否可行。

因此,当应用程序位于前台(用户正在使用该应用程序)并且弹出推送通知并且用户单击它时,该通知会将他带到某个目标屏幕,

我想要的是,当用户单击通知时,是否可以显示一个警报对话框(无论当前屏幕是什么),警告用户他正在离开当前屏幕(带有“转到”/“拒绝”按钮)?

PS:我目前正在使用 SplashActivity(这是我的启动活动)来处理待处理的意图

val intent = Intent(this, SplachActivity::class.java).apply {
                    putExtra(ENTITY_ID, entityId?.toInt())
                    putExtra(ENTITY_TYPE, entityType)
                    addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
                }

                val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
                    addNextIntentWithParentStack(intent)
                    getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
                    )
                }

                val bitmap: Bitmap? = fetchBitmapFromUrl(notificationImageUrl, this)

                val notification = NotificationCompat.Builder(this, CHANNEL_NEW_CONTENT)
                    .setColor(ContextCompat.getColor(this, R.color.yaraBlue))
                    .setContentTitle(title)
                    .setContentText(description)
                    .setSmallIcon(R.drawable.ic_notification_yara)
                    .setLargeIcon(bitmap)
                    .setStyle(
                        NotificationCompat.BigPictureStyle()
                            .bigPicture(bitmap)
                            .bigLargeIcon(null)
                    )
                    .setContentIntent(pendingIntent)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true)
                    .build()
android firebase push-notification firebase-cloud-messaging
1个回答
0
投票

使用自定义广播接收器或在主要活动中处理它以显示 AlertDialog。 广播接收器

class NotificationReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val entityId = intent.getIntExtra(ENTITY_ID, -1)
        val entityType = intent.getStringExtra(ENTITY_TYPE)

        // Launch an activity to show the dialog
        val dialogIntent = Intent(context, MainActivity::class.java).apply {
            putExtra(ENTITY_ID, entityId)
            putExtra(ENTITY_TYPE, entityType)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        context.startActivity(dialogIntent)
    }
}

在主活动中显示对话框

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    // Check if this activity was launched from a notification
    if (intent != null && intent.hasExtra(ENTITY_ID)) {
        val entityId = intent.getIntExtra(ENTITY_ID, -1)
        val entityType = intent.getStringExtra(ENTITY_TYPE)

        // Show the AlertDialog
        showLeaveScreenDialog(entityId, entityType)
    }
}

private fun showLeaveScreenDialog(entityId: Int, entityType: String?) {
    val builder = AlertDialog.Builder(this)
    builder.setTitle("Leaving Current Screen")
    builder.setMessage("You are about to leave the current screen. Do you want to proceed?")
    
    builder.setPositiveButton("Go") { dialog, _ ->
        dialog.dismiss()
        // Navigate to the target screen here
        navigateToTargetScreen(entityId, entityType)
    }

    builder.setNegativeButton("Decline") { dialog, _ ->
        dialog.dismiss()
    }

    builder.show()
}

private fun navigateToTargetScreen(entityId: Int, entityType: String?) {
    // Implement navigation to your target screen here
}

修改您的 PendingIntent

val intent = Intent(this, NotificationReceiver::class.java).apply {
    putExtra(ENTITY_ID, entityId?.toInt())
    putExtra(ENTITY_TYPE, entityType)
}

val pendingIntent: PendingIntent? = PendingIntent.getBroadcast(
    this,
    0,
    intent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

试试这个

© www.soinside.com 2019 - 2024. All rights reserved.