Skip to content

Commit fa4b6bd

Browse files
authored
fix(messaging-core): AuthorizationStatus enum export (#101)
* fix(messaging-core): AuthorizationStatus enum export * fix: call token callback when receiving a token
1 parent 4a04a39 commit fa4b6bd

File tree

8 files changed

+61
-53
lines changed

8 files changed

+61
-53
lines changed

packages/firebase-messaging-core/README.md

+27-41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# @nativescript/firebase-messaging-core
22

3-
```javascript
3+
```cli
44
ns plugin add @nativescript/firebase-messaging-core
55
```
66

@@ -10,8 +10,6 @@ Firebase Messaging Core is a lite package which enables you to use a third-party
1010

1111
On Android it will always use FCM.
1212

13-
14-
1513
## Usage
1614

1715
### iOS - Requesting permissions
@@ -24,21 +22,17 @@ This module provides a requestPermission method which triggers a native permissi
2422
import { MessagingCore, AuthorizationStatus } from '@nativescript/firebase-messaging-core';
2523

2624
async function requestUserPermission() {
27-
const authStatus = await MessagingCore
28-
.getInstance()
29-
.requestPermission({
30-
ios: {
31-
alert: true,
32-
},
33-
});
25+
const authStatus = await MessagingCore.getInstance().requestPermission({
26+
ios: {
27+
alert: true,
28+
},
29+
});
3430
const enabled = authStatus === AuthorizationStatus.AUTHORIZED || authStatus === AuthorizationStatus.PROVISIONAL;
3531

3632
if (enabled) {
3733
console.log('Authorization status:', authStatus);
3834

39-
const didRegister = await MessagingCore
40-
.getInstance()
41-
.registerDeviceForRemoteMessages();
35+
const didRegister = await MessagingCore.getInstance().registerDeviceForRemoteMessages();
4236
}
4337
}
4438
```
@@ -47,8 +41,6 @@ The permissions API for iOS provides much more fine-grain control over permissio
4741

4842
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.
4943

50-
51-
5244
### Foreground state messages
5345

5446
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
5951
import { alert } from '@nativescript/core';
6052
import { MessagingCore } from '@nativescript/firebase-messaging-core';
6153

62-
MessagingCore
63-
.getInstance()
64-
.addOnMessage(async (remoteMessage) => {
65-
alert('A new Push message arrived!', JSON.stringify(remoteMessage));
66-
});
54+
MessagingCore.getInstance().addOnMessage(async (remoteMessage) => {
55+
alert('A new Push message arrived!', JSON.stringify(remoteMessage));
56+
});
6757
```
6858

6959
# 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
8878
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):
8979

9080
```ts
91-
9281
import { ApplicationSettings } from '@nativescript/core';
9382
import { MessagingCore } from '@nativescript/firebase-messaging-core';
9483

95-
9684
async function saveTokenToDatabase(token) {
97-
ApplicationSettings.setString(token);
85+
ApplicationSettings.setString(token);
9886
}
9987

10088
// Get the device token
101-
MessagingCore
102-
.getInstance()
103-
.getCurrentToken()
104-
.then(token => {
105-
saveTokenToDatabase(token);
106-
});
107-
108-
// Listen to whether the token changes
109-
MessagingCore
110-
.getInstance().addOnToken(token => {
111-
saveTokenToDatabase(token);
112-
});
89+
MessagingCore.getInstance()
90+
.getCurrentToken()
91+
.then((token) => {
92+
saveTokenToDatabase(token);
93+
});
94+
95+
// Listen to whether the token changes
96+
MessagingCore.getInstance().addOnToken((token) => {
97+
saveTokenToDatabase(token);
98+
});
11399
```
100+
114101
### Android Integration
115102

116103
Push notification icon and color
117104

118-
If you want to use a specific icon for the push notification, it has to be configured in the tag in the AndroidManifest.xml
105+
If you want to use a specific icon for the push notification, it has to be configured in the tag in the `AndroidManifest.xml`
119106

120107
```xml
121108
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
122-
android:resource="@drawable/your_drawable_name" />
109+
android:resource="@drawable/your_drawable_name" />
123110
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
124-
android:resource="@color/ns_primary" />
111+
android:resource="@color/ns_primary" />
125112
```
126113

127114
### Apple Integration
@@ -140,8 +127,8 @@ Copy that file to `app/App_Resources/iOS/` (if it doesn't exist yet, otherwise m
140127
so it's not removed when you remove and re-add the iOS platform. The relevant content for background push in that file is:
141128

142129
```xml
143-
<key>aps-environment</key>
144-
<string>development</string>
130+
<key>aps-environment</key>
131+
<string>development</string>
145132
```
146133

147134
#### 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:
155142
</array>
156143
```
157144

158-
159145
## License
160146

161147
Apache License Version 2.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export declare enum AuthorizationStatus {
2+
AUTHORIZED = 0,
3+
DENIED = 1,
4+
NOT_DETERMINED = 2,
5+
PROVISIONAL = 3,
6+
EPHEMERAL = 4,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export enum AuthorizationStatus {
2+
AUTHORIZED,
3+
DENIED,
4+
NOT_DETERMINED,
5+
PROVISIONAL,
6+
EPHEMERAL,
7+
}

packages/firebase-messaging-core/index.android.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { AndroidActivityNewIntentEventData, AndroidApplication, Application, Device, Utils } from '@nativescript/core';
2-
import { AuthorizationStatus, IMessagingCore } from '.';
2+
import { IMessagingCore } from '.';
3+
import { AuthorizationStatus } from './common';
4+
5+
export { AuthorizationStatus };
36

47
let defaultInstance: MessagingCore;
58

packages/firebase-messaging-core/index.d.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
export enum AuthorizationStatus {
2-
AUTHORIZED,
3-
DENIED,
4-
NOT_DETERMINED,
5-
PROVISIONAL,
6-
EPHEMERAL,
7-
}
1+
import { AuthorizationStatus } from './common';
2+
3+
export { AuthorizationStatus };
84

95
export interface AndroidPermissions {}
106

packages/firebase-messaging-core/index.ios.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { Application, ApplicationSettings, Device } from '@nativescript/core';
2-
import { AuthorizationStatus, IMessagingCore, Permissions } from '.';
2+
import { IMessagingCore, Permissions } from '.';
3+
import { AuthorizationStatus } from './common';
4+
5+
export { AuthorizationStatus };
36

47
declare const TNSFirebaseCore;
58

@@ -100,11 +103,11 @@ export class MessagingCore implements IMessagingCore {
100103
MessagingCore.#inForeground = false;
101104
});
102105

103-
NSCFirebaseMessagingCore.onMessageCallback = this.#onMessage;
106+
NSCFirebaseMessagingCore.onMessageCallback = this.#onMessage.bind(this);
104107

105-
NSCFirebaseMessagingCore.onTokenCallback = this.#onToken;
108+
NSCFirebaseMessagingCore.onTokenCallback = this.#onToken.bind(this);
106109

107-
NSCFirebaseMessagingCore.onNotificationTapCallback = this.#onNotificationTap;
110+
NSCFirebaseMessagingCore.onNotificationTapCallback = this.#onNotificationTap.bind(this);
108111
}
109112

110113
static getInstance() {

packages/firebase-messaging-core/platforms/ios/src/NSCNotificationHelper.swift

+3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public class NSCNotificationHelper: NSObject {
2727
if #available(iOS 10.0, *) {
2828
NSCUNUserNotificationCenterDelegate.sharedInstance.observe()
2929
}
30+
31+
#if canImport(NSCFIRMessagingDelegate)
3032
NSCFIRMessagingDelegate.sharedInstance.observe()
33+
#endif
3134
let auto = UserDefaults.standard.bool(forKey: NSCNotificationHelper.REMOTE_NOTIFICATIONS_REGISTRATION_STATUS)
3235
let isSimulator = UIDevice.current.name.lowercased().contains("simulator")
3336
if (auto && !isSimulator) {

packages/firebase-messaging-core/platforms/ios/src/NSCUIApplicationDelegate.swift

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public class NSCUIApplicationDelegate: UIResponder , UIApplicationDelegate {
115115
UserDefaults.standard.set(true, forKey: NSCNotificationHelper.REMOTE_NOTIFICATIONS_REGISTRATION_STATUS)
116116
NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback?(UIApplication.shared.isRegisteredForRemoteNotifications, nil)
117117
NSCFirebaseMessagingCore.registerDeviceForRemoteMessagesCallback = nil
118+
119+
NSCFirebaseMessagingCore.onTokenCallback?(NSCFirebaseMessagingCore.apnsToken(toString: deviceToken))
120+
NSCFirebaseMessagingCore.onTokenCallback = nil
118121
}
119122

120123
@objc public func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

0 commit comments

Comments
 (0)