Push Notifications
Push Notifications are an integral part of an engaging app experience. Registering an App for a push notification is rather straight forward.
Apple Devices
Once you're ready to setup Remote Notifications call the following code in your application:
UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
UIApplication.shared.registerForRemoteNotifications()
If the user accepts the prompt then the following AppDelegate method will be fire. With any synced realm
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Convert token to string
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
SyncUser.current?.registerAPNSDevice(deviceTokenString)
}
You can always unregister your current user. This is great for apps that want to remotely disable their other devices. A situation where this is relevant is if you have downloaded a naughty app on your iPad and iPhone and accidentally forgot your iPad at home. With a click of a button, you can silence further pushes to your iPad, keeping your secrets to yourself. 😈
SyncUser.current?.unregisterThisAPNSDevice()
// or you can call
SyncUser.current?.unregisterAPNSDevices([deviceToken1, deviceToken2])
// or you can call
SyncUser.current?.unregisterAllAPNSDevices()
If you call SyncUser.current?.logout() , this will also call SyncUser.current?.unregisterThisAPNSDevice()
Sending a Push Notification
We give you full control over how to send push notifications within the user base.
let notification = Realm.Sync.RemoteNotification()
notification.badge = 3
notification.userId = "someUserId"
notification.payload = [
"sound": "notification.aiff",
... other json values
]
// or
Realm.Sync.SendRemoteNotification(notification)
Sometimes you'd only want to send messages to specific devices. For example, a user may have 2 iPads, one iPhone but you'd only like to send to the specific device.
let notification = Realm.Sync.RemoteNotification()
notification.badge = 3
notification.deviceId = '1394832930ae2'
Realm.Sync.SendRemoteNotification(notification)