diff --git a/Source/ARTPushActivationState.m b/Source/ARTPushActivationState.m index 0e57f7378..8a97c088e 100644 --- a/Source/ARTPushActivationState.m +++ b/Source/ARTPushActivationState.m @@ -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; diff --git a/Source/ARTPushActivationStateMachine+Private.h b/Source/ARTPushActivationStateMachine+Private.h index 2432e15d1..32a0d385e 100644 --- a/Source/ARTPushActivationStateMachine+Private.h +++ b/Source/ARTPushActivationStateMachine+Private.h @@ -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 diff --git a/Source/ARTPushActivationStateMachine.m b/Source/ARTPushActivationStateMachine.m index 49bb422c0..a1ec4894c 100644 --- a/Source/ARTPushActivationStateMachine.m +++ b/Source/ARTPushActivationStateMachine.m @@ -16,6 +16,8 @@ #if TARGET_OS_IOS +#import + NSString *const ARTPushActivationCurrentStateKey = @"ARTPushActivationCurrentState"; NSString *const ARTPushActivationPendingEventsKey = @"ARTPushActivationPendingEvents"; @@ -379,6 +381,14 @@ - (void)callUpdateFailedCallback:(nullable ARTErrorInfo *)error { #endif } +- (void)registerForAPNS { +#if !TARGET_OS_SIMULATOR + dispatch_async(dispatch_get_main_queue(), ^{ + [[UIApplication sharedApplication] registerForRemoteNotifications]; + }); +#endif +} + @end #endif diff --git a/Spec/Tests/PushTests.swift b/Spec/Tests/PushTests.swift index 1aa91cab4..8eafe0cd8 100644 --- a/Spec/Tests/PushTests.swift +++ b/Spec/Tests/PushTests.swift @@ -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()) + } }