How can I go about setting up push notifications using firebase cloud messaging and cloud functions? - ios

I'm been coding for a year now and know swift really well and the basics to javascript. I've built a social media like app and its completely done besides the push notification functionality... which i've been struggling with for quite a while now.
I'm having trouble figuring out how to implement these in order to send notifications like when a user follows another user or they receive a new messege.
I don't understand how to integrate the cloud functions that I would write using and node.js with my xcode project.
When I look for documentation online its usually just how to send push notifications using the firebase notifications console which is cool but doesn't trigger based off specific user actions or events (ex: when the user gets a new follower)
I think since im struggling with grasping the concept of how to implement it, the actual technical set up of it makes even less sense. Any advice or resources would be greatly appreciated.
Thank you

Conceptually, what you have to do is set up server-side processing of your database to manage when events occur. This is what the Node.js code will be. Everything is connected through the database-- your Xcode and Node.js code work independently, but both communicate with Firebase. You can create a Cloud Function that will automatically run when it detects changes in your database, and this will automatically send push notifications.
I've done a similar project-- I set up a listener for my Realtime Database in my Cloud Function. Every time the user got a new follower, i.e. their followers tree was updated, I sent a notification to the device using an FCM token (which I also stored in the database).
exports.onTimeEnd = functions.database.ref("users/{user}/followers")
.onUpdate((snapshot, context) => {
// your code here
}
In that block, you can call a .once() to find out the user's token to send them a notification with.
https://firebase.google.com/docs/functions/get-started
This link here told me everything I had to do in regards to set up in the terminal. Super helpful.

Related

iOS push notifications with Firebase FCM to a specific user

I am trying to send notifications to users when something happens in their account, for example a new friend request or follower, or a tag in an image. I have been looking into Firebase FCM to make this happen but can't figure out how to send push notifications to just 1 user, based on some activity in their account.
Is there any way this can be done in FCM or are there are any other methods out there to make this happen? I'm new to iOS dev and push notifications, so any help will be greatly appreciated!
Thanks!
PS: If I do need to make my own server for this, could someone point me to any resources to do this in python? I've seen quite a few posts for APNs servers in PHP, but I don't know PHP that well.
The easiest way is to use Firebase Cloud Functions , I have experience working with it using Javascript then trigger the function when something changes in database. You can also send a http request with the specific device token which you want a push notification to be delivered to, then the function will do it's magic for you. Firebase have ready to use samples from their Github repo.
NB: - For your case if you really want to use Python then check this answer it might help you get started.

How do I get notifications sent to each device when a child is added, using Swift and Firebase?

I have a fully functioning application that uses Firebase as a backend. I want to be able to have a user receive a notification when a child is added to my Firebase database under that users ID.
I have looked everywhere but all I can find are links to OneSignal or people telling me to "make a custom server" as if it can be done by magic. How do I go about making a server? What language? What do I do with OneSignal? Can someone guide me step by step without telling me to simple make a custom server.
I believe Cloud Functions for Firebase is exactly what you're looking for. Specifically, Realtime Database Triggers:
The Realtime Database supports the onWrite() event, which triggers anytime data is created, destroyed, or changed in a specified database location.
In a typical lifecycle, a Firebase Realtime Database function does the following:
Waits for changes to a particular database location for write events.
Fires when a write event occurs and performs its tasks (see What can I do with Cloud Functions? for examples of use cases).
Receives an event data object that contains two snapshots of the data stored at the specified path: one with the original data prior to the change, and one with the new data.
And going through the What can I do with Cloud Functions?, theres Notifying users:
Notify users when something interesting happens
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. In such an app, a function triggered by Realtime Database writes to store new followers could create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.
Other interesting notification use cases
Send confirmation emails to users subscribing/unsubscribing to a newsletter.
Send a welcome email when a user completes signup.
Send an SMS confirmation when a user creates a new account.

user to user push notifications

I have seen many questions on here and online that address this topic, but they all are from a long time ago, and are possibly outdated. I wanted to get a more recent answer if Firebase has implemented anything recently that is capable of a user to user push notification system, essentially what is used in all chat applications, or if the best option is still currently using Onesignal.
Yes! These days, you can use Cloud Functions for Firebase to do things like send a notification when something interesting happens on the Database side of things.
Here's an example where a user gets notified via notifications when they gain a new follower. Obviously, your part of the database portion will probably change depending on what exactly you want to trigger a notification, but the general theory is the same.

How can I send Parse push notifications to users after specific user action?

I have read a lot of tutorials and doc but I cannot quite wrap my head around it: I am trying to send users of my app a notification when somebody posts something new or if someone likes one of their posts (think Facebook-style table view displaying notifications as they happen). The certificates are set up and the app is able to receive remote notifications that I send through the Parse push console.
Obviously I want to send a notification as the actions happen, but I am unsure how to do that. Parse suggests using cloud code, but it seems to be in Javascript. Is there a way to do it in Swift? My first idea was to send the push from the users' devices as they interact (when the user likes a pic from you, he sends the push and you receive it) but apparently, it is discouraged as it is unsafe.
I understand my question is general, but I am lost as to how I can tackle this issue: can anybody give me an outline of how this should be dealt with? Not necessarily precise code, but a general idea of what I should do?

Consuming external json with parse.com

I'm working on an iOS application developed with Swift. The application is going to need notifications to receive alerts when certain events occur.
To handle push notifications I'm using parse.com. I was able to create an account and integrate push notifications into my application, as it's explained in the Parse.com guide, and are working fine when I send something from the Push section.
What I'm trying to do, and I cannot figure it out, is consume a 3rd party JSON, process that JSON to check some status and report, to the users that are using the application, a status change via a push notification. I know I should process all this in the backend/server side and then push the result or what I'm trying to notify, but I don't know what to do.
Ex. of What I'm trying to archive:
3rd party website ---(json)---> parse.com ---(push notification)--->
My App
I'm aware the tools that Parse.com provides, such as REST API, Cloud Code, Webhooks and Jobs, but I'm totally lost here! I cannot find any guide or documentation that allows me to do what I previously described. I don't even know if its possible, and the documentation at parse.com it's not necesarily up to date.
I hope anyone can help me, or guide me, if parse.com it's not enought for my purpouse.
Thanks for reading.
I would suggest looking into Cloud Code or a Job from Parse and seeing if you can get one to run on a time interval to look for updated JSON.
You can decode JSON using JS, which is exactly what Parse Cloud Code is, you actually write it out on your own computer and then upload it. I've only use the afterSave functions. I would think you probably have to keep some type of object in parse to see what is updated and notify who ever needs to notified when that data is updated.
Also, you'll probably want to pass something to the app to actually update when the user selects the push alert. You can do this in the push alert itself. It converts to a dictionary in the app delegate's method for handling received notifications or launching with one.
Alternatively, if the web server is under your control, you can create a PHP script that will trigger the push to parse.
My best shot at this (after not having used Parse for a long while) would be to set up a Job on Parse that queries the 3rd-party service, and takes that response and throws it into a Cloud Code function, which in turn parses out that JSON and sends off a notification.
Parse's reference materials would probably be a good starting point.
The toughest part for you is the up-to-date-ness of your data. Since you're still polling that 3rd-party service at a regular interval, it's going to be a trade-off between freshness of updates and frequency of jobs (which cost money at a certain scale).

Resources