[英]Ionic 4 + Firebase - Send Notification User Chat App
I'm building a Chat App with Ionic 4 + Firebase and I would like to send a notification to the device of the user when he receives a new message, even if the app is not running.我正在使用 Ionic 4 + Firebase 构建一个聊天应用程序,我想在用户收到新消息时向他的设备发送通知,即使该应用程序没有运行。
I've been googleing a lot and there are different ways, FCM, onesignal (I dont like them since I found that the service is free but they sell the data of your app to third partiy companies..) , etc... but a lot of the answers are like 2-3 years old so I dont want to start building something and then realize that doesnt work with the latest IOS or Android versions...我一直在谷歌上搜索了很多,有不同的方式,FCM,onesignal(我不喜欢它们,因为我发现该服务是免费的,但他们将您的应用程序的数据出售给第三方公司......),等等......但是很多答案都像 2-3 岁,所以我不想开始构建某些东西然后意识到它不适用于最新的 IOS 或 Android 版本......
I dont want to send notifications to all the users like the most of the FCM tutorials do, I'll try to do this in the future if I have to, now I only want to create a cloud function that gets triggered when something has been added to the firebase data path /messages/ and send a notification to the user.我不想像大多数 FCM 教程那样向所有用户发送通知,如果必须的话,我将来会尝试这样做,现在我只想创建一个在某些事情发生时触发的云功能添加到 firebase 数据路径 /messages/ 并向用户发送通知。
Last thing, can this be tested without building the actual app?最后一件事,可以在不构建实际应用程序的情况下进行测试吗? I still in developing phase, I test everything with ionic serve lab and the App Ionic DevApp that allows me to test cordova plugins for native features, all the tutorials for notifications for FCM they explain how to register the app, so the app has to be actually built before.
我仍处于开发阶段,我使用 ionic serve lab 和 App Ionic DevApp 测试所有内容,它允许我测试cordova 插件的本机功能,所有FCM 通知教程都解释了如何注册应用程序,因此应用程序必须是其实之前建的。 Is it possible to test it directly with the Ionic DevApp and ionic serve?
是否可以直接使用 Ionic DevApp 和 ionic serve 进行测试?
Thanks!谢谢!
sendPushNotification() {
const headers = new HttpHeaders({
"Content-Type": "application/json",
Authorization: "firebase_serverkey"
});
console.log("Headers", headers);
let body = {
"notification": {
"title": "User",
"body": "New message",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon"
},
"data": {
"landing_page": "second",
"price": "$3,000.00"
},
"to": this.activatedRoute.snapshot.paramMap.get('token'),
"priority": "high",
"restricted_package_name": ""
}
this.http.post('https://fcm.googleapis.com/fcm/send', JSON.stringify(body), { headers: headers })
.subscribe(res => {
console.log("data from notification", res)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.