Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions packages/firebase-messaging-core/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @nativescript/firebase-messaging-core

```javascript
```cli
ns plugin add @nativescript/firebase-messaging-core
```

Expand All @@ -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
Expand All @@ -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();
}
}
```
Expand All @@ -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).
Expand All @@ -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
Expand All @@ -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
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/your_drawable_name" />
android:resource="@drawable/your_drawable_name" />
<meta-data android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/ns_primary" />
android:resource="@color/ns_primary" />
```

### Apple Integration
Expand All @@ -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
<key>aps-environment</key>
<string>development</string>
<key>aps-environment</key>
<string>development</string>
```

#### Allow processing when a background push is received
Expand All @@ -155,7 +142,6 @@ Open `app/App_Resources/iOS/Info.plist` and add this to the bottom:
</array>
```


## License

Apache License Version 2.0
7 changes: 7 additions & 0 deletions packages/firebase-messaging-core/common.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export declare enum AuthorizationStatus {
AUTHORIZED = 0,
DENIED = 1,
NOT_DETERMINED = 2,
PROVISIONAL = 3,
EPHEMERAL = 4,
}
7 changes: 7 additions & 0 deletions packages/firebase-messaging-core/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum AuthorizationStatus {
AUTHORIZED,
DENIED,
NOT_DETERMINED,
PROVISIONAL,
EPHEMERAL,
}
5 changes: 4 additions & 1 deletion packages/firebase-messaging-core/index.android.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
10 changes: 3 additions & 7 deletions packages/firebase-messaging-core/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export enum AuthorizationStatus {
AUTHORIZED,
DENIED,
NOT_DETERMINED,
PROVISIONAL,
EPHEMERAL,
}
import { AuthorizationStatus } from './common';

export { AuthorizationStatus };

export interface AndroidPermissions {}

Expand Down
11 changes: 7 additions & 4 deletions packages/firebase-messaging-core/index.ios.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -100,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down