Skip to content

Commit fc53bf2

Browse files
authored
Merge pull request #1024 from OneSignal/feature/collapse_id_on_osnotification
Adding Collapse id to OSNotification
2 parents 85920cd + dccc4e1 commit fc53bf2

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

iOS_SDK/OneSignalSDK/Source/OSNotification.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ - (void)parseCommonOneSignalFields:(NSDictionary*)payload {
130130
_templateId = payload[@"ti"];
131131
_templateName = payload[@"tn"];
132132
_badgeIncrement = [payload[@"badge_inc"] integerValue];
133+
_collapseId = payload[@"collapse_id"];
133134
}
134135

135136
- (void)parseApnsFields {
@@ -216,7 +217,7 @@ - (void)parseOtherApnsFields {
216217
}
217218

218219
- (NSString *)description {
219-
return [NSString stringWithFormat: @"notificationId=%@ templateId=%@ templateName=%@ contentAvailable=%@ mutableContent=%@ category=%@ relevanceScore=%@ interruptionLevel=%@ rawPayload=%@", _notificationId, _templateId, _templateName, _contentAvailable ? @"YES" : @"NO", _mutableContent ? @"YES" : @"NO", _category, _relevanceScore, _interruptionLevel, _rawPayload];
220+
return [NSString stringWithFormat: @"notificationId=%@ templateId=%@ templateName=%@ contentAvailable=%@ mutableContent=%@ category=%@ relevanceScore=%@ interruptionLevel=%@ collapseId=%@ rawPayload=%@", _notificationId, _templateId, _templateName, _contentAvailable ? @"YES" : @"NO", _mutableContent ? @"YES" : @"NO", _category, _relevanceScore, _interruptionLevel, _collapseId, _rawPayload];
220221
}
221222

222223
- (NSDictionary *)jsonRepresentation {
@@ -281,6 +282,9 @@ - (NSDictionary *)jsonRepresentation {
281282

282283
if (self.interruptionLevel)
283284
[obj setObject:self.interruptionLevel forKeyedSubscript: @"interruptionLevel"];
285+
286+
if (self.collapseId)
287+
[obj setObject:self.collapseId forKeyedSubscript: @"collapseId"];
284288

285289
return obj;
286290
}

iOS_SDK/OneSignalSDK/Source/OneSignal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ typedef NS_ENUM(NSUInteger, OSNotificationActionType) {
136136
/* iOS 15+ : Interruption Level */
137137
@property(readonly)NSString *interruptionLevel;
138138

139+
@property(readonly, nullable)NSString *collapseId;
140+
139141
/* Parses an APNS push payload into a OSNotification object.
140142
Useful to call from your NotificationServiceExtension when the
141143
didReceiveNotificationRequest:withContentHandler: method fires. */

iOS_SDK/OneSignalSDK/Source/OneSignalNotificationServiceExtensionHandler.m

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ + (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotif
5555
if (!replacementContent)
5656
replacementContent = [request.content mutableCopy];
5757

58-
let notification = [OSNotification parseWithApns:request.content.userInfo];
58+
/*
59+
Add the collapse Id (request.identifier) to userInfo
60+
so it can be parsed by parseWithApns outside of the extension
61+
*/
62+
replacementContent.userInfo = [self userInfoWithCollapseId:replacementContent.userInfo
63+
identifier:request.identifier];
64+
65+
let notification = [OSNotification parseWithApns:replacementContent.userInfo];
5966

6067
// Handle badge count
6168
[OneSignalExtensionBadgeHandler handleBadgeCountWithNotificationRequest:request withNotification:notification withMutableNotificationContent:replacementContent];
@@ -88,6 +95,21 @@ + (UNMutableNotificationContent*)didReceiveNotificationExtensionRequest:(UNNotif
8895
return replacementContent;
8996
}
9097

98+
+ (NSDictionary *)userInfoWithCollapseId:(NSDictionary *)userInfo identifier:(NSString *)identifier {
99+
NSMutableDictionary *userInfoWithCollapseId = [NSMutableDictionary dictionaryWithDictionary:userInfo];
100+
if (userInfoWithCollapseId[@"os_data"]) {
101+
NSMutableDictionary *osdataDict = [NSMutableDictionary dictionaryWithDictionary:userInfoWithCollapseId[@"os_data"]];
102+
osdataDict[@"collapse_id"] = identifier;
103+
userInfoWithCollapseId[@"os_data"] = osdataDict;
104+
} else if (userInfoWithCollapseId[@"custom"]) {
105+
NSMutableDictionary *customDict = [NSMutableDictionary dictionaryWithDictionary:userInfoWithCollapseId[@"custom"]];
106+
customDict[@"collapse_id"] = identifier;
107+
userInfoWithCollapseId[@"custom"] = customDict;
108+
}
109+
110+
return userInfoWithCollapseId;
111+
}
112+
91113
+ (UNMutableNotificationContent*)serviceExtensionTimeWillExpireRequest:(UNNotificationRequest*)request
92114
withMutableNotificationContent:(UNMutableNotificationContent*)replacementContent {
93115
if (!replacementContent)

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ + (UNNotification *)createBasiciOSNotificationWithPayload:(NSDictionary *)userIn
348348
UNNotificationRequest *unNotifRequqest = [UNNotificationRequest alloc];
349349
// Set as remote push type
350350
[unNotifRequqest setValue:[UNPushNotificationTrigger alloc] forKey:@"trigger"];
351+
[unNotifRequqest setValue:@"test_id" forKey:@"identifier"];
351352
[unNotifContent setValue:userInfo forKey:@"userInfo"];
352353
[unNotifRequqest setValue:unNotifContent forKeyPath:@"content"];
353354
[unNotif setValue:unNotifRequqest forKeyPath:@"request"];

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3330,4 +3330,92 @@ - (void)testTimezoneId {
33303330
XCTAssertEqualObjects(OneSignalClientOverrider.lastHTTPRequest[@"timezone_id"], mockTimezone.name);
33313331

33323332
}
3333+
3334+
- (void)testParseCollapseIdInOSData {
3335+
NSDictionary *apsOsData = @{
3336+
@"aps": @{
3337+
@"content-available": @1,
3338+
@"mutable-content": @1,
3339+
@"alert": @"Message Body",
3340+
@"relevance-score": @0.15,
3341+
@"interruption-level": @"time-sensitive",
3342+
},
3343+
@"os_data": @{
3344+
@"i": @"notif id",
3345+
@"ti": @"templateId123",
3346+
@"tn": @"Template name",
3347+
@"collapse_id" : @"test_id"
3348+
}};
3349+
OSNotification *notification = [OSNotification parseWithApns:apsOsData];
3350+
XCTAssertEqualObjects(notification.collapseId, @"test_id");
3351+
NSDictionary *json = [notification jsonRepresentation];
3352+
XCTAssertEqualObjects(json[@"collapseId"], @"test_id");
3353+
}
3354+
3355+
- (void)testParseCollapseIdInCustom {
3356+
NSDictionary *apsCustom = @{
3357+
@"aps": @{
3358+
@"content-available": @1,
3359+
@"mutable-content": @1,
3360+
@"alert": @"Message Body",
3361+
@"relevance-score": @0.15,
3362+
@"interruption-level": @"time-sensitive",
3363+
},
3364+
@"custom": @{
3365+
@"i": @"notif id",
3366+
@"ti": @"templateId123",
3367+
@"tn": @"Template name",
3368+
@"collapse_id" : @"test_id"
3369+
}};
3370+
OSNotification *notification = [OSNotification parseWithApns:apsCustom];
3371+
XCTAssertEqualObjects(notification.collapseId, @"test_id");
3372+
NSDictionary *json = [notification jsonRepresentation];
3373+
XCTAssertEqualObjects(json[@"collapseId"], @"test_id");
3374+
}
3375+
3376+
- (void) testParseCollapseIdFromNotificationRequestWithCustom {
3377+
NSDictionary *apsCustom = @{
3378+
@"aps": @{
3379+
@"content-available": @1,
3380+
@"mutable-content": @1,
3381+
@"alert": @"Message Body",
3382+
@"relevance-score": @0.15,
3383+
@"interruption-level": @"time-sensitive",
3384+
},
3385+
@"custom": @{
3386+
@"i": @"notif id",
3387+
@"ti": @"templateId123",
3388+
@"tn": @"Template name"
3389+
}};
3390+
id notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:apsCustom];
3391+
3392+
UNMutableNotificationContent* content = [OneSignal didReceiveNotificationExtensionRequest:[notifResponse notification].request withMutableNotificationContent:nil
3393+
withContentHandler:nil];
3394+
3395+
XCTAssertEqualObjects(content.userInfo[@"custom"][@"collapse_id"], @"test_id");
3396+
}
3397+
3398+
- (void) testParseCollapseIdFromNotificationRequestWithOSData {
3399+
NSDictionary *apsOSData = @{
3400+
@"aps": @{
3401+
@"content-available": @1,
3402+
@"mutable-content": @1,
3403+
@"alert": @"Message Body",
3404+
@"relevance-score": @0.15,
3405+
@"interruption-level": @"time-sensitive",
3406+
},
3407+
@"os_data": @{
3408+
@"i": @"notif id",
3409+
@"ti": @"templateId123",
3410+
@"tn": @"Template name"
3411+
}};
3412+
id notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:apsOSData];
3413+
3414+
UNMutableNotificationContent* content = [OneSignal didReceiveNotificationExtensionRequest:[notifResponse notification].request withMutableNotificationContent:nil
3415+
withContentHandler:nil];
3416+
3417+
3418+
XCTAssertEqualObjects(content.userInfo[@"os_data"][@"collapse_id"], @"test_id");
3419+
}
3420+
33333421
@end

0 commit comments

Comments
 (0)