Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send PushNotifications, using Firebase Cloud Messaging (FCM) to special UDID, directly from device?

I am thinking about keeping all registration ids(push token) in DB and sending notifications to user from iPhone. I tried something like this but did not get any notification.

func sendPNMessage() {
    FIRMessaging.messaging().sendMessage(
        ["body": "hey"], 
        to: TOKEN_ID, 
        withMessageID: "1", 
        timeToLive: 108)  
 } 

What I am doing wrong or maybe it is impossible at all?

like image 997
Svitlana Avatar asked Jun 04 '16 19:06

Svitlana


People also ask

Is FCM token unique per device?

When the time comes to send a message, the token is read from the datastore and a data payload is sent via FCM to the specific device with the token assigned. A messaging token is unique per device and application.


3 Answers

Currently it's not possible to send messages from the application itself. You can send messages from the Firebase Web Console, or from a custom server using the server-side APIs.

What you might want to do is to contact a server (like via http call) and that server will send the message to the user. This way ensure that the API-KEY of the server is protected.

PS: the sendMessage(..) api is called upstream feature, and can be used to send messages from your app to your server, if you server has an XMPP connection with the FCM server.

like image 187
Diego Giorgini Avatar answered Oct 17 '22 13:10

Diego Giorgini


Yes you can send push notification through Firebase.Please make sure do NOT include the server-key into your client. There are ways "for not so great people" to find it and do stuff... The Proper way to achieve that is for your client to instruct your app-server to send the notification.

You have to send a HTTP-Post to the Google-API-Endpoint.

You need the following headers:

Content-Type: application/json
Authorization: key={your_server_key}
You can obtain your server key within in the Firebase-Project.

HTTP-Post-Content: Sample

{ 
    "notification": {
        "title": "Notification Title",
        "text": "The Text of the notification."
    },
    "project_id": "<your firebase-project-id",
    "to":"the specific client-device-id"
}
like image 44
Dupinder kaur Avatar answered Oct 17 '22 14:10

Dupinder kaur


Google Cloud Functions make it now possible send push notifications from device-to-device without an app server.

From the Google Cloud Functions documentation:

Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. In such an app, a function triggered by Realtime Database writes to store new followers could create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.

Example:

  1. The function triggers on writes to the Realtime Database path where followers are stored.

  2. The function composes a message to send via FCM.

  3. FCM sends the notification message to the user's device.

Here is a demo project for sending device-to-device push notifications with Firebase and Google Cloud Functions.

like image 32
Crashalot Avatar answered Oct 17 '22 13:10

Crashalot