/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Push notification preferences

Copy link

By registering or unregistering the current user's registration token to the Sendbird server as below, you can turn push notifications on or off in the user's app. The registration and unregistration methods in the code below should be called after a user has established a connection with the Sendbird server via the connection methods such as connect(userId:completionHandler:) or connect(userId:authToken:completionHandler:).

Through setPushTriggerOption, the current user can set which message can trigger a push notification or completely turn it off as well. Besides, they can enable the Do Not Disturb (DND) and snooze features through setDoNotDisturb and setSnoozePeriod methods.

func setPushNotification(enable: Bool) {
    guard let token = SendbirdChat.getPendingPushToken() else { return }

    if enable {
        SendbirdChat.registerDevicePushToken(token, unique: false) { status, error in
            guard error == nil else {
                // Handle error.
                return
            }
        }
    }
    else {
        // If you want to unregister the current device only,
        // invoke this method.
        SendbirdChat.unregisterPushToken(token) { response, error in
            guard error == nil else {
                // Handle error.
                return
            }
        }

        // If you want to unregister the all devices of the user,
        // invoke this method.
        SendbirdChat.unregisterAllPushToken { (response, error) in
            guard error == nil else {
                // Handle error.
                return
            }
        }
    }
}

The setPushTriggerOption(_:completionHandler:) setter allows users to configure the type of the message to trigger notifications for all group channels on the application level. The following three options are provided.

List of push trigger options

Copy link
OptionDescription

all

When disconnected from the Sendbird server, the current user receives notifications for all new messages including messages the user has been mentioned in.

mentionOnly

When disconnected from the Sendbird server, the current user only receives notifications for messages the user has been mentioned in.

off

The current user doesn't receive any notifications.

// Send notifications only when the current user is mentioned in messages.
SendbirdChat.setPushTriggerOption(.mentionOnly) { error in
    guard error == nil else {
        // Handle error.
        return
    }
}

The setMyPushTriggerOption setter also allows users to configure the type of the message to trigger notifications for each group channel. The following four options are provided.

OptionDescription

default

The current user’s push notification trigger settings are automatically applied to the channel. This is the default setting.

all

When disconnected from the Sendbird server, the current user receives notifications for all new messages in the channel including messages the user has been mentioned in.

mentionOnly

When disconnected from the Sendbird server, the current user only receives notifications for messages in the channel the user has been mentioned in.

off

The current user doesn't receive any notifications in the channel.

// Apply the user's push notification setting to the channel.
channel.setMyPushTriggerOption(.default) { error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Do Not Disturb

Copy link

If you want to routinely turn off push notifications on the current user's client app according to a specified schedule, use the Do Not Disturb feature.

When evaluating whether to suppress a notification, the server checks in the following priority order:

  1. Snooze: If snooze is active, notifications are paused regardless of other settings.
  2. Day-of-week DND schedules: If weekly DND schedules are configured, the server checks the current time against the configured windows.
  3. Legacy DND: If no weekly DND schedules exist, the legacy setDoNotDisturb() settings are used.

Day-of-week DND schedules and legacy DND are mutually exclusive — setting weekly DND schedules automatically disables legacy DND.

Note: The setDoNotDisturb() and getDoNotDisturb() methods have been deprecated. Use setWeeklyDoNotDisturb() and getWeeklyDoNotDisturb() instead for more flexible scheduling options.

Note: Do Not Disturb can also be set for a user with the Chat Platform API's update push preferences action.

// Deprecated: Use setWeeklyDoNotDisturb() instead.
// The current user won't receive notifications
// during the specified time of every day.
SendbirdChat.setDoNotDisturb(enable: true, startHour: START_HOUR, startMin: START_MIN, endHour: END_HOUR, endMin: END_MIN, timezone: TIMEZONE) { error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Weekly Do Not Disturb

Copy link

Weekly Do Not Disturb allows you to set different Do Not Disturb time windows for each day of the week. This replaces the single-window setDoNotDisturb() method with more flexible scheduling.

Note: When evaluating whether to suppress a notification, the server checks snooze first, then weekly DND schedules, then legacy DND. Setting weekly DND schedules automatically disables legacy DND.

Set weekly Do Not Disturb schedules

Copy link
// Create DND schedules for weekdays.
let weekdaySchedules = DndDayEntry.weekdays([
    DndTimeWindow(startHour: 22, startMinute: 0, endHour: 23, endMinute: 59)
])

// Create schedule for specific days.
let mondaySchedule = DndDayEntry(day: .monday, timeWindows: [
    DndTimeWindow(startHour: 9, startMinute: 0, endHour: 17, endMinute: 0)
])

let allSchedules = weekdaySchedules + [mondaySchedule]

SendbirdChat.setWeeklyDoNotDisturb(schedules: allSchedules, timezone: .current) { error in
    guard error == nil else {
        // Handle error.
        return
    }

    // Weekly DND schedules successfully set.
}

Cross-midnight windows

Copy link

A DndTimeWindow only accepts time ranges within a single day, where startHour:startMinute must be strictly before endHour:endMinute. To create a DND window that spans midnight, you need to manually split it into two separate day entries.

For example, Monday 22:00 to Tuesday 07:00 should be split into Monday 22:00–23:59 and Tuesday 00:00–07:00.

Get weekly Do Not Disturb schedules

Copy link

Retrieve the current weekly Do Not Disturb configuration for the user.

SendbirdChat.getWeeklyDoNotDisturb { schedules, timeZone, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // Process the retrieved schedules.
    for schedule in schedules {
        print("Day: \(schedule.day), Time windows: \(schedule.timeWindows)")
    }
}

Clear weekly Do Not Disturb schedules

Copy link

Remove all weekly Do Not Disturb schedules for the user.

SendbirdChat.clearWeeklyDoNotDisturb { error in
    guard error == nil else {
        // Handle error.
        return
    }

    // Weekly DND schedules successfully cleared.
}

Snooze

Copy link

To snooze notifications for a specific period of time, use the snooze feature.

Note: Snooze can also be set for a user with the Chat Platform API's update push preferences action.

// The current user won't receive notifications
// from START_TS to END_TS.
SendbirdChat.setSnoozePeriod(enabled: true, startTimestamp: START_TS, endTimestamp: END_TS) { error in
    guard error == nil else {
        // Handle error.
        return
    }
}