Multiple push notifications by one post request via google FCM api - android

To sending push notification messages via google FCM api currently I use postman with a json syntax like this:
{
"condition":"my condition",
"notification":{
"title" : "the title",
"body":"the body"
}
}
and it is ok. and if I need to send 2 messages so I have to send messages separately by 2 post call. like this:
{
"condition":"my condition",
"notification":{
"title" : "the title 1",
"body":"the body 1"
}
}
---> pressing send button
{
"condition":"my condition",
"notification":{
"title" : "the title 2",
"body":"the body 2"
}
}
---> pressing send button
and is ok too. but the problem is I have 2 network overheads for sending this messages.
Is there any syntax to send multiple messages by one post request?
I read and I know about the sending to multiple devices and so on... that is not my problem. my problem is : sending 2 or more messages in one post request.

The FCM SDK has an option for batching requests which will let you group messages into a single batch and send them im a single call. For REST request, you can make a list of sub requests:
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"FCM Message",
"body":"This is an FCM notification message to device 0!"
}
}
}
--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
POST /v1/projects/myproject-b5ae1/messages:send
Content-Type: application/json
accept: application/json
{
"message":{
"topic":"readers-club",
"notification":{
"title":"Price drop",
"body":"2% off all books"
}
}
}

Related

Sending push notifications using post requests(manually)(Parse-server)?

with Parse.com I used to be able to send push notifications via simple https post requests, something like:
{
"registration_ids": ["some_device_token_from_the_dashboard"],
"data": { "title": "How are you", "message": "Ok?" }
}
I added a json content type and set Authorization to key=server_key.
Another tool that could be used is: http://www.androidbegin.com/tutorial/gcm.html
Anyway, I'm working with the android SDK v4.13.1 and Parse-server v2.2.16, when I use the dashboard to send push notification to everyone I get the notification, but when I try the post request that used to work with parse.com, the response is:
{
"multicast_id": 8851650859439498656,
"success": 1, "failure": 0,
"canonical_ids": 0,
"results":[ { "message_id": "0:1470077514727128%33b2cdf9f9fd7ecd" } ]
}
It looks like all is find but I don't get the notification at all, btw at the google console I didn't set the IP field so it accepts requests from 0.0.0.0(all interfaces), I assume there something I'm missing here, should the post request be different, what am I doing wrong?, is that a bug or?...
Edit:
Just to clerify, I implement my on PushReceiver that extends ParsePushBroadcastReceiver, and as before when I try to send push via simple post request(using my server-key and registration id) it won't work(the onPushReceive method never called).
POST request details:
My endpoint is https://android.googleapis.com/gcm/send(I did try gcm-http.googleapis.com/gcm/send as well)(https://developers.google.com/cloud-messaging/http for more details), Content-Type set to application/json and Authorization set to key=server_key, the content is given at the beginning of the question.

FCM returning response for single registration id when targeted for multiple registration_ids

I am writing app server side of FCM implementation where it will send a particular message to Multiple devices.
I am using registration_ids as key and JsonArray with multiple recipients, however the response returned is showing only for one recipient as follows
{"multicast_id":7185150746202793615,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
I am not sure whether to set any property in firebase console to enable multiple registration ids acceptance.
Following is the request json :
{
"registration_ids":[
[
"eU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i",
"FERTeU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i"
]
],
"notification":{
"title":"Hello FCM",
"text":"Notification details"
},
"time_to_live":3600,
"priority":"HIGH"
}
And the response for this request is
{
"multicast_id":7697036511101523125,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"InvalidRegistration"
}
]
}
The Problem was with this Json formation for request. It has to be
{
"registration_ids"[ "eU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i",
"FERTeU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i" ],
"notification":{
"title":"Hello FCM",
"text":"Notification details"
},
"time_to_live":3600,
"priority":"HIGH"
}
The extra square brackets ([ ]) is causing the issue.

Gmail API labels Android

I am using the Gmail API for Android for an application to send emails.
I want to send the mails such that they are received in the Social group of messages.
So is it possible in any way that I can set the labels for an email while sending it using the Gmail API ?
It is possible for us to set labels while sending mails through mail.google.com so how can the same be achieved with the Gmail API ?
You cannot specify the labels message should have when sending it. Under what tab certain messages should end up under is up to the user.
But if you are sending a message to the user himself howewer, it's not that hard to modify the message once it has been sent. When you send a message, you get all the label that were applied to the message in the response. Just send a message, and then modify the labels, and you are done.
Here's an example (with regular http-requests, but you could do the same with one of the client libraries):
// Base64-encoding the message and making it url-safe by replacing
// all '+' with '-', and all '/' with '_'.
btoa("To: example#gmail.com\n" +
"From: example#gmail.com\n" +
"Subject: Cool man\n\n" +
"Here is the message").replace(/\+/g, '-').replace(/\//g, '_');
// => "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"
Sending the message:
POST https://www.googleapis.com/gmail/v1/users/me/messages/send
{
"raw": "VG86IGV4YW1wbGVAZ21haWwuY29tCkZyb206IGV4YW1wbGVAZ21haWwuY29tClN1YmplY3Q6IFRoaXMgaXMgdGhlIHN1YmplY3QKCkhlcmUgaXMgd2VyZSB0aGUgbWVzc2FnZSBnb2Vz"
}
Response
{
"id": "150866b2f6956617",
"threadId": "150866b2f6956617",
"labelIds": [
"SENT",
"INBOX",
"UNREAD"
]
}
Then, I just the add the CATEGORY_SOCIAL-label to get it to show under the social-tab (removing the INBOX-label will not show it at all).
Request
POST https://www.googleapis.com/gmail/v1/users/me/messages/150866b2f6956617/modify
{
"addLabelIds": [
"CATEGORY_SOCIAL"
]
}
Worked great!

GCM not using data from JSON payload in the notification

I am sending the following JSON through GCM (Google Cloud Messaging) but I have not been able to get the right response data through the Client. I get a push notification response, but the title is the App's name, and the texts reads: "message" so I can't display my notification properly.
Here is the JSON that I am trying to send:
{
"to": "somekey",
"notification": {
"body":"Test",
"title":"Test"
},
"data": null
}
I think that your problem is on the key-values of the payload of your message.
As you can see at GCM Server documentation, the payload can be set using two different keys:
data: This parameter specifies the key-value pairs of the message's payload.
notification: This parameter specifies the key-value pairs of the notification payload.
You should use the data key and set inside the payload of the notification. Once send, you can print the result of the notification received and see that there are the right payload inside the data key.
Your message should be like the following JSON data:
{
"registration_ids" => "some_target_device_id",
"data": {
"title": "My title",
"message": "This is the message!",
}
}
Authorization: key= AIz......#GCM project key
{ "data": {
"title": "Summer Offer.",
"message": "Click to visit the offer."
},
"to" : "Device Token"
}

google Cloud Messaging Push notification

Can I use POSTMAN client on Google Chrome to send payload message to GCM server for testing purpose.
Secondly if yes, what is the header and url parameter to be sent.
Yes, you can.
1. Send a notification with a JSON payload
URL: https://android.googleapis.com/gcm/send
Headers:
Authorization: key=<your-api-key>
Content-Type: application/json
Body (click on the 'raw' tab):
{
"collapse_key": "score_update",
"time_to_live": 108,
"delay_while_idle": true,
"data": {
"score": "4x8",
"time": "15:16.2342"
},
"registration_ids":["4", "8", "15", "16", "23", "42"]
}
Note: registration_ids is the only required field, all the others are optional.
2. Send a notification with a plain text payload
URL: https://android.googleapis.com/gcm/send
Headers:
Authorization: key=<your-api-key>
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Body (click on the 'x-www-form-urlencoded' tab):
collapse_key=score_update
time_to_live=108
delay_while_idle=1
data.score=4x8
data.time=15:16.2342
registration_id=42
Note: registration_id is the only required field, all the others are optional.
Source: https://developer.android.com/google/gcm/http.html
Just for the record and to complete the nice answer from #Alexandru Rosianu the GCM endpoint changed a while ago and it is suggested to use the new one. Here is an example taken from the official docs:
Authentication
To send a message, the application server issues a POST request. For example:
https://gcm-http.googleapis.com/gcm/send
A message request is made of 2 parts: HTTP header and HTTP body.
The HTTP header must contain the following headers:
Authorization: key=YOUR_API_KEY
Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text. If Content-Type is omitted, the format is assumed to be plain text.
For example:
Content-Type:application/json
Authorization:key=YOUR_API_KEY
{
"notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to" : "bk3RNwTe3H0:CI2k_H..."
}
The HTTP body content depends on whether you're using JSON or plain text. See the Server Reference for a list of all the parameters your JSON or plain text message can contain.
Example using Curl:
# curl --header "Authorization: key=YOUR_API_KEY" \
--header Content-Type:"application/json" \
https://gcm-http.googleapis.com/gcm/send \
-d "{\"notification\": { \"title\": \"Portugal vs. Denmark\"," \
"\"text\": \"5 to 1\" }, \"to\" : \"bk3RNwTe3H0:CI2k_H...\" }"
Yes, you can use POSTMAN.
This GCM Notification Test Tool simplifies the server side testing immensely by reducing the number of items you enter in POSTMAN everytime - http://techzog.com/development/gcm-notification-test-tool-android/

Categories

Resources