How to set up FCM Push notifications for group chat app









up vote
1
down vote

favorite












I am attempting to implement push notifications for a group chat app whose database structure is pasted below. I was wondering how to use the conversation_users node to only send push notifications if the user's a part of that conversation and also how to send push notifications to multiple devices since the code I have right now only sends it to my device using its specific fcmToken. Should I be subscribing users to topics, with those topics being the separate conversationIds? This is my first time using FCM, so any guidance would be greatly appreciated.



Database Structure:



 conversation_messages 
conversationId:
message1:
text: "Hello"
timestamp: 1095819823.123
senderId: aSLKN12KLSDNFK


,
conversation_users
conversationId:
userUID:1
userUID2:1

,
users
userUID:
fcmToken: "fcmToken"
bio: "bio"
username: "username"




Index.js code: Right now this code sends push notifications onto my device for any messages sent within the app. I'm looking to limit notifications to only the conversations the user has joined and to prevent push notifications for messages that I sent.



exports.observeMessages = functions.database.ref('/conversation_messages/convoId/messageId')
.onCreate((snapshot, context) =>

var convoId = context.params.convoId;
var messageId = context.params.messageId;
var message = snapshot.val();

return admin.database().ref('/conversations/' + convoId).once('value', snapshot =>

var conversation = snapshot.val(); // This gives us the conversation name

return admin.database().ref('/users/' + message.senderId).once('value', snapshot =>

var user = snapshot.val();

var notification =
notification:
title: conversation.conversationName,
body: user.username + ": " + message.text
,
token: 'dFlruzs91OM:APA91bHVx-Ga11GZ4fEwGAkrvFIegXPgPkvFrNuYa0U6yL_bdC7urM721SQGdnylpBNYPYMWAFpLbDk7pahe1RUMlQ8rN7iK7EAU89Xg6xpbIcBi6ZbTM7TB8N5zxPwhq0MTdIxIRH61'


admin.messaging().send(notification)
.then((response) =>
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response
)
.catch((error) =>
console.log('Error sending message:', error);
throw new Error("Error sending message");
);

)

)

)









share|improve this question





















  • What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
    – Johnykutty
    Nov 9 at 19:30














up vote
1
down vote

favorite












I am attempting to implement push notifications for a group chat app whose database structure is pasted below. I was wondering how to use the conversation_users node to only send push notifications if the user's a part of that conversation and also how to send push notifications to multiple devices since the code I have right now only sends it to my device using its specific fcmToken. Should I be subscribing users to topics, with those topics being the separate conversationIds? This is my first time using FCM, so any guidance would be greatly appreciated.



Database Structure:



 conversation_messages 
conversationId:
message1:
text: "Hello"
timestamp: 1095819823.123
senderId: aSLKN12KLSDNFK


,
conversation_users
conversationId:
userUID:1
userUID2:1

,
users
userUID:
fcmToken: "fcmToken"
bio: "bio"
username: "username"




Index.js code: Right now this code sends push notifications onto my device for any messages sent within the app. I'm looking to limit notifications to only the conversations the user has joined and to prevent push notifications for messages that I sent.



exports.observeMessages = functions.database.ref('/conversation_messages/convoId/messageId')
.onCreate((snapshot, context) =>

var convoId = context.params.convoId;
var messageId = context.params.messageId;
var message = snapshot.val();

return admin.database().ref('/conversations/' + convoId).once('value', snapshot =>

var conversation = snapshot.val(); // This gives us the conversation name

return admin.database().ref('/users/' + message.senderId).once('value', snapshot =>

var user = snapshot.val();

var notification =
notification:
title: conversation.conversationName,
body: user.username + ": " + message.text
,
token: 'dFlruzs91OM:APA91bHVx-Ga11GZ4fEwGAkrvFIegXPgPkvFrNuYa0U6yL_bdC7urM721SQGdnylpBNYPYMWAFpLbDk7pahe1RUMlQ8rN7iK7EAU89Xg6xpbIcBi6ZbTM7TB8N5zxPwhq0MTdIxIRH61'


admin.messaging().send(notification)
.then((response) =>
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response
)
.catch((error) =>
console.log('Error sending message:', error);
throw new Error("Error sending message");
);

)

)

)









share|improve this question





















  • What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
    – Johnykutty
    Nov 9 at 19:30












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am attempting to implement push notifications for a group chat app whose database structure is pasted below. I was wondering how to use the conversation_users node to only send push notifications if the user's a part of that conversation and also how to send push notifications to multiple devices since the code I have right now only sends it to my device using its specific fcmToken. Should I be subscribing users to topics, with those topics being the separate conversationIds? This is my first time using FCM, so any guidance would be greatly appreciated.



Database Structure:



 conversation_messages 
conversationId:
message1:
text: "Hello"
timestamp: 1095819823.123
senderId: aSLKN12KLSDNFK


,
conversation_users
conversationId:
userUID:1
userUID2:1

,
users
userUID:
fcmToken: "fcmToken"
bio: "bio"
username: "username"




Index.js code: Right now this code sends push notifications onto my device for any messages sent within the app. I'm looking to limit notifications to only the conversations the user has joined and to prevent push notifications for messages that I sent.



exports.observeMessages = functions.database.ref('/conversation_messages/convoId/messageId')
.onCreate((snapshot, context) =>

var convoId = context.params.convoId;
var messageId = context.params.messageId;
var message = snapshot.val();

return admin.database().ref('/conversations/' + convoId).once('value', snapshot =>

var conversation = snapshot.val(); // This gives us the conversation name

return admin.database().ref('/users/' + message.senderId).once('value', snapshot =>

var user = snapshot.val();

var notification =
notification:
title: conversation.conversationName,
body: user.username + ": " + message.text
,
token: 'dFlruzs91OM:APA91bHVx-Ga11GZ4fEwGAkrvFIegXPgPkvFrNuYa0U6yL_bdC7urM721SQGdnylpBNYPYMWAFpLbDk7pahe1RUMlQ8rN7iK7EAU89Xg6xpbIcBi6ZbTM7TB8N5zxPwhq0MTdIxIRH61'


admin.messaging().send(notification)
.then((response) =>
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response
)
.catch((error) =>
console.log('Error sending message:', error);
throw new Error("Error sending message");
);

)

)

)









share|improve this question













I am attempting to implement push notifications for a group chat app whose database structure is pasted below. I was wondering how to use the conversation_users node to only send push notifications if the user's a part of that conversation and also how to send push notifications to multiple devices since the code I have right now only sends it to my device using its specific fcmToken. Should I be subscribing users to topics, with those topics being the separate conversationIds? This is my first time using FCM, so any guidance would be greatly appreciated.



Database Structure:



 conversation_messages 
conversationId:
message1:
text: "Hello"
timestamp: 1095819823.123
senderId: aSLKN12KLSDNFK


,
conversation_users
conversationId:
userUID:1
userUID2:1

,
users
userUID:
fcmToken: "fcmToken"
bio: "bio"
username: "username"




Index.js code: Right now this code sends push notifications onto my device for any messages sent within the app. I'm looking to limit notifications to only the conversations the user has joined and to prevent push notifications for messages that I sent.



exports.observeMessages = functions.database.ref('/conversation_messages/convoId/messageId')
.onCreate((snapshot, context) =>

var convoId = context.params.convoId;
var messageId = context.params.messageId;
var message = snapshot.val();

return admin.database().ref('/conversations/' + convoId).once('value', snapshot =>

var conversation = snapshot.val(); // This gives us the conversation name

return admin.database().ref('/users/' + message.senderId).once('value', snapshot =>

var user = snapshot.val();

var notification =
notification:
title: conversation.conversationName,
body: user.username + ": " + message.text
,
token: 'dFlruzs91OM:APA91bHVx-Ga11GZ4fEwGAkrvFIegXPgPkvFrNuYa0U6yL_bdC7urM721SQGdnylpBNYPYMWAFpLbDk7pahe1RUMlQ8rN7iK7EAU89Xg6xpbIcBi6ZbTM7TB8N5zxPwhq0MTdIxIRH61'


admin.messaging().send(notification)
.then((response) =>
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response
)
.catch((error) =>
console.log('Error sending message:', error);
throw new Error("Error sending message");
);

)

)

)






ios firebase firebase-realtime-database firebase-cloud-messaging






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:08









Eric

4617




4617











  • What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
    – Johnykutty
    Nov 9 at 19:30
















  • What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
    – Johnykutty
    Nov 9 at 19:30















What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
– Johnykutty
Nov 9 at 19:30




What about all users subscribing to a topic(topic name can be group id or whatever), and if a message sent, send notification to that particular topic.
– Johnykutty
Nov 9 at 19:30

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231918%2fhow-to-set-up-fcm-push-notifications-for-group-chat-app%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231918%2fhow-to-set-up-fcm-push-notifications-for-group-chat-app%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo