Skip to content

Commit

Permalink
feat: iOS | Add Get APNs Token
Browse files Browse the repository at this point in the history
Update iOS lib files in order to include the new Get APNs Token method.
Delegate the app's `application:didRegisterForRemoteNotificationWithDeviceToken:` to Cloud Messaging's delegate extension.
  • Loading branch information
OS-ricardomoreirasilva committed Dec 16, 2022
1 parent 6af219a commit 945dfda
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The changes documented here do not include those from the original repository.

## [Unreleased]

### 16-12-2022
- Feat: [iOS] Update iOS lib files in order to include the new Get APNs Token method (https://outsystemsrd.atlassian.net/browse/RMET-2054).

## [Version 1.0.5]

### 08-11-2022
Expand Down
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<source-file src="src/ios/Utils/FirebaseConfigurations.swift" />
<source-file src="src/ios/Utils/FirebaseMessagingErrors.swift" />
<source-file src="src/ios/Utils/FirebaseNotificationEventType.swift" />
<source-file src="src/ios/Utils/OSFCMTokenType.swift" />

<source-file src="src/ios/FirebaseMessagingApplicationDelegate.swift" />
<source-file src="src/ios/FirebaseMessagingController.swift" />
Expand All @@ -68,7 +69,7 @@
<source url="https://cdn.cocoapods.org/"/>
</config>
<pods use-frameworks="true">
<pod name="Firebase/Messaging" spec="~> 8.6.0" />
<pod name="Firebase/Messaging" spec="8.6.1" />
<pod name="OSCommonPluginLib" spec="1.0.0" />
</pods>
</podspec>
Expand Down
18 changes: 17 additions & 1 deletion src/ios/AppDelegate+OSFirebaseCloudMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ + (void)load {
Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
Method swizzled = class_getInstanceMethod(self, @selector(application:firebaseCloudMessagingPluginDidFinishLaunchingWithOptions:));
method_exchangeImplementations(original, swizzled);

original = class_getInstanceMethod(self, @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:));
swizzled = class_getInstanceMethod(self, @selector(application:firebaseCloudMessagingDidReceiveRemoteNotification:fetchCompletionHandler:));
method_exchangeImplementations(original, swizzled);

original = class_getInstanceMethod(self, @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:));
swizzled = class_getInstanceMethod(self, @selector(application:firebaseCloudMessagingdidRegisterForRemoteNotificationsWithDeviceToken:));
method_exchangeImplementations(original, swizzled);
}

- (BOOL)application:(UIApplication *)application firebaseCloudMessagingPluginDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Expand All @@ -18,8 +26,16 @@ - (BOOL)application:(UIApplication *)application firebaseCloudMessagingPluginDid
return YES;
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
- (void)application:(UIApplication *)application firebaseCloudMessagingDidReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self application:application firebaseCloudMessagingDidReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];

(void)[FirebaseMessagingApplicationDelegate.shared application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application firebaseCloudMessagingdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[self application:application firebaseCloudMessagingdidRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

(void)[FirebaseMessagingApplicationDelegate.shared application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

@end
4 changes: 2 additions & 2 deletions src/ios/FirebaseMessagingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ open class FirebaseMessagingController: NSObject {
}

// MARK: Token Public Methods
public func getToken() async {
public func getToken(ofType type: OSFCMTokenType = .fcm) async {
do {
let token = try await self.firebaseManager?.getToken() ?? ""
let token = try await self.firebaseManager?.getToken(of: type) ?? ""
let topic = self.firebaseManager?.getGeneralTopic() ?? ""
try await self.subscribe(topic, token: token)
} catch let error as FirebaseMessagingErrors {
Expand Down
20 changes: 17 additions & 3 deletions src/ios/Managers/MessagingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ public class MessagingManager: MessagingProtocol {
return "\(FirebaseApp.app()?.options.gcmSenderID ?? "")\(Self.generalTopic)"
}

/// Returns the Firebase Cloud Messaging registration token. This token is obtained asynchronously.
/// Returns the token associated to the parameter `type`.
/// If `fcm` it returns the Firebase Cloud Messaging registration token.
/// If `apns` it returns the Apple Push Notification service token.
/// This token is obtained asynchronously.
/// - Returns: Registration token.
public func getToken() async throws -> String {
return try await Messaging.messaging().token()
/// - Parameter type: type of token to return. Can be `FCM` or `APNs`.
public func getToken(of type: OSFCMTokenType) async throws -> String {
var result: String
let messagingInstance = Messaging.messaging()

if type == .apns {
guard let data = messagingInstance.apnsToken else { throw FirebaseMessagingErrors.obtainingTokenError }
result = data.map{ String(format: "%02.2hhx", $0) }.joined()
} else {
result = try await messagingInstance.token()
}

return result
}

/// Deletes the Firebase cloud Messaging registration token. The deletion is made asynchronously.
Expand Down
10 changes: 9 additions & 1 deletion src/ios/OSFirebaseCloudMessaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ class OSFirebaseCloudMessaging: CDVPlugin {
func getToken(command: CDVInvokedUrlCommand) {
self.callbackId = command.callbackId
Task {
await self.plugin?.getToken()
await self.plugin?.getToken(ofType: .fcm)
}
}

@objc(getAPNsToken:)
func getAPNsToken(command: CDVInvokedUrlCommand) {
self.callbackId = command.callbackId
Task {
await self.plugin?.getToken(ofType: .apns)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/ios/Protocols/MessagingProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ public protocol MessagingProtocol {
/// - Returns: Main topic.
func getGeneralTopic() -> String

/// Returns the Firebase Cloud Messaging registration token. This token is obtained asynchronously.
/// Returns the token associated to the parameter `type`.
/// If `fcm` it returns the Firebase Cloud Messaging registration token.
/// If `apns` it returns the Apple Push Notification service token.
/// This token is obtained asynchronously.
/// - Returns: Registration token.
func getToken() async throws -> String
/// - Parameter type: type of token to return. Can be `FCM` or `APNs`.
func getToken(of type: OSFCMTokenType) async throws -> String

/// Deletes the Firebase cloud Messaging registration token. The deletion is made asynchronously.
func deleteToken() async throws
Expand Down
5 changes: 5 additions & 0 deletions src/ios/Utils/OSFCMTokenType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Indicates the type of token to be requested
public enum OSFCMTokenType {
case fcm
case apns
}
4 changes: 4 additions & 0 deletions www/OSFirebaseCloudMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ exports.getToken = function (success, error) {
exec(success, error, 'OSFirebaseCloudMessaging', 'getToken');
};

exports.getAPNsToken = function (success, error) {
exec(success, error, 'OSFirebaseCloudMessaging', 'getAPNsToken');
};

exports.subscribe = function (topic, success, error) {
exec(success, error, 'OSFirebaseCloudMessaging', 'subscribe', [topic]);
};
Expand Down

0 comments on commit 945dfda

Please sign in to comment.