From 94f2490ad9bb6680f69e45b64e0866b087991d1c Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Wed, 20 Jul 2022 13:31:07 +0200 Subject: [PATCH 1/2] fix(messaging-core): AuthorizationStatus enum export --- packages/firebase-messaging-core/README.md | 68 ++++++++----------- packages/firebase-messaging-core/common.d.ts | 7 ++ packages/firebase-messaging-core/common.ts | 7 ++ .../firebase-messaging-core/index.android.ts | 5 +- packages/firebase-messaging-core/index.d.ts | 10 +-- packages/firebase-messaging-core/index.ios.ts | 5 +- 6 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 packages/firebase-messaging-core/common.d.ts create mode 100644 packages/firebase-messaging-core/common.ts diff --git a/packages/firebase-messaging-core/README.md b/packages/firebase-messaging-core/README.md index d7a85b95..01162ba5 100644 --- a/packages/firebase-messaging-core/README.md +++ b/packages/firebase-messaging-core/README.md @@ -1,6 +1,6 @@ # @nativescript/firebase-messaging-core -```javascript +```cli ns plugin add @nativescript/firebase-messaging-core ``` @@ -10,8 +10,6 @@ Firebase Messaging Core is a lite package which enables you to use a third-party On Android it will always use FCM. - - ## Usage ### iOS - Requesting permissions @@ -24,21 +22,17 @@ This module provides a requestPermission method which triggers a native permissi import { MessagingCore, AuthorizationStatus } from '@nativescript/firebase-messaging-core'; async function requestUserPermission() { - const authStatus = await MessagingCore - .getInstance() - .requestPermission({ - ios: { - alert: true, - }, - }); + const authStatus = await MessagingCore.getInstance().requestPermission({ + ios: { + alert: true, + }, + }); const enabled = authStatus === AuthorizationStatus.AUTHORIZED || authStatus === AuthorizationStatus.PROVISIONAL; if (enabled) { console.log('Authorization status:', authStatus); - const didRegister = await MessagingCore - .getInstance() - .registerDeviceForRemoteMessages(); + const didRegister = await MessagingCore.getInstance().registerDeviceForRemoteMessages(); } } ``` @@ -47,8 +41,6 @@ The permissions API for iOS provides much more fine-grain control over permissio On Android, you do not need to request user permission. This method can still be called on Android devices; however, and will always resolve successfully. - - ### Foreground state messages To listen to messages in the foreground, call the onMessage method inside of your application code. Code executed via this handler is able to interact with your application (e.g. updating the state or UI). @@ -59,11 +51,9 @@ For example, the Alert API could be used to display a new Alert each time a mess import { alert } from '@nativescript/core'; import { MessagingCore } from '@nativescript/firebase-messaging-core'; -MessagingCore - .getInstance() - .addOnMessage(async (remoteMessage) => { - alert('A new Push message arrived!', JSON.stringify(remoteMessage)); - }); +MessagingCore.getInstance().addOnMessage(async (remoteMessage) => { + alert('A new Push message arrived!', JSON.stringify(remoteMessage)); +}); ``` # Always show notifications when the application is in foreground @@ -88,40 +78,37 @@ The examples below use a NativeScript ApplicationSettings to store and manage th Once your application has started, you can call the getToken method on the Cloud Messaging module to get the unique device token (if using a different push notification provider, such as Amazon SNS, you will need to call getAPNSToken on iOS): ```ts - import { ApplicationSettings } from '@nativescript/core'; import { MessagingCore } from '@nativescript/firebase-messaging-core'; - async function saveTokenToDatabase(token) { - ApplicationSettings.setString(token); + ApplicationSettings.setString(token); } // Get the device token - MessagingCore - .getInstance() - .getCurrentToken() - .then(token => { - saveTokenToDatabase(token); - }); - - // Listen to whether the token changes - MessagingCore - .getInstance().addOnToken(token => { - saveTokenToDatabase(token); - }); +MessagingCore.getInstance() + .getCurrentToken() + .then((token) => { + saveTokenToDatabase(token); + }); + +// Listen to whether the token changes +MessagingCore.getInstance().addOnToken((token) => { + saveTokenToDatabase(token); +}); ``` + ### Android Integration Push notification icon and color -If you want to use a specific icon for the push notification, it has to be configured in the tag in the AndroidManifest.xml +If you want to use a specific icon for the push notification, it has to be configured in the tag in the `AndroidManifest.xml` ```xml + android:resource="@drawable/your_drawable_name" /> + android:resource="@color/ns_primary" /> ``` ### Apple Integration @@ -140,8 +127,8 @@ Copy that file to `app/App_Resources/iOS/` (if it doesn't exist yet, otherwise m so it's not removed when you remove and re-add the iOS platform. The relevant content for background push in that file is: ```xml - aps-environment - development +aps-environment +development ``` #### Allow processing when a background push is received @@ -155,7 +142,6 @@ Open `app/App_Resources/iOS/Info.plist` and add this to the bottom: ``` - ## License Apache License Version 2.0 diff --git a/packages/firebase-messaging-core/common.d.ts b/packages/firebase-messaging-core/common.d.ts new file mode 100644 index 00000000..9a90f6b3 --- /dev/null +++ b/packages/firebase-messaging-core/common.d.ts @@ -0,0 +1,7 @@ +export declare enum AuthorizationStatus { + AUTHORIZED = 0, + DENIED = 1, + NOT_DETERMINED = 2, + PROVISIONAL = 3, + EPHEMERAL = 4, +} diff --git a/packages/firebase-messaging-core/common.ts b/packages/firebase-messaging-core/common.ts new file mode 100644 index 00000000..fb99d9a9 --- /dev/null +++ b/packages/firebase-messaging-core/common.ts @@ -0,0 +1,7 @@ +export enum AuthorizationStatus { + AUTHORIZED, + DENIED, + NOT_DETERMINED, + PROVISIONAL, + EPHEMERAL, +} diff --git a/packages/firebase-messaging-core/index.android.ts b/packages/firebase-messaging-core/index.android.ts index eb6fac35..c70869c1 100644 --- a/packages/firebase-messaging-core/index.android.ts +++ b/packages/firebase-messaging-core/index.android.ts @@ -1,5 +1,8 @@ import { AndroidActivityNewIntentEventData, AndroidApplication, Application, Device, Utils } from '@nativescript/core'; -import { AuthorizationStatus, IMessagingCore } from '.'; +import { IMessagingCore } from '.'; +import { AuthorizationStatus } from './common'; + +export { AuthorizationStatus }; let defaultInstance: MessagingCore; diff --git a/packages/firebase-messaging-core/index.d.ts b/packages/firebase-messaging-core/index.d.ts index b390353f..ce04f724 100644 --- a/packages/firebase-messaging-core/index.d.ts +++ b/packages/firebase-messaging-core/index.d.ts @@ -1,10 +1,6 @@ -export enum AuthorizationStatus { - AUTHORIZED, - DENIED, - NOT_DETERMINED, - PROVISIONAL, - EPHEMERAL, -} +import { AuthorizationStatus } from './common'; + +export { AuthorizationStatus }; export interface AndroidPermissions {} diff --git a/packages/firebase-messaging-core/index.ios.ts b/packages/firebase-messaging-core/index.ios.ts index 93e1035e..d154cc7f 100644 --- a/packages/firebase-messaging-core/index.ios.ts +++ b/packages/firebase-messaging-core/index.ios.ts @@ -1,5 +1,8 @@ import { Application, ApplicationSettings, Device } from '@nativescript/core'; -import { AuthorizationStatus, IMessagingCore, Permissions } from '.'; +import { IMessagingCore, Permissions } from '.'; +import { AuthorizationStatus } from './common'; + +export { AuthorizationStatus }; declare const TNSFirebaseCore; From f9645ebf4cddc1344b0ea0cf34057198e5384d4b Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Wed, 20 Jul 2022 16:59:03 +0200 Subject: [PATCH 2/2] fix: call token callback when receiving a token --- packages/firebase-messaging-core/index.ios.ts | 6 +++--- .../platforms/ios/src/NSCNotificationHelper.swift | 3 +++ .../platforms/ios/src/NSCUIApplicationDelegate.swift | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/firebase-messaging-core/index.ios.ts b/packages/firebase-messaging-core/index.ios.ts index d154cc7f..f2a196d9 100644 --- a/packages/firebase-messaging-core/index.ios.ts +++ b/packages/firebase-messaging-core/index.ios.ts @@ -103,11 +103,11 @@ export class MessagingCore implements IMessagingCore { MessagingCore.#inForeground = false; }); - NSCFirebaseMessagingCore.onMessageCallback = this.#onMessage; + NSCFirebaseMessagingCore.onMessageCallback = this.#onMessage.bind(this); - NSCFirebaseMessagingCore.onTokenCallback = this.#onToken; + NSCFirebaseMessagingCore.onTokenCallback = this.#onToken.bind(this); - NSCFirebaseMessagingCore.onNotificationTapCallback = this.#onNotificationTap; + NSCFirebaseMessagingCore.onNotificationTapCallback = this.#onNotificationTap.bind(this); } static getInstance() { diff --git a/packages/firebase-messaging-core/platforms/ios/src/NSCNotificationHelper.swift b/packages/firebase-messaging-core/platforms/ios/src/NSCNotificationHelper.swift index 26ebe929..21138d0e 100644 --- a/packages/firebase-messaging-core/platforms/ios/src/NSCNotificationHelper.swift +++ b/packages/firebase-messaging-core/platforms/ios/src/NSCNotificationHelper.swift @@ -27,7 +27,10 @@ public class NSCNotificationHelper: NSObject { if #available(iOS 10.0, *) { NSCUNUserNotificationCenterDelegate.sharedInstance.observe() } + +#if canImport(NSCFIRMessagingDelegate) NSCFIRMessagingDelegate.sharedInstance.observe() +#endif let auto = UserDefaults.standard.bool(forKey: NSCNotificationHelper.REMOTE_NOTIFICATIONS_REGISTRATION_STATUS) let isSimulator = UIDevice.current.name.lowercased().contains("simulator") if (auto && !isSimulator) { diff --git a/packages/firebase-messaging-core/platforms/ios/src/NSCUIApplicationDelegate.swift b/packages/firebase-messaging-core/platforms/ios/src/NSCUIApplicationDelegate.swift index 4e3b8822..5a7f7eec 100644 --- a/packages/firebase-messaging-core/platforms/ios/src/NSCUIApplicationDelegate.swift +++ b/packages/firebase-messaging-core/platforms/ios/src/NSCUIApplicationDelegate.swift @@ -115,6 +115,9 @@ public class NSCUIApplicationDelegate: UIResponder , UIApplicationDelegate { UserDefaults.standard.set(true, forKey: NSCNotificationHelper.REMOTE_NOTIFICATIONS_REGISTRATION_STATUS) NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback?(UIApplication.shared.isRegisteredForRemoteNotifications, nil) NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback = nil + + NSCFirebaseMessagingCore.onTokenCallback?(NSCFirebaseMessagingCore.apnsToken(toString: deviceToken)) + NSCFirebaseMessagingCore.onTokenCallback = nil } @objc public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {