Manage push notification device registrations to ensure users receive timely updates across all their devices. Handle registration, unregistration, and device lifecycle management with proper error handling and state management.
Device registration requires an active AmityClient instance and a valid push notification token. The SDK uses a “last write wins” strategy for device management.

Overview

Device registration is essential for delivering push notifications to your users. The social.plus SDK provides comprehensive device management capabilities across all platforms, ensuring reliable notification delivery while maintaining security and user privacy.

Key Concepts

  • Device Token: Unique identifier for each app installation (FCM token for Android, APNs token for iOS)
  • User Association: Each device can be associated with only one user at a time
  • Token Lifecycle: Tokens can change and require re-registration
  • Platform Differences: Each platform has unique token requirements and behaviors
Platform Setup Required: Complete the platform-specific push notification setup before implementing device registration:

Device Registration

Prerequisites

Before registering a device, ensure you have:

Active Client

AmityClient instance properly initialized and authenticated

Valid Token

Platform-specific push notification token obtained

User Permissions

Push notification permissions granted by user

Network Access

Active internet connection for registration

Platform-Specific Registration

do {
    let result = try await client.registerPushNotification(withDeviceToken: "<token>")
} catch {
    // Handle error here
}

Device Unregistration

When to Unregister

Unregister devices in these scenarios:

User Logout

When user logs out of the app

Account Deletion

When user deletes their account

Privacy Settings

When user disables notifications

App Uninstall

Before app uninstallation (if possible)

Unregistration Implementation

Unlike the registration, unregistering for push does not require the AmityClient instance to be associated with any user, therefore you can unregister the device from receiving push notifications as soon as the AmityClient has been initialized with a valid API key.
  • if a valid userId is passed, social.plus’s backend will stop sending push notifications to this device only if the currently active push notification associated with this device is also associated with that user. No action is taken otherwise.
  • if no userId is passed, social.plus’s backend will stop sending push notifications to this device.
// unregister from receiving push notifications for the user with id `userId`
client.unregisterDeviceForPushNotification(forUserId: userId) { [weak self] _, success, error in
  ...
}

// unregister from receiving push notifications for this device
client.unregisterDeviceForPushNotification(forUserId: nil) { [weak self] _, success, error in
  ...
}

Troubleshooting

Important: Always test device registration and token refresh scenarios thoroughly before production deployment. Each platform has unique requirements and edge cases.