【问题标题】:Pass data with PendingIntent and retrieve with onNewIntent when app launches应用程序启动时使用 PendingIntent 传递数据并使用 onNewIntent 检索
【发布时间】:2021-04-21 09:30:37
【问题描述】:

我有一个将接收推送通知的 Android 应用程序。该通知具有可穿戴设备支持,因此该通知也将在带有操作按钮的 Android Wear 中可见。

当通知到达onMessageReceived 类中的onMessageReceived 时,我希望传递数据。尝试在 Intent 中设置数据并通过 Pending Intent 传递。

val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        intent.data = Uri.parse("12345")
        intent.putExtra("user_id", "USER ID")
        intent.putExtra("date", "DATE")
        val pendingIntent = PendingIntent.getActivity(
            applicationContext,
            System.currentTimeMillis().toInt() , intent,
            0
        )
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel =
                NotificationChannel(CHANNEL_ID,
                    CHANNEL_NAME,
                    CHANNEL_IMPORTANCE)
            val notificationManager =
                this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)

            val notificationBuilder =  NotificationCompat.Builder(this,CHANNEL_ID)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(body)
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .extend(NotificationCompat.WearableExtender()
                    .addAction(NotificationCompat.Action(R.drawable.icon,
                        "Explore",
                        pendingIntent))
                )
            notificationManager.notify(0, notificationBuilder.build())
        }

当点击Wear的通知时,捕获onNewIntent中的intent以获取数据传递。但是找不到通过的数据。

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
    setIntent(intent)
    // intent.getStringExtra("date")
    // intent.extras.getString("date")
    // intent.extras.get("date")
    // intent.data
}

无法获取数据。有什么方法可以通过待处理的意图来传递意图?或者如何获取通过 Pending Intent 传递的值。

【问题讨论】:

  • onNewIntent() 被调用了吗?请验证。

标签: android kotlin android-notifications android-pendingintent onnewintent


【解决方案1】:

我的假设是 onNewIntent() 没有被调用。

如果您指定FLAG_ACTIVITY_CLEAR_TOP 并且您的应用程序已经在运行并且任务堆栈中有MainActivity 的实例,Android 将删除MainActivity 的现有实例并创建一个新实例。带有“附加”的Intent 将在onCreate() 中传递,而onNewIntent() 将不会被调用。

如果您的应用未运行,点击通知将启动您的应用并创建MainActivity 的新实例,并且带有“附加”的Intent 将在onCreate()onNewIntent() 中传递调用。

此外,由于您在PendingIntent 中输入的Intent 将被Android 用于从非Activity 上下文中启动Activity,因此您需要指定Intent.FLAG_ACTIVITY_NEW_TASK

此外,在获得带有“附加”的PendingIntent 时,您应该始终指定FLAG_UPDATE_CURRENT,因为如果不这样做,您可能会获得带有一些旧“附加”的现有PendingIntent,或者可能根本没有。使用FLAG_UPDATE_CURRENT 将确保您的Intent“附加”被复制到PendingIntent,即使它已经存在。像这样:

val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.data = Uri.parse("12345")
    intent.putExtra("user_id", "USER ID")
    intent.putExtra("date", "DATE")

val pendingIntent = PendingIntent.getActivity(
        applicationContext,
        System.currentTimeMillis().toInt() , intent,
        PendingIntent.FLAG_UPDATE_CURRENT
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 2022-06-22
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多