使用api

时间:2016-07-05 07:38:36

标签: php firebase firebase-cloud-messaging firebase-notifications

我正在使用PHP使用Firebase API并使用JSON格式发送帖子数据。响应是给我message_id,但邮件没有送达。

当我使用Firebase控制台而不是具有相同字段的API时,它已成功传递。

我的PHP代码:

<?php

$json_data = '{ "data": { 
                  "title": "Hey you got a message",
                  "text": "Hi from firebase api"
                },
                "to": "/topics/all",
                "priority": "high"
              }';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'Authorization:key="Used my server key here"'
                                          ));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>

响应:

{"message_id":64666973642901*****}

PS:用星号隐藏信息。

更新 当我将to字段设置为特定标记时,正在传递消息。但是当它设置为topics/all时无效。 有没有其他方法可以将它发送给与应用程序关联的所有人,就像在控制台中一样?

1 个答案:

答案 0 :(得分:3)

我终于发现了。起初我们的应用程序不会包含任何主题,我们必须明确创建它们。

在您的应用中使用以下方法订阅主题

FirebaseMessaging.getInstance().subscribeToTopic("NAME OF TOPIC");

因此,要将其发送给所有应用用户,请使用上述方法让您的每个用户订阅特定主题。我创建了一个名字&#34; all&#34;。

现在将JSON HTTP POST请求中的to字段设置为topics/NAME OF TOPICtopics/all

注意:订阅某个主题时,如果该名称不存在任何主题,则会自动创建新主题。