I am trying to send an FCM message to a topic. But getting "Invalid topic value provided" error from the server.
Json Payload
{
"message":{
"topic":"/topics/news",
"data":{
"title":"Hellow World",
"message":"This is the Topic Message",
"type1":"100",
"type2":"abc"
}
}
}
Response
{
"error":{
"code":400,
"message":"Request contains an invalid argument.",
"status":"INVALID_ARGUMENT",
"details":[
{
"#type":"type.googleapis.com/google.rpc.BadRequest",
"fieldViolations":[
{
"field":"message.topic",
"description":"Invalid topic value provided."
}
]
},
{
"#type":"type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode":"INVALID_ARGUMENT"
}
]
}
}
I have tried topic value as "news" (without '/topics/') but it throws same error. I can send message to the topic from firebase console without any problem.
Any help appreciated. TIA
Edit1 - Notification payload below works fine but data payload doesn't work. As per documentation, data payloads too are allowed https://firebase.google.com/docs/cloud-messaging/android/topic-messaging
Request
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
EDIT2 :
This works. I had a small bug in my code which was adding additional quotes to the topic. Below request works like a charm
{
"message":{
"topic":"news",
"data":{
"title":"Hellow World",
"message":"This is the Topic Message",
"type1":"100",
"type2":"abc"
}
}
}
according to doc yor request should be like this:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
Related
I use firebase push notification to device token. When app is opening or in foreground I can get notification well. But when app is kill or clear app on current task, I cannot receive notification send.
I have tried on onMessageReceived aleardy at first time work. but now it's not work when killed app.
Code Receive Notification:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(p0: RemoteMessage) {
val data = p0!!.data
Log.e("AAAAAAAAAAAAA: ","data
111111111111111111111111111111111111111111111111:" + data["key1"])
}
}
Post Send notification data:
Send to: https://fcm.googleapis.com/fcm/send
Data :
{
"to" : "token key",
"data": {
"key1" : "value1",
"key2" : "value2",
"other_key" : true
}
}
Result, for app is opening, I can receive data well, but when killed I cannot receive data.
If you're sending data to
https://fcm.googleapis.com/fcm/send
you're using the legacy http protocol as you can see here
This is not very clear in documentation.
In this protocol to receive the data message when app is in background or closed you should use this payload:
{
"to" : "token key",
"data": {
"key1" : "value1",
"key2" : "value2",
"other_key" : true
},
"priority" : 10,
"time_to_live" : 60
}
Test first with maximum priority (10) and then downgrade according t your needs. Also adjust time_to_live in seconds according to your needs.
I am sending a push token to a specific device via the FCM API on testing with Postman, but I intend to send them from the server.
{
"to" : "my_device_token",
"notification" : {
"notificationTitle" :"test from server",
"notificationBody" :"test lorem ipsum"
}
}
I am receiving the response
{
"multicast_id": 4996861050764876123,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results":
[
{
"message_id": "0:1519567530487886%f95ee7d4f95ee123"
}
]
}
Which shows no error, however I don't get any notifications on my phone.
I tried the same device token using on firebase website "Notifications => New Message => Single Device", and it works.
I see threads about not receiving the notifications when the app is not running, however I don't get any even if the app is running.
Edit: My application is built using Xamarin.Android, if relevant.
The notification property keys are title and body:
{
"to" : "my_device_token",
"notification" : {
"title" :"test from server",
"body" :"test lorem ipsum"
}
}
See Table 2b in the documentation.
I am sending push in a single devices using via FCM api. The JSON is given below.
In the case of Android
{
"to" : "device Tokens", // Your android device token
"data" :
{
"body" : "test",
"title" : "test",
"pushtype" : "events",
};
In the case of IOS json
{
"to" : "device Tokens", // iphone tokens
"data" :
{
"body" : "test",
"title" : "test",
"pushtype" :"events",
},
"notification" : {
"body" : "test",
"content_available" : true,
"priority" "high",
"title" = "C#"
}
} ;
put in index.js
PushNotification.configure({
onNotification: function (notification) {
console.log("NOTIFICATION:", notification);
},
requestPermissions: Platform.OS === 'ios'
})
in your screen
createChannels =()=>{
PushNotification.createChannel(
{
channelId:"test-channel",
channelName:"Test-channel"
}
)
}
handleNotification=()=>{
PushNotification.localNotification({
channelId:"test-channel",
title:"Kinza ",
message: "hi gow adww biy",
});
PushNotification.localNotificationSchedule({
channelId: "test-channel",
title:"Alram",
message:"Hi how are you",
date: new Date(Date.now() + 20*1000),
allowWhileIdle:'true',
});
}
Early I had a chance to test FCM service sending request through Postman
https://android.googleapis.com/gcm/send
Header:
"key":"Content-Type","value":"application/json"
"key":"Authorization","value":"key=AIzaSyAA-XXXXXXXXXXXXXXXXXXXX"
Body:
{
"registration_ids":["token_id"],
"data": {
"title" : "title",
"message" : "This is something you might be interested in"
}
}
But now I receive such kind of response
{
"multicast_id": 6211560223995368844,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
Is there anybody could help me to fix my issue. I want to send Push Notification with Data payload and without Notification payload(I couldn't do it through Firebase console because it sends only not null Notification)
We are implementing FCM in our iOS, Android and web apps and saw that the documentation speaks about sending different values to different OS types via ApnsConfig, WebpushConfig and AndroidConfig objects (https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages)
We tried all kinds of variations but we just can't get this to work. Does anybody know what we are doing wrong? Here is a example of how we try to send something:
{
"notification" : {
"title" :"Title",
"body": "Body",
}
"data" : {
"all_devices_key" : "all_devices_value",
}
"content_available": true,
"to": "...",
"apns": {
"headers": {
"ios_only_key": "ios_only_value",
},
}
}
We also tried the payload and other combinations in the apns like the following but nothing ever comes through.
"apns": {
"payload": {
"ios_only_key": "ios_only_value",
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker",
"action-loc-key" : "PLAY"
},
"badge" : 5
},
"acme1" : "bar",
"acme2" : [ "bang", "whiz" ]
},
}
Are we misunderstanding the documentation? How can we send specific key-value-pairs to only iOS or Android or Web?
sorry for my bad English level, I'm from Argentina.
I have the following messages data structure in Firebase:
"messages"
"-KezmqXSdKCNFFA432Uc___-KfCEwklG_y3naRDIUiY"
"messageDate": "20170620"
"messageTime": "18:44"
"message": "Hi"
"-KezFDSAADFASFFS3221___-KASDF32324SDFASD1FS"
"messageDate": "20170620"
"messageTime": "22:23"
"message": "How are you?"
Where -KezmqXSdKCNFFA432Uc, -KfCEwklG_y3naRDIUiY, -KezFDSAADFASFFS3221 and -KASDF32324SDFASD1FS are users.
My problem is that I created a childEventListener in "messages" node to receive new users messages but I am receiving all the new messages of all the users (I'm logged in one user per app) because my childListener is in "messages" node.
Is it correct that if I have 1000 users when adding a message, a new message reaches the 1000 users? (Assuming that within the app, you can check to which user that message belongs).
Thanks!
If you do a structure like similar to this:
-chats
- chatUID
- members
- userUID
- lastMessageSent:messageUID
- ... more properties
-chatMessages
- chatUID
- messageUID
- sentBy: userUID
- messageDate:""
- messageTime:""
- message:""
-userChats
- userUID
- chatUID
you can attach a listener to /userChats/userUID, which will display active chats, and a listener to /chatMessages/chatUID, which will get all chat messages for a specific chat conversation.
This way is a lot easier to setup firebase security rules, and users will only receive chat messages which they are apart of.
Thanks to #Linxy for a brilliant answer
I have created a firebase database regarding #Linxy answer
Here is the complete JSON export
{
"Chats" : {
"-Lsfsd234xda" : {
"lastMessageSent" : "-LrDEBo1-Message",
"members" : [ "-LrDEBoLokW-5mhaT3ys", "-LrDEBoLokW-5mhaT3yz" ],
"more_properties" : "goes here"
}
},
"Users" : {
"-LrDEBoLokW-5mhaT3ys" : {
"id" : "-LrDEBoLokW-5mhaT3ys",
"userDisplayName" : "Qadir Hussain",
"userEmail" : "XXXXX.XXXX#gmail.com",
"userPhotoUrl" : "https://lh3.googleusercontent.com/a-/AAuE7XXXXXXXXX"
},
"-LrDEBoLokW-5mhaT3yz" : {
"id" : "-LrDEBoLokW-5mhaT3yz",
"userDisplayName" : "Ishaq Bhojani",
"userEmail" : "XXXXXXX.XXXXXX#gmail.com",
"userPhotoUrl" : "https://lh3.googleusercontent.com/a-/AAuE7mB3KTbXXXXXXXX"
}
},
"chatMessages" : {
"-Lsfsd234xda" : {
"-LrDEBo-MessageUID" : {
"message" : "Hi there!",
"messageDate" : "10/10/2019",
"messageTime" : "10:16pm",
"sentBy" : "-LrDEBoLokW-5mhaT3ys"
},
"-LrDEBo1-MessageUID" : {
"message" : "Hello",
"messageDate" : "10/10/2019",
"messageTime" : "10:17pm",
"sentBy" : "-LrDEBoLokW-5mhaT3yz"
}
}
},
"userChats" : {
"-LrDEBoLokW-5mhaT3ys" : {
"0" : "-Lsfsd234xda",
"1" : "-Lsfsd234xda1",
"chatUID" : "-Lsfsd234xda"
}
}
}
I know it's late to answer but for future readers although Linxy's answer is neater, I would like to point out a more efficient one having been tried both structures:
ChatMessages
smallerUID_biggerUID
messageUID
sentBy : userUID
messageDate : ""
message : ""
.
.
.
.
UserChats
userUID
pairUID
lastMessage : ""
.
.
.
.
In this way, instead of first finding out the chatId then finding out which user is associated with that chatId, we can directly search which users should appear in our active chat tab and get thouse users' information (username, profilePicture). The reason for that is we can always calculate the chatId if we know the user's id we would like to message with. So for the message tab, we calculate the chatId (smallerUID_biggerUID) in client side and search for the messages in referencing it.
In order to structure your database, please read this post: Structuring your Firebase Data correctly for a Complex App. You'll find here for sure the answer to your question.
As a conclusion, try to flatten(denormalize) your database as much as possible.
Hope it helps.
this structure doesn't support what you want to do, it better to change it by using something like channels, where a channel contains the messages between two persons, so when any one of them send a message the other one will be notified.
{
"users": {
"userId": {
"conversations": {
"conversationId": {
"unseenCount": 0
},
"conversationId2": {
"unseenCount": 3
}
}
},
"conversations": {
"conversationId": {
"displayedMessage": "Message",
"members": {
"userId1": true,
"userId2": true
},
"messages": {
"messageId": {
"type": "text",
"text": "Hello",
"createdAt": "",
"senderId": "userId",
"status": "sent",
"payload": ""
}
},
"lastMessage": "my last message"
}
}
}
I think this will be the best structure for it:
{
messages: {
A8Fcn28ak9ask46: {
chat_id: "combination of sender and receivers number",
sender_id: "person sending the message",
receiver_id: "person send it to",
text: "message that the user types",
timestamp: "123981849404"
},
...
}
}
then when u get the results, you can filter through the chat_id's in forward and in reverse, which will get the conversation between two people.
Hope it helps.