Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call to the registerForRemoteNotifications #1293

Merged
merged 10 commits into from
Apr 15, 2022
1 change: 1 addition & 0 deletions Source/ARTPushActivationState.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ - (ARTPushActivationState *)transition:(ARTPushActivationEvent *)event {
return self;
}
else if ([event isKindOfClass:[ARTPushActivationEventCalledActivate class]]) {
[self.machine registerForAPNS];
return validateAndSync(self.machine, event);
}
return nil;
Expand Down
2 changes: 2 additions & 0 deletions Source/ARTPushActivationStateMachine+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern NSString *const ARTPushActivationPendingEventsKey;
@property (readonly, nonatomic) ARTPushActivationEvent *lastEvent_nosync;
@property (readonly, nonatomic) ARTPushActivationState *current_nosync;

- (void)registerForAPNS;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions Source/ARTPushActivationStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#if TARGET_OS_IOS

#import <UIKit/UIKit.h>

NSString *const ARTPushActivationCurrentStateKey = @"ARTPushActivationCurrentState";
NSString *const ARTPushActivationPendingEventsKey = @"ARTPushActivationPendingEvents";

Expand Down Expand Up @@ -379,6 +381,19 @@ - (void)callUpdateFailedCallback:(nullable ARTErrorInfo *)error {
#endif
}

- (void)registerForAPNS {
#if !TARGET_OS_SIMULATOR
if ([NSThread isMainThread]) {
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
#endif
}

@end

#endif
22 changes: 22 additions & 0 deletions Spec/Tests/PushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,26 @@ class PushTests: XCTestCase {
pushRegistererDelegate = nil
expect(rest.internal.options.pushRegistererDelegate).to(beNil())
}

func test__016__activate_should_call_registerForAPNS_while_transition_from_not_activated() {
var registerForAPNSMethodWasCalled = false

let hook = rest.push.internal.activationMachine.testSuite_injectIntoMethod(after: NSSelectorFromString("registerForAPNS")) {
registerForAPNSMethodWasCalled = true
}

defer {
hook.remove()
rest.push.internal.activationMachine.transitions = nil
}
waitUntil(timeout: testTimeout) { done in
rest.push.internal.activationMachine.transitions = { event, _, _ in
if event is ARTPushActivationEventCalledActivate {
done()
}
}
rest.push.activate()
}
expect(registerForAPNSMethodWasCalled).to(beTrue())
}
}