Firebase chat Push Notifications

血红的双手。 提交于 2019-11-27 08:28:17

Hope someone finds this useful, it helped in my case.

Currently sending push notifications from device to device isn't possible with solution Firebase offers. If you want that type of thing, you should try OneSignal which is free and very easy to implement.

There are two features in the new Firebase to send notifications to users:

  • Firebase Cloud Messaging - the next generation of Google Cloud Messaging
  • Firebase Notifications - built on top of Cloud Messaging, which allow you to easily send notifications to users through the Firebase Console

To send messages with Cloud Messaging, you need to specify an Authorization Key in a request. Since knowing this key allows sending of messages on your behalf, you'll want to limit access to it to trusted processes. This indeed can be a server, but any trusted process is fine.

(iOS) A little late; but perhaps can help anyone in the future with this.

I recently had this issue, and my solution was to utilize the device group method.

Ergo:

  1. Would create a device group with the registration_ids (or array of device ids for the push)

  2. Would then send the push to said device group

  3. Delete device group

See: https://firebase.google.com/docs/cloud-messaging/ios/device-group

You can send a push notification from Android using Http POST request, i'm using retrofit to make this, example:

Url: https://fcm.googleapis.com/fcm/send

Http Headers:

Content-Type: application/json
Authorization: key=Your_FCM_Key

Body(Json):

{
 "to" : "user_device_id"
  "data" : {
    "message": "test message",
    "color": "black"
  },
  "notification" : {
     "title": "Testing FCM",
     "body":"My first notification",
     "tag":"chat_msg",
     "color" : "#FF4081",
     "icon" : "ic_stat_onesignal_default", 
   }
}

You can make a test using Advanced Rest Client "Extension of Google Chrome"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!