You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase-admin Python SDK通知Channel ID设置方法咨询

在Firebase Admin SDK中设置Android通知Channel ID

在Firebase Admin SDK中,对应Legacy HTTP协议里的android_channel_id参数,是通过AndroidConfig嵌套的AndroidNotification对象来设置的,而非顶层的通用Notification对象。以下是主流语言的实现示例:

Node.js 示例

const admin = require('firebase-admin');
admin.initializeApp();

const message = {
  notification: {
    title: '测试通知',
    body: '这是一条带Channel ID的通知'
  },
  android: {
    notification: {
      channelId: 'your_target_channel_id' // 此处填入你的通知渠道ID
    }
  },
  token: 'target_device_token'
};

admin.messaging().send(message)
  .then((response) => {
    console.log('通知发送成功:', response);
  })
  .catch((error) => {
    console.log('通知发送失败:', error);
  });

Java 示例

import com.google.firebase.messaging.AndroidConfig;
import com.google.firebase.messaging.AndroidNotification;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;

public class FirebaseNotificationSender {
  public static void main(String[] args) throws Exception {
    // 构建基础通知内容
    Notification notification = Notification.builder()
        .setTitle("测试通知")
        .setBody("这是一条带Channel ID的通知")
        .build();

    // 配置Android专属参数,设置Channel ID
    AndroidConfig androidConfig = AndroidConfig.builder()
        .setNotification(AndroidNotification.builder()
            .setChannelId("your_target_channel_id") // 填入已在客户端创建的渠道ID
            .build())
        .build();

    // 组装完整消息并发送
    Message message = Message.builder()
        .setNotification(notification)
        .setAndroidConfig(androidConfig)
        .setToken("target_device_token")
        .build();

    String response = FirebaseMessaging.getInstance().send(message);
    System.out.println("通知发送成功: " + response);
  }
}

注意事项

  • 你设置的Channel ID必须是Android客户端已提前创建的通知渠道,否则通知会被系统默认渠道接收,或无法正常展示自定义的渠道行为(如铃声、震动模式等)。

内容的提问来源于stack exchange,提问作者R.LI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:55:18