I have successfully integrated push notifications for my app using Firebase Cloud Functions. Now, I am looking to enhance the user experience by updating the app's badge count alongside the push notifications. I understand that this can only be achieved through server-side code and not locally.
My current approach involves retrieving the number of new users from the server and using that number as the badge count in the push notification. However, I am facing challenges in implementing this solution. After spending considerable time trying to figure it out, I am seeking guidance to move forward.
My tech stack includes Firebase functions and Typescript in VSCode. The steps I plan to take are:
- Retrieve a list of userIDs from the 'admin' node
- Iterate over these userIDs on the 'user' node to check if the user's 'newUser' parameter is true
- Compile the results into an array
- Calculate the count of new users and use that as the badge count in the push notification
Here is the structure of my 'users' database:
"users": {
"2NBvNgdNRVe3nccTEDts2Xseboma": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0d3cfcdc5cfcec5e0d3cfcdc5cfcec58ec3cfcd">[email protected]</a>"
"newUser": "true",
"referral": "none",
...
},
"hjC6os6wzIV1FyULmGxalU3fM7ef": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f1edefe7edece7c7eef1e7c2f1edefe7edece7ace1edef">[email protected]</a>"
"newUser": "false",
"referral": "Bennett",
...
}
And this is how the 'admin' database is set up:
"admin": {
"2NBvNgdNRVe3nccTEDts2Xseboma": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6b5a9aba3a9a8a386b5a9aba3a9a8a3e8a5a9ab">[email protected]</a>"
"familyName": "Someone",
"memberSince": "1529119893",
},
"hjC6os6wzIV1FyULmGxalU3fM7ef": {
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abd8c4c6cec4c5ceeec7d8ceebd8c4c6cec4c5ce85c8c4c6">[email protected]</a>"
"familyName": "Someone Else",
"memberSince": "1529125722",
...
}
Below is the code snippet where I am struggling:
exports.getNewUserCount =
functions.database.ref('/users/{userID}/newUser')
.onUpdate((snapshot, _context) => {
console.log('test 2')
// Get a database reference
const db = admin.database();
const ref = db.ref('admin');
return ref.once('value', function(adminSnap) {
const userData = adminSnap.val()
console.log('admin key:', adminSnap.key)
console.log('user data:', userData)
})
});
I am currently stuck at step #1 of retrieving the list of users from the admin node. Any assistance would be greatly appreciated.
UPDATE
After managing to retrieve a snapshot of all users, I am now struggling with iterating over them. How can I convert the snapshot into an array of user keys?
Furthermore, once I have the list of user keys, how do I iterate over the 'users' node to identify the new users (step #2 above)?
Lastly, how can I compile these new users into an array (step #3 above) and calculate the count for the 'badge' parameter in the push notification (step #4 above)?
This process seems inefficient, and I am open to suggestions for improvement. Is there a more streamlined approach to identifying new users without going through the 'admin' node first? Any insights would be valuable as I have been grappling with this for days.
For additional context, I am proficient in Swift, and the app is designed for iOS. Thank you!
UPDATE #2
As an alternative approach, I decided to skip the 'admin' node altogether and directly retrieve a snapshot of all users. Here is the revised code:
const db = admin.database();
const ref = db.ref('users');
return ref.once('value').then((adminSnap) => {
console.log('admin key:', adminSnap.key)
// initialize an array to store user data
let newUserCount = 0;
// iterate over the snapshot to extract individual user data
adminSnap.forEach(function (userSnap) {
const userData = userSnap.val();
const userKey = userSnap.key
if (userData.newUser === true) {
newUserCount++
console.log('new user:', userKey, userData.newUser, userData.email)
}
});
console.log(newUserCount)
})
While this new code provides the required data for the badge parameter in the push notification, I am concerned about its efficiency. As the database grows, will this approach strain the server and potentially slow down the process? Moreover, could it lead to increased bandwidth costs for my Firebase account?
What started as a simple task has now become quite challenging. I am open to exploring different methods to achieve my goal. Any suggestions would be greatly appreciated. Thank you!