Skip to content

Commit cb1d461

Browse files
committed
move badge clearing and notif type updates to notifications module
Move uiscene check to core in a bundle utils file Create stub unit testing file for notifications
1 parent 0572813 commit cb1d461

File tree

11 files changed

+370
-29
lines changed

11 files changed

+370
-29
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 230 additions & 1 deletion
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// OSBundleUtils.h
3+
// OneSignal
4+
//
5+
// Created by Elliot Mawby on 6/20/24.
6+
// Copyright © 2024 Hiptic. All rights reserved.
7+
//
8+
9+
@interface OSBundleUtils : NSObject
10+
+ (BOOL)isAppUsingUIScene;
11+
@end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// OSBundleUtils.m
3+
// OneSignalCore
4+
//
5+
// Created by Elliot Mawby on 6/17/24.
6+
// Copyright © 2024 Hiptic. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "OSBundleUtils.h"
11+
@implementation OSBundleUtils
12+
13+
+ (BOOL)isAppUsingUIScene {
14+
if (@available(iOS 13.0, *)) {
15+
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"] != nil;
16+
}
17+
return NO;
18+
}
19+
20+
@end

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
#import <OneSignalCore/OneSignalWrapper.h>
5757
#import <OneSignalCore/OSInAppMessages.h>
5858
#import <OneSignalCore/OSLocation.h>
59-
59+
#import <OneSignalCore/OSBundleUtils.h>
6060
// TODO: Testing: Should this class be defined in this file?
6161
@interface OneSignalCoreImpl : NSObject
6262

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,38 @@ + (void)start {
251251
@selector(onesignalSetApplicationIconBadgeNumber:)
252252
);
253253
[OneSignalNotificationsUNUserNotificationCenter setup];
254+
255+
[self registerLifecycleObserver];
256+
254257
}
255258
#pragma clang diagnostic pop
256259

260+
+ (void)registerLifecycleObserver {
261+
// Replacing swizzled lifecycle selectors with notification center observers for scene based Apps
262+
if ([OSBundleUtils isAppUsingUIScene]) {
263+
[self registerLifecycleObserverAsUIScene];
264+
} else {
265+
[self registerLifecycleObserverAsUIApplication];
266+
}
267+
}
268+
269+
+ (void)registerLifecycleObserverAsUIScene {
270+
if (@available(iOS 13.0, *)) {
271+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSNotificationManager registering for Scene Lifecycle notifications"];
272+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:@"UISceneWillEnterForegroundNotification" object:nil];
273+
}
274+
}
275+
276+
+ (void)registerLifecycleObserverAsUIApplication {
277+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSNotificationManager registering for Application Lifecycle notifications"];
278+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
279+
}
280+
281+
+ (void)willEnterForeground {
282+
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
283+
[OSNotificationsManager sendNotificationTypesUpdateToDelegate];
284+
}
285+
257286
+ (void)resetLocals {
258287
_lastMessageReceived = nil;
259288
_lastMessageIdFromAction = nil;
@@ -967,6 +996,10 @@ + (UNNotificationRequest*)prepareUNNotificationRequest:(OSNotification*)notifica
967996
return [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
968997
}
969998

999+
- (void)dealloc {
1000+
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"OSNotificationsManager observer deallocated"];
1001+
[[NSNotificationCenter defaultCenter] removeObserver:self];
1002+
}
9701003

9711004
@end
9721005

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// OneSignalNotificationsTests.swift
3+
// OneSignalNotificationsTests
4+
//
5+
// Created by Elliot Mawby on 6/17/24.
6+
// Copyright © 2024 Hiptic. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
final class OneSignalNotificationsTests: XCTestCase {
12+
13+
override func setUpWithError() throws {
14+
// Put setup code here. This method is called before the invocation of each test method in the class.
15+
}
16+
17+
override func tearDownWithError() throws {
18+
// Put teardown code here. This method is called after the invocation of each test method in the class.
19+
}
20+
21+
func testExample() throws {
22+
// This is an example of a functional test case.
23+
// Use XCTAssert and related functions to verify your tests produce the correct results.
24+
// Any test you write for XCTest can be annotated as throws and async.
25+
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
26+
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
27+
}
28+
29+
func testPerformanceExample() throws {
30+
// This is an example of a performance test case.
31+
measure {
32+
// Put the code you want to measure the time of here.
33+
}
34+
}
35+
36+
func testClearBadgesWhenAppEntersForeground() throws {
37+
// NotificationManager Start? Or Mock NotificationManager start
38+
// Mock receive a notification or have badge count > 0
39+
40+
// Then background the app
41+
42+
// Foreground the app
43+
44+
// Ensure that badge count == 0
45+
}
46+
47+
func testDontclearBadgesWhenAppBecomesActive() throws {
48+
// NotificationManager Start? Or Mock NotificationManager start
49+
// Mock receive a notification or have badge count > 0
50+
51+
// Then resign active
52+
53+
// App becomes active the app
54+
55+
// Ensure that badge count == previous badge count
56+
}
57+
58+
func testUpdateNotificationTypesOnAppEntersForeground() throws {
59+
// NotificationManager Start? Or Mock NotificationManager start
60+
// Deny notification permission
61+
62+
// Then background the app
63+
64+
// Change app notification permissions
65+
66+
// Foreground the app for within 30 seconds
67+
68+
// Ensure that we update the notification types
69+
}
70+
71+
72+
}

iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.m

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ +(OneSignalLifecycleObserver*) sharedInstance {
4949

5050
+ (void)registerLifecycleObserver {
5151
// Replacing swizzled lifecycle selectors with notification center observers for scene based Apps
52-
if ([UIApplication isAppUsingUIScene]) {
52+
if ([OSBundleUtils isAppUsingUIScene]) {
5353
[self registerLifecycleObserverAsUIScene];
5454
} else {
5555
[self registerLifecycleObserverAsUIApplication];
@@ -68,7 +68,6 @@ + (void)registerLifecycleObserverAsUIScene {
6868
+ (void)registerLifecycleObserverAsUIApplication {
6969
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"registering for Application Lifecycle notifications"];
7070
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
71-
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
7271
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(didBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
7372
[[NSNotificationCenter defaultCenter] addObserver:[OneSignalLifecycleObserver sharedInstance] selector:@selector(willResignActive) name:UIApplicationWillResignActiveNotification object:nil];
7473
}
@@ -113,11 +112,6 @@ - (void)didEnterBackground {
113112
}
114113
}
115114

116-
- (void)willEnterForeground {
117-
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"application/scene willEnterForeground"];
118-
[OneSignalTracker applicationWillEnterForeground];
119-
}
120-
121115
- (void)dealloc {
122116
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:@"lifecycle observer deallocated"];
123117
[[NSNotificationCenter defaultCenter] removeObserver:self];

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@
3131

3232
+ (void)onFocus:(BOOL)toBackground;
3333
+ (void)onSessionEnded:(NSArray<OSInfluence *> *) lastInfluences;
34-
+ (void)applicationWillEnterForeground;
3534

3635
@end

iOS_SDK/OneSignalSDK/Source/OneSignalTracker.m

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,6 @@ + (void)onFocus:(BOOL)toBackground {
8181
}
8282
}
8383

84-
// This is a separate lifecycle event than application became active
85-
// Notably this is NOT called when the app resumes after resigning active
86-
// From things like entering and exiting the notification center
87-
+ (void)applicationWillEnterForeground {
88-
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Foregrounded started"];
89-
[OSNotificationsManager clearBadgeCount:false fromClearAll:false];
90-
}
91-
9284
+ (void)applicationBecameActive {
9385
[OneSignalLog onesignalLog:ONE_S_LL_DEBUG message:@"Application Active started"];
9486
[OSFocusTimeProcessorFactory cancelFocusCall];
@@ -102,8 +94,6 @@ + (void)applicationBecameActive {
10294
if ([OneSignal shouldStartNewSession])
10395
[OneSignal startNewSession:NO];
10496
else {
105-
// This checks if notification permissions changed when app was backgrounded
106-
[OSNotificationsManager sendNotificationTypesUpdateToDelegate];
10797
[[OSSessionManager sharedSessionManager] attemptSessionUpgrade];
10898
// TODO: Here it used to call receivedInAppMessageJson with nil, this method no longer exists
10999
// [OneSignal receivedInAppMessageJson:nil];

iOS_SDK/OneSignalSDK/Source/UIApplication+OneSignal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@
2828
#import <UIKit/UIKit.h>
2929
@interface UIApplication (OneSignal)
3030
+ (BOOL)applicationIsActive;
31-
+ (BOOL)isAppUsingUIScene;
3231
@end

0 commit comments

Comments
 (0)