gpt4 book ai didi

c++ - 使用 http post 在 FCM 中推送通知

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:32 29 4
gpt4 key购买 nike

我想向我的 fcm 客户发送 http post。

我用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;
}

最佳答案

我根据你的来源制作的。这个来源使用了“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;
}

关于c++ - 使用 http post 在 FCM 中推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45668384/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com