Skip to content

Commit 81c3ed1

Browse files
authored
Add new Push v3 notification types (#3708)
* Add new Push v3 notification types * Update CHANGELOG.md * Make it more flexible * Remove isKnown logic
1 parent 1c9c7de commit 81c3ed1

File tree

4 files changed

+65
-25
lines changed

4 files changed

+65
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## StreamChat
77
### ✅ Added
8+
- Add support for Push v3 notification types [#3708](https://github.com/GetStream/stream-chat-swift/pull/3708)
89
- Add `ChatMessageController.partialUpdateMessage()` [#3531](https://github.com/GetStream/stream-chat-swift/pull/3531)
910
- Add Location Sharing Support [#3531](https://github.com/GetStream/stream-chat-swift/pull/3531)
1011
- Add `ChatMessage.sharedLocation`

DemoAppPush/NotificationService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class NotificationService: UNNotificationServiceExtension {
9696
let chatNotification = chatHandler.handleNotification { chatContent in
9797
switch chatContent {
9898
case let .message(messageNotification):
99-
if messageNotification.type == .reminderDue {
99+
if messageNotification.type == .messageReminderDue || messageNotification.type == .reactionNew {
100100
return contentHandler(content)
101101
}
102102

Sources/StreamChat/APIClient/ChatRemoteNotificationHandler.swift

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@ public class MessageNotificationContent {
2222
}
2323
}
2424

25+
/// The type of push notifications supported by the Stream Chat SDK.
2526
public struct PushNotificationType: Equatable {
2627
public var name: String
2728

28-
init(name: String) {
29-
self.name = name
29+
init(eventType: EventType) {
30+
name = eventType.rawValue
3031
}
3132

32-
init?(eventType: EventType) {
33-
switch eventType {
34-
case .messageNew, .messageReminderDue:
35-
self.init(name: eventType.rawValue)
36-
default:
37-
return nil
38-
}
39-
}
40-
41-
public static var newMessage: PushNotificationType = .init(name: EventType.messageNew.rawValue)
42-
public static var reminderDue: PushNotificationType = .init(name: EventType.messageReminderDue.rawValue)
33+
/// When the push notification is for a new message.
34+
public static var messageNew: PushNotificationType = .init(eventType: .messageNew)
35+
/// When the push notification is for a message reminder that is overdue.
36+
public static var messageReminderDue: PushNotificationType = .init(eventType: .messageReminderDue)
37+
/// When the push notification is for a message that has been updated.
38+
public static var messageUpdated: PushNotificationType = .init(eventType: .messageUpdated)
39+
/// When the push notification is for a new reaction.
40+
public static var reactionNew: PushNotificationType = .init(eventType: .reactionNew)
4341
}
4442

4543
public class UnknownNotificationContent {
@@ -120,24 +118,23 @@ public class ChatRemoteNotificationHandler {
120118
return completion(.unknown(UnknownNotificationContent(content: content)))
121119
}
122120

123-
guard let type = dict["type"] else {
124-
return completion(.unknown(UnknownNotificationContent(content: content)))
125-
}
126-
127-
guard let pushType = PushNotificationType(eventType: EventType(rawValue: type)) else {
128-
return completion(.unknown(UnknownNotificationContent(content: content)))
129-
}
130-
131121
guard let cid = dict["cid"], let id = dict["id"], let channelId = try? ChannelId(cid: cid) else {
132122
completion(.unknown(UnknownNotificationContent(content: content)))
133123
return
134124
}
135125

126+
guard let type = dict["type"] else {
127+
return completion(.unknown(UnknownNotificationContent(content: content)))
128+
}
129+
130+
let pushType = PushNotificationType(eventType: EventType(rawValue: type))
131+
136132
getContent(cid: channelId, messageId: id) { message, channel in
137133
guard let message = message else {
138134
completion(.unknown(UnknownNotificationContent(content: self.content)))
139135
return
140136
}
137+
let pushType = PushNotificationType(eventType: EventType(rawValue: type))
141138
let content = MessageNotificationContent(
142139
message: message,
143140
channel: channel,

Tests/StreamChatTests/APIClient/ChatRemoteNotificationHandler_Tests.swift

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,55 @@ final class ChatRemoteNotificationHandler_Tests: XCTestCase {
246246
XCTAssertEqual(false, channelRepository.getChannel_store)
247247
XCTAssertEqual(false, messageRepository.getMessage_store)
248248
}
249-
249+
250+
func test_handleNotification_supportedPushNotificationTypes() throws {
251+
let cid = ChannelId.unique
252+
let expectation = XCTestExpectation()
253+
let expectedChannel = ChatChannel.mock(cid: cid)
254+
let expectedMessage = ChatMessage.mock()
255+
channelRepository.getChannel_result = .success(expectedChannel)
256+
messageRepository.getMessageResult = .success(expectedMessage)
257+
258+
let notificationTypes: [String: PushNotificationType] = [
259+
"message.new": .messageNew,
260+
"reaction.new": .reactionNew,
261+
"notification.reminder_due": .messageReminderDue,
262+
"message.updated": .messageUpdated
263+
]
264+
265+
var assertions: [Bool] = []
266+
expectation.expectedFulfillmentCount = notificationTypes.count
267+
268+
for notificationType in notificationTypes {
269+
let content = createNotificationContent(
270+
cid: expectedChannel.cid,
271+
messageId: expectedMessage.id,
272+
type: notificationType.key
273+
)
274+
let handler = ChatRemoteNotificationHandler(client: clientWithOffline, content: content)
275+
let canHandle = handler.handleNotification { pushNotificationContent in
276+
switch pushNotificationContent {
277+
case .message(let messageNotificationContent):
278+
assertions.append(messageNotificationContent.type == notificationType.value)
279+
case .unknown(let unknownNotificationContent):
280+
XCTFail(unknownNotificationContent.content.debugDescription)
281+
}
282+
expectation.fulfill()
283+
}
284+
285+
XCTAssertEqual(true, canHandle)
286+
}
287+
288+
wait(for: [expectation], timeout: defaultTimeout)
289+
XCTAssertEqual(assertions, Array(repeatElement(true, count: notificationTypes.count)))
290+
}
291+
250292
// MARK: -
251293

252-
func createNotificationContent(cid: ChannelId, messageId: MessageId) -> UNNotificationContent {
294+
func createNotificationContent(cid: ChannelId, messageId: MessageId, type: String = "message.new") -> UNNotificationContent {
253295
let content = UNMutableNotificationContent()
254296
let payload: [String: String] = [
255-
"type": "message.new",
297+
"type": type,
256298
"cid": cid.rawValue,
257299
"id": messageId
258300
]

0 commit comments

Comments
 (0)