使用PHP在Firebase上创建TOPIC [英] Create TOPIC on firebase using PHP

查看:49
本文介绍了使用PHP在Firebase上创建TOPIC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时在Firebase上创建主题.管理员一旦在我的数据库中创建了某个记录,就将创建相应的主题,以便与该主题关联的用户可以使用Firebase接收通知.

I want to create topics on firebase on runtime. As soon as admin creates a certain record in my database, I will create the respective topic so that users associated with that topic may receive notifications using firebase.

在主题固定的情况下,我可以像这样成功发送符号:

In case of fixed topic, I am able to send the notations successfully like this:

public static function SendFireBaseBroadCast($topicName, $title, $body) {


        #API access key from Google API's Console
        define('API_ACCESS_KEY', 'API_KEY');
        $msg = array
            (
            'body' => $body,
            'title' => $title,
            'icon' => 'myicon', /* Default Icon */
            'sound' => 'mySound'/* Default sound */
        );
        $fields = array
            (
            'to' => "/topics/" . $topicName,
            'notification' => $msg
        );


        $headers = array
            (
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        curl_close($ch);

    }

如何在运行时在Firebase上创建主题?

How can I create topics though on runtime on firebase?

推荐答案

Firebase消息主题无法单独创建-订阅一台设备后它们便开始存在,而没有订阅任何设备时它们就不再存在.

Firebase message topics cannot be created on their own - they start to exist as soon as one device is subscribed to them and stop to exist when no device is subscribed.

从服务器的角度来看,您可以将消息发送到您选择的任何主题(只要名称本身有效).在任何情况下,Firebase都会接受该消息并将其传递给所有已订阅的设备(如果未订阅任何设备,则为0).

From a server perspective, you can send a message to any topic of your choice (as long as the name by itself is valid). Firebase will accept the message in any case and deliver it to all subscribed devices (0 if no device is subscribed).

如果您想将应用程序提供的可用主题发布到应用程序的所有客户端,则需要与Firebase分开进行(例如,使用API​​端点).

If you want to publish the available topics provided by your application to all the clients of your application, you need to do this separately from Firebase (e.g. with an API endpoint).

如果在应用程序中重命名主题,则需要为客户端重新订阅新主题(最好是从旧主题中取消订阅).您可以使用实例ID API( https://developers.google.com/每个实例的instance-id/reference/server ).请注意,当前无法检索订阅该主题的所有设备的列表.也无法重命名主题并将所有订阅的设备从一个主题移到另一个.这是您必须在应用程序级别上实现的业务逻辑.

If you rename a topic in your application, you will need to re-subscribe the clients to the new topic (and preferably unsubscribe them from the old one). You can do this with the Instance ID API (https://developers.google.com/instance-id/reference/server) per instance. Please note that it‘s currently not possible to retrieve a list of all devices subscribed to a topic. It‘s also not possible to rename a topic and move all subscribed devices from one topic to another. This is business logic that you would have to implement on your application‘s level.

Firebase Admin SDK提供了管理主题订阅的方法,请参见 https://firebase.google.com/docs/admin/setup 获取官方SDK的列表.

The Firebase Admin SDKs provide methods to manage topic subscriptions, see https://firebase.google.com/docs/admin/setup for a list of official SDKs.

如果您需要/想要坚持使用PHP,请在 https:/上找到一个非官方的Admin SDK./github.com/kreait/firebase-php (免责声明:我是维护者)

If you need/want to stick to PHP, there‘s an unofficial Admin SDK at https://github.com/kreait/firebase-php (Disclaimer: I‘m the maintainer)

这篇关于使用PHP在Firebase上创建TOPIC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆