【问题标题】:How to send notification to a Topic如何向主题发送通知
【发布时间】:2020-01-10 11:36:10
【问题描述】:

我想要一个代码从一个设备向多个设备发送关于特定主题的通知,并且我想在订阅该主题的设备上显示该通知?我将使用 firestore 存储数据和存储令牌,并使用 Firebase 消息发送通知

【问题讨论】:

    标签: firebase flutter dart firebase-cloud-messaging


    【解决方案1】:

    这是我发送特定主题通知的代码

    我希望这对新开发者有所帮助。

    import 'package:http/http.dart' as http;
    
    Future<void> sendNotification(subject,title) async{
    
    final postUrl = 'https://fcm.googleapis.com/fcm/send';
    
    String toParams = "/topics/"+'yourTopicName';
    
    final data = {
    "notification": {"body":subject, "title":title},
    "priority": "high",
    "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "id": "1",
    "status": "done",
    "sound": 'default',
    "screen": "yourTopicName",
    },
    "to": "${toParams}"};
    
    final headers = {
    'content-type': 'application/json',
    'Authorization': 'key=key'
    
    };
    
    final response = await http.post(postUrl,
    body: json.encode(data),
    encoding: Encoding.getByName('utf-8'),
    headers: headers);
    
    if (response.statusCode == 200) {
    // on success do 
    print("true");
    } else {
    // on failure do 
    print("false");
    
    }
    }
    

    使用订阅

    FirebaseMessaging _firebaseMessaging =  FirebaseMessaging();
    _firebaseMessaging.subscribeToTopic("yourTopicName");
    

    【讨论】:

      【解决方案2】:

      设备发送消息要求您调用 Firebase Cloud Messaging API 并指定 FCM 服务器密钥。顾名思义,此密钥只能在受信任的环境中使用,例如您的开发机器、您控制的服务器或 Cloud Functions 等环境。这是必需的原因是任何拥有您的 FCM 服务器密钥的人都可以向您应用的所有用户发送消息。

      最简单的开始方法是简单地运行curl 命令或类似的命令,调用legacy FCM REST API。在此处查看示例:How can I send a Firebase Cloud Messaging notification without use the Firebase Console? 要发送到主题,请确保 to 值类似于 "/topics/your_topic"

      对于更高的生产级别,您可能需要引入服务器或使用 Cloud Functions。然后发送消息就变成了一个多步骤的过程,例如:

      1. 想要发送消息、将该消息写入数据库或调用 API 的客户端。
      2. 此写入操作会触发您的服务器或 Cloud Functions,它会验证请求(确定此用户有权发送此消息)。
      3. 然后是服务器端代码calls the Firebase Admin API to send a message to a topic

      有关这方面的一个示例,请参阅folder in the functions-samples repo

      另见:

      【讨论】:

        【解决方案3】:

        您可以使用firebase_messagingFirebaseMessaging.subscribeToTopic订阅主题

        FirebaseMessaging().subcribeToTopic('topic_name');
        

        您可以使用 Firebase 控制台或一些后端代码向主题发送通知,例如在 Cloud Functions 中。

        Learn more.

        【讨论】:

          【解决方案4】:

          根据firebase_messaging readme页面,在最后一节,你不能使用flutter firebase_messaging库发送云消息阅读Sending Message

          要为用户订阅主题:

          FirebaseMessaging _firebaseMessaging =  FirebaseMessaging();
          _firebaseMessaging.subscribeToTopic("MyTopic");
          

          这将使该设备订阅主题MyTopic

          您也可以通过以下方式退订:

          _firebaseMessaging.unsubscribeFromTopic("MyTopic");
          

          【讨论】:

          • 我想知道如果用户卸载应用程序并重新安装它会发生什么,他会失去所有订阅的主题?将不得不再次订阅所有这些?谢谢!
          • 检查this。当用户订阅时,这意味着他的应用实例唯一的注册令牌将链接到主题,并且在文档中,您将看到如果卸载并重新安装应用,此注册令牌将发生变化。
          • 所以如果令牌发生变化,我将不得不再次注册那个人,对吧?
          • 是的,你知道。就像他们登录并删除了应用程序,重新下载应用程序时必须重新登录。
          猜你喜欢
          • 2017-11-08
          • 2017-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-19
          • 1970-01-01
          相关资源
          最近更新 更多