当接收到通知时更改通知内容

3

有没有一种方法可以在通知显示在设备上之前更改通知内容?例如:我有一个名为John的用户订阅了一个名为food的主题,我向该主题发送推送通知并用标题“Good morning username”,但是设备将显示“Good morning John”。

我想知道在Android或iOS上是否可以实现以及如何实现。


你尝试过重写 FirebaseMessagingServiceonMessageReceived 方法吗? - glagarto
没有,我还没有尝试过。使用onMessageReceived可以更改通知本身吗? - David H Moreno
1个回答

1

对于Android,可以通过覆盖FirebaseMessagingServiceonMessageReceived方法来实现。

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Check your notification is not null and subcribed to `food` topic.
    if(remoteMessage.getFrom() == "food" &&
        remoteMessage.getNotification() != null){ 
        String title = remoteMessage.getNotification().getTitle(); // Title from the notification. E.g `Good morning`

        // Get the `username` of the user stored in the device. Use SharedPreference.

        String message = title + " " + username; // "Good morning username"
        // Call your notification here.
    }
}

我不确定这只适用于Android。不幸的是,我不熟悉iOS,但我认为这是可能的。以下是有关在iOS上接收通知的参考链接:https://firebase.google.com/docs/cloud-messaging/ios/receive 希望它能帮到你。 - glagarto
1
在iOS上,您可以使用mutable-content字段,在通知显示给用户之前对其进行操作。https://developer.apple.com/documentation/usernotifications/modifying_content_in_newly_delivered_notifications - Arthur Thompson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接