How can I send a Firebase Cloud Messaging notification without use the Firebase Console?

后端 未结 16 1287
南旧
南旧 2020-11-22 10:17

I\'m starting with the new Google service for the notifications, Firebase Cloud Messaging.

Thanks to this code https://github.com/firebase/quickstart-a

相关标签:
16条回答
  • 2020-11-22 11:02

    Use a service api.

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

    Method Type: POST

    Headers:

    Content-Type: application/json
    Authorization: key=your api key
    

    Body/Payload:

    { "notification": {
        "title": "Your Title",
        "text": "Your Text",
         "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
      },
        "data": {
        "keyname": "any value " //you can get this data as extras in your activity and this data is optional
        },
      "to" : "to_id(firebase refreshedToken)"
    } 
    

    And with this in your app you can add below code in your activity to be called:

    <intent-filter>
        <action android:name="OPEN_ACTIVITY_1" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    

    Also check the answer on Firebase onMessageReceived not called when app in background

    0 讨论(0)
  • 2020-11-22 11:02

    Examples using curl

    Send messages to specific devices

    To send messages to specific devices, set the to the registration token for the specific app instance

    curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{ "data": { "score": "5x1","time": "15:10"},"to" : "<registration token>"}' https://fcm.googleapis.com/fcm/send
    

    Send messages to topics

    here the topic is : /topics/foo-bar

    curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{ "to": "/topics/foo-bar","data": { "message": "This is a Firebase Cloud Messaging Topic Message!"}}' https://fcm.googleapis.com/fcm/send
    

    Send messages to device groups

    Sending messages to a device group is very similar to sending messages to an individual device. Set the to parameter to the unique notification key for the device group

    curl -H "Content-type: application/json" -H "Authorization:key=<Your Api key>"  -X POST -d '{"to": "<aUniqueKey>","data": {"hello": "This is a Firebase Cloud Messaging Device Group Message!"}}' https://fcm.googleapis.com/fcm/send
    

    Examples using Service API

    API URL : https://fcm.googleapis.com/fcm/send

    Headers

    Content-type: application/json
    Authorization:key=<Your Api key>
    

    Request Method : POST

    Request Body

    Messages to specific devices

    {
      "data": {
        "score": "5x1",
        "time": "15:10"
      },
      "to": "<registration token>"
    }
    

    Messages to topics

    {
      "to": "/topics/foo-bar",
      "data": {
        "message": "This is a Firebase Cloud Messaging Topic Message!"
      }
    }
    

    Messages to device groups

    {
      "to": "<aUniqueKey>",
      "data": {
        "hello": "This is a Firebase Cloud Messaging Device Group Message!"
      }
    }
    
    0 讨论(0)
  • 2020-11-22 11:04

    As mentioned by Frank, you can use Firebase Cloud Messaging (FCM) HTTP API to trigger push notification from your own back-end. But you won't be able to

    1. send notifications to a Firebase User Identifier (UID) and
    2. send notifications to user segments (targeting properties & events like you can on the user console).

    Meaning: you'll have to store FCM/GCM registration ids (push tokens) yourself or use FCM topics to subscribe users. Keep also in mind that FCM is not an API for Firebase Notifications, it's a lower-level API without scheduling or open-rate analytics. Firebase Notifications is build on top on FCM.

    0 讨论(0)
  • 2020-11-22 11:04

    You can use for example a PHP script for Google Cloud Messaging (GCM). Firebase, and its console, is just on top of GCM.

    I found this one on github: https://gist.github.com/prime31/5675017

    Hint: This PHP script results in a android notification.

    Therefore: Read this answer from Koot if you want to receive and show the notification in Android.

    0 讨论(0)
提交回复
热议问题