Push Notifications
Push notifications play a central role on your app user engagement.

Webhook

With this solution, live events are sent from Amity's servers to your servers. Once an event lands on your server, you have full power and control to do whatever you feel is best with each notification; including editing/removing/stopping the notification before it reaches your users devices.
In this scenario, there's no iOS SDK involvement needed. The whole notification process is managed on your end.

Direct Push Notifications

With this solution, the notifications will be triggered and delivered to your users directly by Amity's servers. There's nothing that the iOS client has to do in order to display the notification to your users. Amity's servers will prepare for you a notification that can be directly displayed to the user as and when it's received.

Push Notification Examples

As Amity's servers are responsible for choosing the content of the push notification, you can expect your users to receive the following notifications for different kinds of events:
    Event: New channel has been created and the user has been added among other members. Push Notification Title: %s (%s = New Channel display name) Push Notification Body: You're now member of %s! (%s = New Channel display name)
    Event: A new user has joined a channel. Push Notification Title: %s (%s = user display name) Push Notification Body: %1s has joined %2s (%1s = user display name, %2s = channel display name)
    Event: A new message has been received in a channel where the user is already a member. Push Notification Title: %1s (%2s) (%1s = user display name, %2s = channel display name) Push Notification Body: %s (%s = message text body if text message, Image Message if image message, Special message otherwise)

Push Notification Triggers

A new push notification will be sent to a specific user when:
    A new message is sent in a channel of the user who is already an existing member of it.
    A new channel is created and the user is among the listed members of the channel on creation.
    A new member joins a channel of the user who is already an existing member of it.

Client Registration

Registering your app for push notification will require a registered AmityClient instance (necessary to know which user is associated with this device) and a push notification token.
Amity's Development Kit does not manage:
    user-facing requests for push notifications and authorizations
    the creation and refreshing of push notification tokens
It's up to your app to take those steps and pass the notification token to the SDK.
Swift
Objective-C
1
// assume the client has been initialized with a valid API key and associated to a user
2
client.registerDeviceForPushNotification(withDeviceToken: token) { [weak self] success, error in
3
...
4
}
Copied!
1
// assume the client has been initialized with a valid API key and associated to a user
2
[self.client registerDeviceForPushNotificationWithDeviceToken:token completion:^(BOOL success, NSError * _Nullable error) {
3
...
4
}];
Copied!
We recommend observing the completion block outcome to ensure a successful registration.
If the device was previously registered with this or another user, the previous registration is invalidated as soon as this new request is received, which means that the device will always receive notifications of up to one user.

Client Unregistration

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.

The userId Parameter

The unregistration allows to pass an optional userId:
    if a valid userId is passed, Amity'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, Amity's backend will stop sending push notifications to this device.
Swift
Objective-C
1
// assume the client has been initialized with a valid API key
2
// unregister from receiving push notifications for the user with id `userId`
3
client.unregisterDeviceForPushNotification(forUserId: userId) { [weak self] _, success, error in
4
...
5
}
6
7
// unregister from receiving push notifications for this device
8
client.unregisterDeviceForPushNotification(forUserId: nil) { [weak self] _, success, error in
9
...
10
}
Copied!
1
// assume the client has been initialized with a valid API key
2
// unregister from receiving push notifications for the user with id `userId`
3
[self.client unregisterDevicePushNotificationForUserId:userId completion:^(NSString * _Nullable, BOOL success, NSError * _Nullable error) {
4
...
5
}];
6
7
// unregister from receiving push notifications for this device
8
[self.client unregisterDevicePushNotificationForUserId:nil completion:^(NSString * _Nullable, BOOL success, NSError * _Nullable error) {
9
...
10
}];
Copied!
You can register and unregister as many times as you'd like, however please remember that we use the "Last write wins" strategy.

Client Push Notification Toggles

The SDK has three levels of notifications and in order for it to be sent, a notification has to pass throughout all three levels.
    Network Level: (via Admin Panel) turning off notifications at this level effectively disable push notifications altogether for all of your customers.
    User Level: (via client) A user can choose to enable/disable the notifications that it receives on the device (this is an absolute option: enable all or disable all). Please note that this setting is per user, not per device: regardless of which device sets this toggle, the new preference will take effect in all the devices where the user is logged in.
    Channel Level: (via client) A user can choose to enable/disable notifications for a specific channel (where is member of). Again, this preference is per user, not per device.

User Level Push Notification Toggle

In order to get and set the user level push notifications preference, we use the object AmityUserNotificationsManager, obtained from the current AmityClient:
Swift
Objective-C
1
let pushNotificationManager = client.notificationManager
2
3
// enable preference
4
pushNotificationManager.enable(for: modules) { (success, error) in
5
...
6
}
7
8
// disable preference
9
pushNotificationManager.disable { success, error in
10
...
11
}
Copied!
1
EkoUserNotificationsManager *notificationManager = self.client.notificationManager;
2
3
// get preference
4
[notificationManager isAllowedWithCompletion:^(BOOL isAllowed, NSError * _Nullable) {
5
// isAllowed = YES => user level enabled
6
// isAllowed = NO => user level disabled
7
...
8
}];
9
10
// set preference
11
[notificationManager setIsAllowed:YES completion:^(BOOL success, NSError * _Nullable error) {
12
...
13
}];
Copied!

Channel Level Push Notification Toggle

For channel preferences, we use the ChannelLevelPushNotificationManager instead, obtained via an instance of AmityChannelRepository:
Swift
Swift
Objective-C
1
let channelRepository = AmityChannelRepository(client: client)
2
let pushNotificationManager = channelRepository.notificationManagerForChannel(withId: channelId)
3
4
// get notification settings
5
pushNotificationManager.getSettings { (settings, error) in
6
...
7
}
8
9
// enable preference
10
pushNotificationManager.enable { success, error in
11
...
12
}
13
14
// disable preference
15
pushNotificationManager.disable { success, error in
16
...
17
}
Copied!
1
EkoChannelRepository *channelRepository = [[EkoChannelRepository alloc] initWithClient:self.client];
2
EkoChannelNotificationsManager *notificationManager = [channelRepository notificationManagerForChannelId:channelId];
3
4
// get preference
5
[notificationManager isAllowedWithCompletion:^(BOOL isAllowed, NSError * _Nullable) {
6
// isAllowed = YES => user level enabled
7
// isAllowed = NO => user level disabled
8
...
9
}];
10
11
// set preference
12
[notificationManager setIsAllowed:YES completion:^(BOOL success, NSError * _Nullable error) {
13
...
14
}];
Copied!
Last modified 1mo ago