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");
);
)
)
)
ios firebase firebase-realtime-database firebase-cloud-messaging
add a comment |
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");
);
)
)
)
ios firebase firebase-realtime-database firebase-cloud-messaging
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
add a comment |
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");
);
)
)
)
ios firebase firebase-realtime-database firebase-cloud-messaging
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
ios firebase firebase-realtime-database firebase-cloud-messaging
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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