This is tested using Firebase Realtime Database, not with Firestore. Before all of these, I assume you have already been using the realtime database. More importantly, you already have the GoogleService-Info.plist in your Xcode project. It is important because it contains your bundle identifier. Without Firebase knowing your bundle identifier, you can’t proceed to enable Cloud messaging.

Let’s get this one thing straight. Push notification a.ka. Remote notification. We want to have a Push Notification on our device as a response to a change in the firebase database, and in order to do that, I’m using cloud functions. Cloud functions is a feature of Firebase that allows you to listen to change/s in the firebase database and trigger a response. The response we want is to send a notification. In Firebase’s term, cloud messaging. That’s a confusing term to interchange with remote notification . I know.

Install this pod libraries first in addition to your preinstalled libraries with Firebase.

pod 'Firebase/Analytics' pod 'Firebase/Core' pod 'Firebase/Messaging'

In your Xcode project’s signing and capabilities, enable or add capabilities:

Background Modes -> Remote Notifications Push Notifications

Let’s create a certificate signing request by going to Keychain Access.

On the toolbar, click Keychain Access beside File In this dropdown menu, Certificate Assistant -> Request a Certificate From a Certificate Authority Enter your credentials into the text fields and choose Save to disk .

Then go to Apple Developer. For this, you need a paid developer account which costs $99.

Open Certificates, Identifiers & Profiles Navigate to Identifiers Click on the current project you are working on. Beside the Push Notifications, click Configure . Depending on which environment you are, click Create Certificate Choose a file -> Upload the CertificateSigningRequest.certSigningRequest you just created. Continue and download aps_development.cer.

Once that is done, you will have a .cert file on your desktop. Now let’s convert that to .p12. Still, using the Keychain Access.

File -> Import Items -> Select your .cert file Drag that file to System , assuming you are in login in the Keychains sidebar. Drag it back from System to login . You now have a duplicate, but you’ll only need the most recent. Select My Certificates in the Category. Select the most recent certificate pertaining to your project, and right-click. Click Export “Apple Development IOS Push Services: your.project…” Make sure to export it in .p12 format. It will then ask you to create a password to protect this certficate. Give it one and click ok .

In order to test this while under development, you need a Provisioning Profile for development to authorize your devices to run an app that is not yet published on the App Store.

Open Certificates, Identifiers & Profiles Select Profiles Generate a Profile Select iOS App Development and continue Select your App ID and continue Select the iOS Development certificate of the App ID you have chosen in the previous step, then click Continue. Select the iOS devices that you want to include in the Provisioning Profile, then click Continue. Make sure to select all the devices you want to use for your testing. Name the provisioning profile whatever you want and generate. Download the provisioning profile and open it to install. If it crashes your Xcode, just go to your Xcode project settings -> Signing & Capabilities -> Disable Automatically Manage Signing -> Import the provisioning profile.

To enable the cloud messaging feature, go to https://console.cloud.google.com/ -> Search for your Firebase project and select it -> APIs and Services -> + ENABLE APIS AND SERVICES -> Search for Firebase Cloud Messaging API -> click Enable.

In your Firebase console, navigate to Project Settings -> Cloud Messaging -> iOS app configuration -> APNs Certificates -> Upload the .p12 file here, then enter the password for this certificate.

That’s all you need for now to allow Cloud Messaging on your iOS device. It’s like a term for Remote Push Notifications. I was baffled at that at first.

Now this is the part where we detect changes in the firebase database. If you haven’t installed the firebase CLI yet, please do so.

Create a directory where you want to put the cloud functions code. In my case, I created triggers directory under my Xcode project directory. Laundry City/triggers ‘cd triggers` In the terminal, type firebase init functions , but make sure you’re logged in with firebase cli. Choose javascript and type y as in yes to any remaining questions from the cli. Open index.js and create your function such as below:

// index.js const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.sendNewChangesNotification = functions.database.ref('houses/{house}/items/{item}/').onWrite((change, context) => { var topic = "Serenity"; // A message that contains the notification that devices will receive var message = { notification: { title: 'New Quantity', body: change.after.val().product + ' now has ' + change.after.val().tentative + ' new items.' } }; // Using Cloud Messaging to create notification return admin.messaging().sendToTopic(topic, message).then(function (response) { console.log('Successfully sent message:', response); return null; }).catch(function (error) { throw new Error("Error sending message:", error); }); })

First thing to note here is, you won’t start your ref with the Project ID.. Instead, the branch below it. The ones in the brackets are ids , which varies. This will detect any write or changes happening in that reference. It will catch the changes in the change parameter. Thereby, allowing access to the new tentative value in change.after.val().tentative from the reference houses/1/items/2/tentative . 1 and 2 here are just examples of IDs. sendNewChangesNotification is just a variable. You can change it however you like.

When everything is in place, go back to terminal and deploy it with firebase deploy . Now, you have a listener or a trigger whenever your items have been changed or added.

Your AppDelegate.swift should look like this. This firebase sample is as instructed in the documentation.

To send a notification, admin.messaging.send will do the trick. But you have to provide a token , topic , or a condition . Token is generated by the AppDelegate and is uploaded to Firebase automatically. If you specify a specific token, then only the device with that token will receive the notification. Topic is what its name suggests. To subscribe to a topic, add this to wherever you need to put in your Xcode project:

Messaging.messaging().subscribe(toTopic: "Serenity") { error in print("Subscribed to weather topic") }