I'm sending to Firebase Cloud Messaging with the following:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=[SERVER_KEY] (The one found in Firebase Project>Settings>Cloud Messaging
Body:
{
"notification" = {
"title": "Title",
"text": "Your Text",
"sound": "default",
"badge": "1"
},
"to" = "a4fbc860-23fd-4ffc-95b7-11c90d8db320" (The Device ID from the hand held)
}
When I send this I continually receive: {"results":[{"error":"InvalidRegistration"}]}
I'm sure it's something simple that I'm missing. The docs aren't very clear on what elements are required and the nomenclature is different between the error messages and the Firebase site. I've tried to swap out "registration_ids" with "to" (using the array, of course), but the result is the same.
When sending to a single device, "to" field should specify an FCM registration token, not a Device ID. You can retrieve the registration token using FirebaseInstanceId.getInstance().getInstanceId() method.
This token looks like two strings, a short and a long one, separated by a colon, e.g. "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
Links to relevant documentation:
Registration token
Legacy HTTP Protocol errors
Related
I'm setting up a Slack App to post messages via the Web API and using a bot OAuth token from an ASP.NET web app, and one of the requirements is for the Slack app to be able to return a relevant value for when a user has last read their messages in their DM with the Slack bot. This is to track which messages require updating or reposting.
At first I figured you can send a request with the conversations.info method to get the "last_read" property from the JSON response. And if I was using the user OAuth token (with "xoxp-...") in the request for any direct message opened by the user associated with the token, I would get a value such as "1571423219851.000000" milliseconds.
But when I would use the bot token (using "xoxb-...") to check a DM to a user, it would have for the "last_read" field in zeros.
The steps to reproduce the issue is:
Have the Slack App open a direct message with a user (in this case with myself, as the user OAuth token is set to my account) by conversations.open method, supplying the bot user token and users=(your_slack_id). Save the DM id for later.
https://api.slack.com/methods/conversations.open/test
"ok": true,
"no_op": true,
"already_open": true,
"channel": {
"id": "DP4NRGLNE"
}
Send a message to propogate the message window using the DM id.
https://api.slack.com/methods/chat.postMessage/test
Test with the conversations.info method using the bot token and DM id.
https://api.slack.com/methods/conversations.info/test
However, when I run the method, I would get a response like this.
"ok": true,
"channel": {
"id": "DP4NRGLNE",
"created": 1571404761,
"is_archived": false,
"is_im": true,
"is_org_shared": false,
"user": "UN4F12QH7",
"last_read": "0000000000.000000",
"latest": {
...
},
"unread_count": 3,
"unread_count_display": 3,
"is_open": true,
"priority": 0
}
Expected: The "last_read" field is set to some value, i.e. "1571404761.000000", with unread_count set to 0.
Actual: The "last_read" field is still set to "0000000000.000000" (and unread_count not resetting to 0), despite having message history in the direct message, and having acknowledged "Mark read" for new messages/ physically opened the chat log and scrolled down over the new messages.
The suspicion is that using the user OAuth token will return a relevant last_read value because a user themselves will set a last_read timestamp in a given conversation while a Slack bot doesn't ever set the "last_read" field for a conversation they are part of (i.e. a user doesn't need to know when the bot itself last read the chat?). Though if it that was the case, then why is using a bot token unable to see a set value for the last_read field for a user in their DM? That's sort of where this question comes from (and the confusion).
This is probably an incorrect theory, but Slack documentation doesn't seem to address this concern, so any suggestions would be welcome.
EDIT: After looking closer at the documentation, it would appear that the child family of methods im doesn't have a concept of im.info, hence the default value of 0 is set for a dm.
As the documentation states about the last_read attribute for channel types:
last_read is the timestamp for the last message the calling user has
read in this channel
If you use the bot token to call conversations.history then the calling user is the bot user, which apparently has not read any of the messages yet.
So this is not a bug, but works as intended.
In C# i am calling a Restful service to send notification to mobile device using Firebase Cloud Messaging(FCM).
I am sending notification to a Topic.I want to know how many users it is successfully sent to, who are subscribed to this topic.
I am using following Post API.
https://fcm.googleapis.com/fcm/send
{
"to" : "/topics/testdesktop_51to75",
"time_to_live" : 60,
"data":{
"alertId": "",
"alertTypeId": "",
"detailUrl": "",
"isFeatured": "",
"description":" ",
"payLoad": "",
"publishDate": "",
"title": ""
}
}
but in response i am getting only message id.
You're sending a message to a topic, in which case the response from the FCM server merely indicates whether the messages was received correctly by the server. The fanning out of the message to the tokens that are subscribed to that topic happens afterwards, and no data about the number of devices it was delivered to is returns in the initial response.
If you want to learn more about the number of tokens that the message was sent to, how long that so-called fan-out took, and how many deliveries were successful, you can do so by connecting the FCM delivery data to BigQuery. For more on that have a look at Understanding message delivery in the Firebase documentation. I also recommend reading the blog post Life of a message from FCM to the device.
I am getting error while sending message using Firebase cloud messaging admin API.
Error message is below
Caused by: com.google.api.client.http.HttpResponseException: 400 Bad Request
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"errors": [
{
"message": "Request contains an invalid argument.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}
Let me put my admin configuration here..
FileInputStream serviceAccount = new FileInputStream("My service accout file.json");
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://deliveryeat-1aa42.firebaseio.com").build();
FirebaseApp.initializeApp(options);
Message sending code is below
// This registration token comes from the client FCM SDKs.
String registrationToken = "YOUR_REGISTRATION_TOKEN";
// See documentation on defining a message payload.
Message message = Message.builder().putData("score", "850").putData("time", "2:45").setToken(registrationToken).build();
// Send a message to the device corresponding to the provided
// registration token.
String response = FirebaseMessaging.getInstance().sendAsync(message).get();
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
maven dependencies that i am using is following
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>5.9.0</version>
</dependency>
So can anyone help me in this? What am I doing wrong?
Yet another cause of this is that your message is too large:
Notification messages can contain an optional data payload. Maximum payload for both message types is 4KB, except when sending messages from the Firebase console, which enforces a 1024 character limit.
https://firebase.google.com/docs/cloud-messaging/concept-options
One possible cause for this is that the client and server are connected to different firebase projects. Project name appears in the google-services.json file on the client and in the credentials json on the server.
Firebase FCM: invalid-argument
I suspect that your registrationToken has an invalid format. It should be 152 characters.
To confirm that, try building your message with setTopic("test") instead of setToken(registrationToken).
In my case, the problem was that certain keys are not allowed in a notification data payload. Specifically, the key 'from' is forbidden.
Firebase data message payload
I had my project name uppercased in the URL (strange, because my project name is actually uppercased)
I received this error trying to set the 'content_available' flag to true (which is what the docs say to do: https://firebase.google.com/docs/cloud-messaging/http-server-ref).
Turns out you need it to be 'content-available' instead.
In my case the problem was in Condition part.
I used invalid characters.
For example wrong: 03:00' in topics
This error also occurs when trying to send a push-notification to an iOS simulator device.
I've been trying to send cloud messages to users who subscribed to multiple topics through an API call (POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send) with the following body:
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer MY_ACCESS_TOKEN`
},
body: {
"message": {
"condition": "'cat' in topics || 'dog' in topics",
"notification": {
"title": "Message Title",
"body": "Message Body"
}
}
}
}
I wrote this config according to the FCM documentation. I can get a response similar to this:
{
"name": "projects/myproject-b5ae1/messages/5735743068807585451"
}
but no messages were received by any devices.
I've used the access token for other APIs, which works. I'm also aware that the FCM documentation also provided a different body for the cURL request, which I also tried but not working.
The weird thing is if I only provide one topic in the condition, a message is actually successfully sent to subscribed devices. Am I writing something wrong?
I've already referenced other solutions on Stackoverflow but everything is still not working:
Firebase API call with multiple topics in condition
How to send FCM Push Notifications to multiple topics
After talking to Firebase Support, they pointed out the problem I'm having here is a known bug they are still trying to fix. And there's no specific timeline for a fix yet.
Therefore, there's no problem with my code and I'll have to figure out a workaround myself if I really want to use the "||"(OR) operator.
My current workaround is keep track of subscribers with a database (since Firebase doesn't offer subscribers querying), then implement the OR condition myself by finding a union of subscribers to different topics.
P.S. I just don't get why the downvotes, I actually found a bug here and referenced all the solutions I could found on SO before asking the question here.
I am using the latest Bluemix Push Notification service. I'm using the MFPPush API to register the device (Android).
Here is my code snippet:
var success = function(message)
{
console.log("Success: " + message);
alert("Reg Device: " + message);
};
var failure = function(message)
{
console.log("Error: " + message);
alert("Error: " + message);
};
MFPPush.registerDevice({}, success, failure);
var notification = function(notification)
{
// notification is a JSON object.
alert(notification.message);
};
MFPPush.registerNotificationsCallback(notification);
The success message contains the following information:
Token:APA91bFtkSr59Zxlr52HU****Uij
UserId: ""
DeviceId: g5c6d98f-0867-3fd1-a353-15bcdef675a2
When I send the notification, my device receives the message.
The Swagger REST API shows that I can arbitrarily give some token, userId and deviceId:
{
"deviceId": "TestDeviceId",
"platform": "G",
"token": "************",
"userId": "Joe"
}
How do I get the "TestDeviceId", and how do I get the "token"? I don't see any API to get that information.
Note: You should probably just use the Client SDK to register as it handles all of this in the background--automatically assigning each device a unique ID. You shouldn't explore this unless you know what you're doing. There isn't a really a reason for trying to manually set the deviceID.
When you register a device for the Push Notifications service, you set these values in the body of the POST request. On a successful call, it will return these values in the response. I'll do a demo of this later in the post.
You can also retrieve a list of the device registrations for the Push Notifications service.
You can use that deviceId to retrieved detailed information about the device, send a specific push notification to that device, subscribe it to a tag and send push notifications to those devices, etc.
Swagger Documentation is here.
Regarding those values, you can put whatever you want in them when you register. Typically, these values would be set automatically by the Bluemix Mobile Android/iOS Client SDK when you do the register call. However, you could do this manually using the REST client.
For example:
Here, I'm registering a device:
It registered successfully:
This is what I get if I ask the Push Notifications service for information about my registered devices (for the "deviceId": "arandomdeviceid"):
The Android BMS Core Client SDK sets this deviceId using a unique UUID from the device and hashing it with MD5.
You can look in here more more information about that.
#anamica (a) Allow userId parameter to be passed along with the MFPPush registration like MFPPush.register({"userId": "AUniqueUserId"}, success, failure) (b) Add an additional parameter to the target "userIds" (array).
This enhancement has been done, you can give a try by updating the latest SDK.