后端的Firebase推送通知ChannelID

时间:2020-03-23 23:08:53

标签: java android firebase push-notification google-cloud-messaging

我正在尝试找到将channelID(https://developer.android.com/reference/android/app/NotificationChannel.html)添加到从后端代码传到移动应用的Firebase通知中的正确方法。 Android团队已在应用程序中设置了ChannelID,包括默认的channel-id。

由于channelID由操作系统处理,因此Android团队无法拦截通知并对其进行操作。 (当没有channelID时,将使用默认值)。
这是我正在使用的代码:

    var notification = new com.google.firebase.messaging.Notification(title, body);
    Map<String, String> dataMap = new HashMap<>();
    dataMap.put("link", link);
    //channelID is based on parameter.  
    //Different notifications being sent out from the backend have different channel_ID
    dataMap.put("android_channel_id", channelID); 
    var message = Message.builder()
            .setNotification(notification)
            .putAllData(dataMap)
            .setToken(token)
            .build();

    var response = send(message);
    response.setToken(token);

    return response;

我正在使用com.google.firebase.messaging构建要发送的消息。这将返回如下响应:

{
 "aps": {
    "alert": {
        "title": "Message Title",
        "body": "So-and-So has notified you"
    }
 },
 "link": "www.templink.com",
 "android_channel_id":"some_channel based on parameter"
} 

此格式无效。我正在考虑从生成的消息的alert字段中添加android_channel_id。但是由于这是通过构建器完成的,因此我不清楚如何在通知本身内部添加channelID。 (我正在使用的firebase内置通知仅接受两个参数:title,body)

第一个问题是,是否在通知本身中添加channelID是让Android操作系统知道传入通知的channelID的正确方法?如果是这样,我该如何使用仅接受标题和正文的Firebase通知来做到这一点?

第二个问题是,如果上述方法不是最好的方法,那么将通道ID包含在要发送的通知中以便Android OS识别通道ID的最佳方法是什么?

到目前为止,大多数文档都是从Android角度编写的。 (https://firebase.google.com/docs/cloud-messaging/android/client)。这篇文章:https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c对我有很大帮助。但是,它仍未使用Firebase通知。

0 个答案:

没有答案