简体   繁体   中英

FCM - Push notification works only in console not in postman

My application developed in NativeScript. For FCM I use nativescript-plugin-firebase .

I have received a push notification whenever I tried from the FCM console. But, I never received a push notification when I try from post man as below.

URL : POST : https://fcm.googleapis.com/fcm/send
Headers : Authorization = key="******", Content-Type=application/json

Data:

{
    "data": {
        "title": "RAJA RAJA",
        "message": "another test",
        "name": "Muthukumar ME"
    },
    "to" : "**************************************"
}

Response:

{
    "multicast_id": 5806593945960213086,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1521623661699559%161a06bff9fd7ecd"
        }
    ]
}

Anyone knows what have I missed that push notification is not coming when I try in postman even though I get a success response.

{ 
    "to" : "********",
    "priority": "high",
    "notification": {
        "title": "Title",
        "body" : "First Notification",
        "text": "Text"
    }
}

I am sending notification thru "topics" like:-

{
 "to" : "/topics/XXXX",

 "notification" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "click_action":"DisplayTestActivity"
 },
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "key_1" : "Data for key one",
   "click_action":"DisplayTestActivity"
 }
}

if you sending notification like this and you are unable to get the notification thru postman then

"Make sure that following line should be their in your splash activity or first page means executed before you are going to get notification "

FirebaseMessaging.Instance.SubscribeToTopic("XXXX");
if(!GetString(Resource.String.google_app_id).Equals("XXXXXXXXXXXXXXXXXXXXX"))  throw new System.Exception("Invalid Json file");
Task.Run(() =>
{

    var instanceId = FirebaseInstanceId.Instance;
    instanceId.DeleteInstanceId();
    Android.Util.Log.Debug("TAG", "{0} {1}", instanceId.Token, instanceId.GetToken(GetString(Resource.String.gcm_defaultSenderId), Firebase.Messaging.FirebaseMessaging.InstanceIdScope));

});

*above code is in c# so use your programming language.. thanks

Send Data Message using HTTP protocol with POSTMAN

You have to copy Legecy Server Key from Firebase Console > Project Settings > Cloud Messaging

Note: Firebase has upgraded our server keys to a new version. You may continue to use your Legacy server key, but it is recommended that you upgrade to the newest version.

Select POST. Enter request URL as https://fcm.googleapis.com/fcm/send Add Headers Authorization: key= OR Authorization: key=and Content-Type: application/json.

Setting-up with POSTMAN Now Select Body > raw > JSON (application/json) and add following code:

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "First Notification",
     "title": "Collapsing A"
 },
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "key_1" : "Data for key one",
     "key_2" : "Hellowww"
 }
}

You can push a Generic notification (with notification payload) or a Custom notifications (with notification and data payload) and hit Send.

{
 "to" : "YOUR_FCM_TOKEN_WILL_BE_HERE",
 "collapse_key" : "type_a",
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "key_1" : "Data for key one",
     "key_2" : "Hellowww"
 }
}

Note that Custom notification will only trigger if there is only data (without notification) node in the payload. Hence, you'd need to move the body and title to data node.

Keep in Mind: Use registration_ids instead of to node if you want to send notification to multiple devices with corresponding firebase_instance_id's.

  1. Open your application in mobile and connect it to PC
  2. Then after opening chrome and paste this chrome://inspect/#devices in Remote Target Hit Inspect: Demo1
  3. Then after you will see the registrationId under Device registered
  4. Copy this Id and paste it after "to":" registrationId " in POSTMAN : Demo2

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM