如何将简单的 "push notification" 从一台设备发送到另一台设备?
How to send simple "push notification" from one device to other device?
我有一个用 Flutter 制作的应用程序。我正在尝试编写一种方法,让用户在我的应用程序中将彼此添加为朋友,但在我这样做之前,我想从一个设备向另一个设备发送推送通知。其实我觉得如果我开始了,剩下的我可以用自己的算法解决。
我尝试过的方法:
- 已安装node.js
- 从项目终端:firebase 登录
- firebase 初始化
- 函数文件已创建并存在于我的项目中
- 我有index.ts个文件
- 我在应用程序打开时为每台设备获得唯一令牌。
我想在 index.ts 文件中放入一个简单的通知发送代码,但我做不到。并且此通知应该可以从一台设备发送到另一台设备。
这是发送设备到设备通知的简单解决方案。
首先创建 json 格式的参数,如下所示
var params = {
"to": "device token",
"notification": {
"title": "Notification Title",
"body": "Notification Message",
"sound": "default",
},
"data": {
"customId": "01",
"badge": 0,
"alert": "Alert"
}
};
然后var url = 'https://fcm.googleapis.com/fcm/send';
调用api。
得到如下代码的响应
var response = await http.post(url,
headers: {
"Authorization": "key= Web server Key over here",
"Content-Type": "application/json"
},body: json.encode(params)
);
if (response.statusCode == 200) {
Map<String, dynamic> map = json.decode(response.body);
print("fcm.google: "+map.toString());
}
else {
Map<String, dynamic> error = jsonDecode(response.body);
print("fcm.google: "+error.toString());
}
我有一个用 Flutter 制作的应用程序。我正在尝试编写一种方法,让用户在我的应用程序中将彼此添加为朋友,但在我这样做之前,我想从一个设备向另一个设备发送推送通知。其实我觉得如果我开始了,剩下的我可以用自己的算法解决。
我尝试过的方法:
- 已安装node.js
- 从项目终端:firebase 登录
- firebase 初始化
- 函数文件已创建并存在于我的项目中
- 我有index.ts个文件
- 我在应用程序打开时为每台设备获得唯一令牌。
我想在 index.ts 文件中放入一个简单的通知发送代码,但我做不到。并且此通知应该可以从一台设备发送到另一台设备。
这是发送设备到设备通知的简单解决方案。 首先创建 json 格式的参数,如下所示
var params = {
"to": "device token",
"notification": {
"title": "Notification Title",
"body": "Notification Message",
"sound": "default",
},
"data": {
"customId": "01",
"badge": 0,
"alert": "Alert"
}
};
然后var url = 'https://fcm.googleapis.com/fcm/send';
调用api。
得到如下代码的响应
var response = await http.post(url,
headers: {
"Authorization": "key= Web server Key over here",
"Content-Type": "application/json"
},body: json.encode(params)
);
if (response.statusCode == 200) {
Map<String, dynamic> map = json.decode(response.body);
print("fcm.google: "+map.toString());
}
else {
Map<String, dynamic> error = jsonDecode(response.body);
print("fcm.google: "+error.toString());
}