Firebase如何发送主题通知

时间:2017-02-22 06:26:06

标签: php android firebase firebase-cloud-messaging

我使用以下脚本向特定用户发送通知:

y.push(arr2[j]);

脚本工作正常,但我如何向安装了我的应用的所有用户发送通知。我在我的应用程序(警报)中创建了一个主题,我可以通过firebase控制台向所有用户发送通知。任何人都可以指导我更新上面的主题脚本。

4 个答案:

答案 0 :(得分:21)

我通过替换

修复了
$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $msg
);

$fields = array
(
    'to'  => '/topics/alerts',
    'notification'          => $msg
);

答案 1 :(得分:2)

您可以发送不带卷曲的通知(我的服务器上不可用)。 我准备了一个函数,可以将通知发送到指定主题:

sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "new_post", "YOUR_SERVER_KEY");

function sendNotification($title = "", $body = "", $customData = [], $topic = "", $serverKey = ""){
    if($serverKey != ""){
        ini_set("allow_url_fopen", "On");
        $data = 
        [
            "to" => '/topics/'.$topic,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => $customData
        ];

        $options = array(
            'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $data ),
                'header'=>  "Content-Type: application/json\r\n" .
                            "Accept: application/json\r\n" . 
                            "Authorization:key=".$serverKey
            )
        );

        $context  = stream_context_create( $options );
        $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
        return json_decode( $result );
    }
    return false;
}

答案 2 :(得分:0)

我正在使用google firebase多次,我建议使用简单的代码来发送关于主题的通知。

public function test()
{
    // Method - 1
    // $fcmUrl = 'https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1';
    // $notification = [
    //     "message" => [
    //         "topic" => "foo-bar",
    //         "notification" => [
    //             "body" : "This is a Firebase Cloud Messaging Topic Message!",
    //             "title" : "FCM Message",
    //         ]
    //     ]
    // ]; 

    // Method - 2 

    $fcmUrl = 'https://fcm.googleapis.com/fcm/send';



    $notification = [
        "to" => '/topics/cbtf',
            "data" => [
                "message" : "Messaging Topic Message!",
            ]
        ]
    ];


    $headers = [
        'Authorization: key=AIza...............klQ5SSgJc',
        'Content-Type: application/json'
    ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    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($notification));
    $result = curl_exec($ch);
    curl_close($ch);

    return true;
}

答案 3 :(得分:0)

您可以将通知发送到Firebase中的任何主题。而且,您可以使用任何语言(仅是http请求)进行操作,但始终必须保持JSON格式,以便可以从

捕获来自Android部分的通知
 public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Timber.d("Data Payload: " + remoteMessage.getData());
}

因此您需要发送以下JSON格式

{
 "to": "\/topics\/general",
 "data": {
 "data": {
  "title": "Database Notification",
  "message": "New Data Added"
   }
 }
}

这样您就可以从remoteMessage.getData()获取此数据集