【问题标题】:Android notification click doesn't work when running the app from notification从通知运行应用程序时,Android 通知点击不起作用
【发布时间】:2022-12-10 19:28:35
【问题描述】:

我的应用程序接收 FCM 并显示为通知。所以我创建这样的通知:

    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    /**
     * Notification Channel
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(id, "Notification", NotificationManager.IMPORTANCE_HIGH)
        notificationChannel.description = "This is notification channel"
        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.RED
        notificationChannel.enableVibration(true)
        notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 500)

        notificationManager.createNotificationChannel(notificationChannel)
    }

    val messageNotification = NotificationCompat.Builder(this, id)
        .setContentIntent(resultPendingIntent)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_baseline_delivery_dining_24)
        .setLargeIcon(bitmap)
        .setContentTitle("${getString(R.string.new_orders)} ${tableNumber}")
        .setContentText("${orderNumber} ${name}")

    /**
     * Main Notification
     */
    val summaryNotification = NotificationCompat.Builder(this, id)
        .setContentIntent(resultPendingIntent)
        .setAutoCancel(true)
        .setWhen(System.currentTimeMillis())
        .setStyle(
            NotificationCompat.InboxStyle()
                .setSummaryText("Incoming orders")
        )
        .setSmallIcon(R.drawable.ic_baseline_delivery_dining_24)
        .setLargeIcon(bitmap)
        .setContentTitle("${getString(R.string.new_orders)} ${tableNumber}")
        .setContentText("${orderNumber} ${name}")

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        summaryNotification.setGroup(BuildConfig.APPLICATION_ID)
        messageNotification.setGroup(BuildConfig.APPLICATION_ID)
        summaryNotification.setGroupSummary(true)
    }

    notificationManager.notify(value, messageNotification.build())
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        notificationManager.notify(1, summaryNotification.build())

我这样设置PendingIntent:

val resultPendingIntent = PendingIntent.getActivity(
        this,
        intent = Intent(this, NotificationActivity::class.java).apply {
            putExtra(ORDER_ID, message.data["id"])
        },
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE
    )

manifest.xml 是这样的:

<application
    android:name=".App"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.MyStyle">
    ...

    <service
        android:name=".service.NotificationMessage"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/logo_icon" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id" />
</application>

当我正常打开应用程序时,每次单击传入通知。它总是按预期工作。该应用程序将转到我的NotificationActivity。

但是当我的应用程序没有打开时,我会单击传入通知。该应用程序将运行,然后直接转到NotificationActivity。在那之后,当下一个通知传入时,我点击时没有任何反应。点击后消失。

当我从通知运行应用程序时,如何让通知点击工作?

【问题讨论】:

  • notificationManager.notify(1, summaryNotification.build())为什么你总是使用相同的通知ID 1?
  • 我创建了分组通知,所以我需要一个 id 来加载任何分组。

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


【解决方案1】:

你能解决这个问题吗,我面临着同样的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多