提问者:小点点

如何在不使用Firebase控制台的情况下发送Firebase云消息通知?


我开始使用新的Google通知服务

多亏了这个代码https://github.com/Firebase/quickstart-android/Tree/master/messaging,我才能够从我的Firebase用户控制台向我的Android设备发送通知。

是否有任何API或方法可以在不使用Firebase控制台的情况下发送通知?我的意思是,例如,一个PHP API或类似的东西,从我自己的服务器直接创建通知。


共3个答案

匿名用户

FireBaseCloudMessaging有一个服务器端API,您可以调用它来发送消息。参见https://firebase.google.com/docs/cloud-messaging/server。

发送消息可以像使用调用HTTP端点一样简单。请参阅https://firebase.google.com/docs/cloud-messaging/server.implementing-http-connection-server-protocol

curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" \
    --Header "Content-Type: application/json" \
    https://fcm.googleapis.com/fcm/send \
    -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"title\":\"Hello\",\"body\":\"Yellow\"}}"

匿名用户

这可以使用CURL

function sendGCM($message, $id) {


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

    $fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message
            )
    );
    $fields = json_encode ( $fields );

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

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, true );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );
}

?>

是要发送到设备的消息

是设备注册令牌

null

匿名用户

使用服务API。

URL:

方法类型:

标题:

Content-Type: application/json
Authorization: key=your api key

null

{ "notification": {
    "title": "Your Title",
    "text": "Your Text",
     "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
  },
    "data": {
    "keyname": "any value " //you can get this data as extras in your activity and this data is optional
    },
  "to" : "to_id(firebase refreshedToken)"
} 

在你的应用程序中,你可以在你的activity中添加以下代码来调用:

<intent-filter>
    <action android:name="OPEN_ACTIVITY_1" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

同时检查Firebase上的答案onMessageReceived not call当应用程序在后台时