Try push notifications with the iOS example XMTP app
This guide describes how to set up push notifications for the XMTP iOS example app built with the xmtp-ios SDK using Firebase Cloud Messaging (FCM) and a custom notification server.
Perform this setup to understand how you can enable push notifications for your own app built with the xmtp-ios
SDK.
Prerequisites
- An iOS device for testing. Push notifications don't work on simulators
- A Firebase account and a project set up in the Firebase console
Set up Firebase Cloud Messaging
For this tutorial, we'll use Firebase Cloud Messaging (FCM) as a convenient way to set up a messaging server.
-
Create an FCM project
Go to the Firebase Console, create a new project, and follow the setup instructions. -
Add your app to the FCM project
Add your iOS app to the project by following the Firebase setup workflow. You'll need your app's bundle ID. -
Download
GoogleService-Info.plist
At the end of the setup, download theGoogleService-Info.plist
file and add it to your Xcode project. -
Generate FCM credentials
In the Firebase console, navigate to your project settings, select the Cloud Messaging tab, and note your server key and sender ID. You'll need these for your notification server.
Configure the iOS example app for push notifications
-
Enable push notifications
In Xcode, go to your project's target capabilities and enable push notifications. -
Register for notifications
Modify theAppDelegate
to register for remote notifications and handle the device token. -
Handle incoming notifications
Implement the necessary delegate methods to handle incoming notifications and foreground notification display.
Run the notification server
-
Clone and configure the notification server
If you're using the example notification server, clone the repository and follow the setup instructions. Make sure to configure it with your FCM server key. -
Run the server
Start the server locally or deploy it to a hosting service.
-
Subscribe to push notifications in the app
When initializing the XMTP client in your app, subscribe to push notifications using the device token obtained during registration. -
Decode a notification envelope
When you receive a push notification, you may want to decode the notification envelope to display a message preview or other information.