如何在 Android 中取消推送通知时发送请求

时间:2021-01-13 14:01:28

标签: android firebase-cloud-messaging android-pendingintent

当应用程序从 push notification 收到 FCM 时,它会调用 onMessageReceived。 (请参阅 123。)

当用户点击通知时,它会启动应用程序,然后向服务器发送用户已阅读通知的请求。

我想知道设备何时收到推送通知,但用户刷了它(或清除了所有通知)。我想向服务器发送一个请求,要求用户简单地取消通知

我尝试发送 BroadcastReceiver 并显示日志(请参阅 45),但它可以在应用程序在发送通知时打开时起作用。我想,那

MyFirebaseMessagingService:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    ...
    // An event when a user swipes a notification.
    val intent = Intent(this, NotificationBroadcastReceiver::class.java)
    intent.action = "notification_cancelled"
    val deleteIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
    notificationBuilder.setDeleteIntent(deleteIntent)

    // Navigation to an activity when a user taps the notification.
    // It doesn't matter to this question.
    val intent2 = Intent(this, MainActivity::class.java)
    val navigateIntent = PendingIntent.getActivity(this, notificationId, intent2,
        PendingIntent.FLAG_UPDATE_CURRENT)
    notificationBuilder.setContentIntent(navigateIntent)
    ...
}

通知广播接收器:

class NotificationBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Timber.d("NotificationBroadcastReceiver onReceive")
        Toast.makeText(context, "Notification dismissed", Toast.LENGTH_LONG).show()
        // Send a request to the server.
    }
}

AndroidManifest:

<uses-permission android:name="com.uremont.NOTIFICATION_PERMISSION" />

    <receiver
        android:name=".receiver.NotificationBroadcastReceiver"
        android:exported="true"
        android:permission="NOTIFICATION_PERMISSION"
        >
        <intent-filter>
            <action android:name="notification_cancelled" />
        </intent-filter>
    </receiver>

仅在应用程序打开时有效。但是当应用程序在后台或被杀死时,它不会对滑动做出反应。可能我们不应该使用 BroadcastReceiver,例如,使用 PendingIntent.getServicePendingIntent.getForegroundService

我们可以向服务器发送请求吗?

1 个答案:

答案 0 :(得分:0)

不久之后它就正常了(虽然我几乎没有改变)。经过大量研究,我提出了这个解决方案。在从 API 19 到 API 30 的多个 Android 模拟器和设备上进行了测试。

因为使用 BroadcastReceivernot safe,所以在 AndroidManifest 中添加:

<receiver
    android:name=".NotificationBroadcastReceiver"
    android:exported="false"
    />

通知广播接收器:

class NotificationBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Timber.d("NotificationBroadcastReceiver onReceive")

        if (intent.extras != null) {
            // Receive parameters of a cancelled notification.
            val authToken = intent.getStringExtra(EXTRA_TOKEN)
            val code = intent.getStringExtra(EXTRA_CODE)
            Timber.d("token = $authToken, code = $code")
            // We can access context even if the application was removed from the recent list.
            Toast.makeText(context, "Notification $code was cancelled", Toast.LENGTH_SHORT).show()
            // Send data to a server.
        }
    }

    companion object {
        const val EXTRA_TOKEN = "EXTRA_TOKEN"
        const val EXTRA_CODE = "EXTRA_CODE"
    }
}

MyFirebaseMessagingService:

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    ...
    // Get notification code from data.
    val code = remoteMessage.data["code"]

    val notificationBuilder = NotificationCompat.Builder(this,
    ...

    val notificationId = Random.nextInt()
    val intent = Intent(this, NotificationBroadcastReceiver::class.java).apply {
        putExtra(EXTRA_TOKEN, authToken)
        putExtra(EXTRA_CODE, code)
    }
    val deleteIntent = PendingIntent.getBroadcast(this, notificationId, intent,
        PendingIntent.FLAG_CANCEL_CURRENT)
    notificationBuilder.setDeleteIntent(deleteIntent)

    val notification = notificationBuilder.build()
    notificationManager.notify(notificationId, notification)
}

使用推送令牌向 Android 设备发送推送消息,例如:

{
  "to": "ddSOGiz4QzmY.....:APA91bHgoincFw.......",
  "data": {
    "title": "Test",
    "message": "Test",
    "code": "ABCDEF"
  }
}

您可以看到发送推送消息的不同方案 here。如果用户在应用程序上按下“强制停止”,它会won't receive 推送消息(“AliExpress”除外,哈哈)。

当用户关闭推送通知时,会调用 NotificationBroadcastReceiver::onReceive()。应用程序获取推送消息的参数。然后我们可以看到一条toast消息,并将这些参数发送给服务器。

当用户按下“全部清除”通知时,将触发所有关闭事件。所以,你会看到一系列的祝酒词。服务器将同时接收多个请求(检查它是否可以在 0.01 秒内处理 10 个请求)。

相关问题