Android 应用程序启动时,使用PendingEvent传递数据并使用onNewIntent检索数据

Android 应用程序启动时,使用PendingEvent传递数据并使用onNewIntent检索数据,android,kotlin,android-notifications,android-pendingintent,onnewintent,Android,Kotlin,Android Notifications,Android Pendingintent,Onnewintent,我有一个Android应用程序,它将接收推送通知。该通知具有可穿戴支持,因此该通知也将在Android wear中显示,并带有操作按钮 我希望在通知到达FirebaseMessagingService类中的onMessageReceived时传递数据。尝试在Intent中设置数据,并以挂起的Intent传递该数据 val intent = Intent(this, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTI

我有一个Android应用程序,它将接收推送通知。该通知具有可穿戴支持,因此该通知也将在Android wear中显示,并带有操作按钮

我希望在通知到达
FirebaseMessagingService
类中的
onMessageReceived
时传递数据。尝试在Intent中设置数据,并以挂起的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
中捕获意图以获得传递的数据。但找不到传递的数据

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

无法获取数据。有没有办法让意向书与待决意向书一起通过?或者如何获得带有挂起意图的传递值。

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

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())
        }
如果您指定
FLAG\u ACTIVITY\u CLEAR\u TOP
,并且您的应用程序已经在运行,并且任务堆栈中有一个
MainActivity
实例,Android将删除现有的
MainActivity
实例并创建一个新实例。带有“extras”的
意图将在
onCreate()
中传递,并且不会调用
onNewIntent()

如果您的应用程序未运行,点击通知将启动您的应用程序并创建一个新的
MainActivity
实例,带有“extras”的
Intent
将在
onCreate()
中传递,并且不会调用
onNewIntent()

此外,由于Android将使用您放在
pendingent
中的
Intent
从非
Activity
上下文启动
Activity
,因此您需要指定
Intent.FLAG\u Activity\u NEW\u TASK

此外,在获取带有“extras”的
PendingEvent
时,您应该始终指定
FLAG_UPDATE_CURRENT
,因为如果不指定,您可能会获取带有一些旧“extras”的现有
PendingEvent
,或者根本没有。使用
FLAG\u UPDATE\u CURRENT
将确保将您的
意图
的“附加内容”复制到
挂起内容
,即使它已经存在。像这样:

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
    )
正在调用
onNewIntent()
?请核实。