Skip to content

Messaging

Send push notification_

Send push notification to your users using Appwrite Messaging.

5 min read

Raw

You can send, schedule, and manage push notifications to your apps using Appwrite Messaging. Push notifications can be used to deliver new message notifications, app updates, promotional offers, and other messages straight to your user's devices.

1. Add provider

Push notifications must be sent through third-party providers, like Apple Push Notification service and Firebase Cloud Messaging. The push notification APIs for Apple and Android devices can only be accessed through these services.

You must configure these services before you can send your first push notification.

2. Add targets

Before sending your first push notification, your application must register itself for push notification, then provide the device token to Appwrite.

First, enable push notification in your app. Add push notification capability to your app by clicking your root-level app in XCode > Signing & Capabilities > Capabilities > Search for Push Notifications.

Authentication Key
Authentication Key

First, register for remote notifications in your app delegate's application(_:didFinishLaunchingWithOptions:) method.

Swift
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, _ in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
return true
}

Next, create a handler for when the app receives the push notification device token.

Swift
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
/* store this `token` */
let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
}

Since the token is saved in UserDefaults, you can access it from anywhere in your app. With this saved apnsToken, you can create a push target with Appwrite when the user logs in. Each push target is associated with an account, heres an example with an email password login. The same logic applies to all types of login methods.

Swift
func login() async {
do {
let session = try await account.createEmailPasswordSession(email: username, password: password)
let token = /* Retrieve the stored push token */
try await account.createPushTarget({
targetId: ID.unique(),
identifier: token
})
} catch {
print("Login failed: \(error.localizedDescription)")
}
}

3. Request permissions

Your app must ask for permission to receive push notification from the user.

Before your app can receive push notifications, you need to request the user for permissions. Appwrite provides a utility to help request permissions to display notificaitons.

You can learn more about requesting permissions from the Apple Developer Documentation.

4. Send message

You can send messages in both the Appwrite Console and programmatically using the Appwrite Server SDK.

To send a test message, navigate to Messaging > Messages > Create message > Push notification.

Create email message
Create email message

Add your message and in the targets step, select one of your test targets. Set the schedule to Now and click Send.

Verify that you can receive the message in your inbox. If not, check for logs in the Appwrite Console or in your provider's logs.

Was this page helpful?

Share what worked or what we should fix. Once approved, our agents automatically apply suggested updates to the docs.