Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit b7a2948

Browse files
author
Daniel Gámez Franco
committed
Adds options to disable automatic notification creation (which sets the current notification center delegate) and to always handle notification when the application is in foreground.
1 parent 2efd4b0 commit b7a2948

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

docs/MESSAGING.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ Similarly to the message callback you can either wire this through `init` or as
128128
);
129129
```
130130

131+
### Disable automatic notification creation
132+
133+
By default, this plugin will display a notification every time it receives one. If you want to disable this
134+
behaviour and handle the notifications yourself on the `onMessageReceivedCallback`, you need to set the
135+
`displayNotifications` option to `false`:
136+
137+
```js
138+
firebase.init({
139+
displayNotifications: false,
140+
});
141+
```
142+
143+
You can display or schedule notifications yourself using the plugin [`nativescript-local-notifications`](https://github.com/EddyVerbruggen/nativescript-local-notifications).
144+
145+
This might be helpful too if you or some other plugin you use is already setting the current notification center
146+
`delegate` property, as in that case adding another plugin that does that would result in a conflict.
147+
148+
### Always show notifications when the application is in foreground
149+
150+
If you always want to display notifications while the application is in foreground withouth sending additional
151+
parameters/data when sending the push notification, you need to set the `showWhenInForeground` option to `true`:
152+
153+
```js
154+
firebase.init({
155+
showWhenInForeground: true,
156+
});
157+
```
158+
131159
### Send messages to Topics
132160
Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.
133161

src/messaging/messaging.ios.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ let _registerForRemoteNotificationsRanThisSession = false;
2020
let _userNotificationCenterDelegate: UNUserNotificationCenterDelegateImpl;
2121
let _messagingConnected: boolean = null;
2222
let _firebaseRemoteMessageDelegate: FIRMessagingDelegateImpl;
23+
let _displayNotifications: boolean = true;
24+
let _showWhenInForeground: boolean = false;
2325

2426
// Track whether or not registration for remote notifications was request.
2527
// This way we can suppress the "Allow notifications" consent popup until the listeners are passed in.
2628
const NOTIFICATIONS_REGISTRATION_KEY = "Firebase-RegisterForRemoteNotifications";
2729

2830
export function initFirebaseMessaging(arg) {
31+
_displayNotifications = arg.displayNotifications === undefined ? _displayNotifications : !!arg.displayNotifications;
32+
_showWhenInForeground = arg.showWhenInForeground === undefined ? _showWhenInForeground : !!arg.showWhenInForeground;
33+
2934
if (arg.onMessageReceivedCallback !== undefined || arg.onPushTokenReceivedCallback !== undefined) {
3035
if (arg.onMessageReceivedCallback !== undefined) {
3136
addOnMessageReceivedCallback(arg.onMessageReceivedCallback);
@@ -387,33 +392,35 @@ function _registerForRemoteNotifications() {
387392
}
388393
});
389394

390-
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initWithCallback((unnotification, actionIdentifier?, inputText?) => {
391-
// if the app is in the foreground then this method will receive the notification
392-
// if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
393-
// if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
394-
const userInfo = unnotification.request.content.userInfo;
395-
const userInfoJSON = firebaseUtils.toJsObject(userInfo);
396-
updateUserInfo(userInfoJSON);
397-
398-
if (actionIdentifier) {
399-
_pendingActionTakenNotifications.push({
400-
actionIdentifier,
401-
userInfoJSON,
402-
inputText
403-
});
404-
if (_notificationActionTakenCallback) {
405-
_processPendingActionTakenNotifications();
395+
if (_displayNotifications) {
396+
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initWithCallback((unnotification, actionIdentifier?, inputText?) => {
397+
// if the app is in the foreground then this method will receive the notification
398+
// if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
399+
// if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
400+
const userInfo = unnotification.request.content.userInfo;
401+
const userInfoJSON = firebaseUtils.toJsObject(userInfo);
402+
updateUserInfo(userInfoJSON);
403+
404+
if (actionIdentifier) {
405+
_pendingActionTakenNotifications.push({
406+
actionIdentifier,
407+
userInfoJSON,
408+
inputText
409+
});
410+
if (_notificationActionTakenCallback) {
411+
_processPendingActionTakenNotifications();
412+
}
406413
}
407-
}
408414

409-
userInfoJSON.foreground = true;
410-
_pendingNotifications.push(userInfoJSON);
411-
if (_receivedNotificationCallback) {
412-
_processPendingNotifications();
413-
}
414-
});
415+
userInfoJSON.foreground = true;
416+
_pendingNotifications.push(userInfoJSON);
417+
if (_receivedNotificationCallback) {
418+
_processPendingNotifications();
419+
}
420+
});
415421

416-
curNotCenter.delegate = _userNotificationCenterDelegate;
422+
curNotCenter.delegate = _userNotificationCenterDelegate;
423+
}
417424

418425
if (typeof (FIRMessaging) !== "undefined") {
419426
_firebaseRemoteMessageDelegate = FIRMessagingDelegateImpl.new().initWithCallback((appDataDictionary: NSDictionary<any, any>) => {
@@ -543,9 +550,10 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
543550
const userInfo = notification.request.content.userInfo;
544551
const userInfoJSON = firebaseUtils.toJsObject(userInfo);
545552

546-
if (userInfoJSON["gcm.notification.showWhenInForeground"] === "true" || // this is for FCM
547-
userInfoJSON["showWhenInForeground"] === true || // this is for non-FCM,
548-
(userInfoJSON.aps && userInfoJSON.aps.showWhenInForeground === true) // and this as well (so users can choose where to put it)
553+
if ( _showWhenInForeground || // Default value, in case we always want to show when in foreground.
554+
userInfoJSON["gcm.notification.showWhenInForeground"] === "true" || // This is for FCM, ...
555+
userInfoJSON["showWhenInForeground"] === true || // ...this is for non-FCM...
556+
(userInfoJSON.aps && userInfoJSON.aps.showWhenInForeground === true) // ...and this as well (so users can choose where to put it).
549557
) {
550558
// don't invoke the callback here, since the app shouldn't fi. navigate to a new page unless the user pressed the notification
551559
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-plugin-firebase",
3-
"version": "6.8.1",
3+
"version": "6.9.0",
44
"description": "Fire. Base. Firebase!",
55
"main": "firebase",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)