-->

Firebase chat Push Notifications

2019-01-09 01:48发布

问题:

I am developing a chat app using Firebase as backend. It is required that on every new message receiver gets a push notification saying that he got a new message. This is first time I am doing something like this so I have a question:

Since we do not use our own server for this, do we need a 3rd party provider (like Batch for example) for handling push notifications? If I am not mistaken I think Firebase does not support sending push notifications like this but only ones you send from console.

Any help would be great.

回答1:

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.



回答2:

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.



回答3:

(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



回答4:

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"