简体   繁体   English

Ionic 4 + Firebase - 发送通知用户聊天应用

[英]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.

相关问题 使用 Firebase 实时数据库发送聊天应用构建的推送通知 - Send push notification for chat app build using Firebase Realtime Database 将通知用户发送到用户ionic 3 / firebase的最佳方法是什么 - What is the best way to send notification user to user ionic 3 / firebase Firebase聊天-通知其他用户 - Firebase Chat - Notification to Other User 聊天应用程序的Firebase推送通知 - Firebase Push Notification for chat app 使用Firebase在应用程序处于后台时的聊天通知 - chat notification when app is background using firebase Firebase 推送通知,带有 Firebase 功能和聊天应用程序的 FCM - Firebase Push notification With Firebase Function and FCM for Chat App 用户离线时如何发送图像,就像带有Firebase的聊天应用中的短信一样? - How to send image when user is offline just like the text message in chat app with firebase? 如何在Firebase 3中从App向App发送通知 - How to send notification from App to App in Firebase 3 在Firebase聊天应用程序中检索注册用户 - registered user retrieving in firebase chat app ionic 框架和 firebase(与经过身份验证的用户一对一聊天) - ionic framework and firebase (one on one chat with authenticated user)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM