使用http post在FCM中推送通知

时间:2017-08-14 06:12:50

标签: c++ http notifications messaging

我想将http帖子发送给我的fcm客户端。

我使用FCM创建了android客户端应用程序,服务器在Window环境中执行。

我找到了如何推送fcm提供的通知,最后

我发现使用http,我可以轻松搞定。

火基说我可以用这个。

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{
  "to": "/topics/foo-bar",
  "data": {
    "message": "This is a Firebase Cloud Messaging Topic Message!",
   }
}

但我不知道如何将其应用于我的代码。 因为我没学会如何使用http。

匹配主机,我使用'gethostbyname'功能。 但是这个函数返回'nullptr'!

我想知道的是hname应该是什么?我的本地IP?或者是什么? 我真的很困惑。

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

#define MAXLINE 4096
#define MAXSUB  400

SSIZE_T process_http(int sockfd, char *page, char *poststr,char *auKey,char *topic)
{
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    SSIZE_T n;
    snprintf(sendline, MAXSUB,
        "https:%s\r\n"
        "Content-type: application/json\r\n"
        "Authorization:key=%s\r\n"
        "{\r\n"
            "\"to\":\"\\topics\\%s\",\r\n"
            "\"data\" : {\r\n"
                "\"message\" : \"%s\",\r\n"
            "}\r\n"
        "}\r\n"
        , page, auKey, topic, poststr);

    printf("send data : %s\n\n", sendline);
    send(sockfd, sendline, strlen(sendline), 0);
    while ((n = recv(sockfd, recvline, MAXLINE, 0)) > 0)
    {
        recvline[n] = '\0';
        printf("%s\n\n", recvline);
    }
    return n;
}

int main()
{
    WSADATA wsaData;
    SOCKET hSocket;
    int strlen;
    hostent *Host;

    SOCKADDR_IN servAddr;

    //********** You can change. Put any values here *******
    char *hname = "https://fcm.googleapis.com/fcm/send";
    char *page_0 = "//fcm.googleapis.com/fcm/send";
    char *auKey = "AAAAXTiYFNU:APA91bGcH-Ee7JDVC9ZvDA4n09VR3W6x3AEcVrWQjrhbCeQQ_L5pF-7mNRgeMg0xW8g78uLVkjygJ93Za3NL7BXxDvetpZYTSHfiitZwPpiD9iQhgslNuy5Mlz4iuhAHILUpXhAK_o1W";
    char *poststr_0 = "1#";// using camera Number
    char *topic = "news"; // input topic name
    //*******************************************************

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("WSAStartup failed.\n");
        exit(1);
    }
    while (1)
    {
        hSocket = socket(PF_INET, SOCK_STREAM, 0);
        if (hSocket == INVALID_SOCKET)
        {
            printf("Socket failed.\n");
            exit(1);
        }
        //Host = gethostbyname(hname);
        memset(&servAddr, 0, sizeof(servAddr));
        servAddr.sin_family = AF_INET;
        servAddr.sin_addr.s_addr = inet_addr(hname);
        servAddr.sin_port = htons(80);

        if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
        {
            printf("Connect failed.\n");
            exit(1);
        }

        printf("Connected\n\n");

        process_http(hSocket, page_0, poststr_0, auKey, topic);
        closesocket(hSocket);

        Sleep(10000);

    }
    WSACleanup;
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我是根据您的来源制作的。此源使用“ C”。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")

#define MAXLINE 4096
#define MAXSUB  400

SSIZE_T process_http(int sockfd, char *page, char *hname, char *data,char *auKey)
{
    char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    SSIZE_T n;
    sprintf(sendline,
        // Header 
        "POST %s HTTP/1.1\r\n"
        "Host: %s\r\n"
        "Content-Type: application/json;charset=utf-8;\r\n"
        "Authorization:key=%s\r\n"
        "Content-Length: %d\r\n\r\n"
        // Data
        "%s", page, hname, auKey, strlen(data), data);

    printf("send data : %s\n\n", sendline);
    send(sockfd, sendline, strlen(sendline), 0);
    while ((n = recv(sockfd, recvline, MAXLINE, 0)) > 0)
    {
        recvline[n] = '\0';
        printf("recv : %s\n\n", recvline);
    }
    return n;
}

int main()
{
    WSADATA wsaData;
    SOCKET hSocket;
    int strlen;
    struct hostent *Host;
    SOCKADDR_IN servAddr;

    char *hname = "fcm.googleapis.com";
    char *page_0 = "/fcm/send";
    char *auKey = "(Input your authorization key)";
    char *data = "";

    data =
        "{\"to\":\"/topics/(Input your topic)\","
        "\"data\":{"
            "\"title\":\"TEST TITLE\","
            "\"message\":\"TEST_Message\""
            "}"
        "}\"\r\n";

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        printf("WSAStartup failed.\n");
        exit(1);
    }

    if ((Host = gethostbyname(hname)) == NULL) {
        fprintf(stderr, " gethostbyname error for host: %s:",hname);
        exit(1);
    }

    hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (hSocket == INVALID_SOCKET)
    {
        printf("Socket failed.\n");
        exit(1);
    }
    memset(&servAddr, 0, sizeof(servAddr));
    servAddr.sin_family = AF_INET; // IP v4
    servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)*Host->h_addr_list));
    servAddr.sin_port = htons(80);

    if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
    {
        printf("Connect failed.\n");
        exit(1);
    }

    printf("Connected\n\n");

    process_http(hSocket, page_0, hname, data, auKey);
    closesocket(hSocket);

    WSACleanup;
    return 0;
}