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

Commit e18e546

Browse files
Merge pull request #912 from Danziger/master
Allows disabling automatic notification creation and always showing notifications when in foreground
2 parents e7ef841 + e1520c7 commit e18e546

File tree

3 files changed

+85
-49
lines changed

3 files changed

+85
-49
lines changed

docs/MESSAGING.md

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

113+
### Disable automatic notification creation
114+
115+
By default, this plugin will display a notification every time it receives one. If you want to disable this
116+
behaviour and handle the notifications yourself on the `onMessageReceivedCallback`, you need to set the
117+
`displayNotifications` option to `false`:
118+
119+
```js
120+
firebase.init({
121+
displayNotifications: false,
122+
});
123+
```
124+
125+
You can display or schedule notifications yourself using the plugin [`nativescript-local-notifications`](https://github.com/EddyVerbruggen/nativescript-local-notifications).
126+
127+
This might be helpful too if you or some other plugin you use is already setting the current notification center
128+
`delegate` property, as in that case adding another plugin that does that would result in a conflict.
129+
130+
### Always show notifications when the application is in foreground
131+
132+
If you always want to display notifications while the application is in foreground withouth sending additional
133+
parameters/data when sending the push notification, you need to set the `showWhenInForeground` option to `true`:
134+
135+
```js
136+
firebase.init({
137+
showWhenInForeground: true,
138+
});
139+
```
140+
113141
### Send messages to Topics
114142
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.
115143

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);
@@ -388,33 +393,35 @@ function _registerForRemoteNotifications() {
388393
}
389394
});
390395

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

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

417-
curNotCenter.delegate = _userNotificationCenterDelegate;
423+
curNotCenter.delegate = _userNotificationCenterDelegate;
424+
}
418425

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

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

src/scripts/postinstall.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,28 +2730,28 @@ function read (opts, cb) {
27302730
\***********************************/
27312731
/***/ (function(module, exports) {
27322732

2733-
module.exports = function(module) {
2734-
if(!module.webpackPolyfill) {
2735-
module.deprecate = function() {};
2736-
module.paths = [];
2737-
// module.parent = undefined by default
2738-
if(!module.children) module.children = [];
2739-
Object.defineProperty(module, "loaded", {
2740-
enumerable: true,
2741-
get: function() {
2742-
return module.l;
2743-
}
2744-
});
2745-
Object.defineProperty(module, "id", {
2746-
enumerable: true,
2747-
get: function() {
2748-
return module.i;
2749-
}
2750-
});
2751-
module.webpackPolyfill = 1;
2752-
}
2753-
return module;
2754-
};
2733+
module.exports = function(module) {
2734+
if(!module.webpackPolyfill) {
2735+
module.deprecate = function() {};
2736+
module.paths = [];
2737+
// module.parent = undefined by default
2738+
if(!module.children) module.children = [];
2739+
Object.defineProperty(module, "loaded", {
2740+
enumerable: true,
2741+
get: function() {
2742+
return module.l;
2743+
}
2744+
});
2745+
Object.defineProperty(module, "id", {
2746+
enumerable: true,
2747+
get: function() {
2748+
return module.i;
2749+
}
2750+
});
2751+
module.webpackPolyfill = 1;
2752+
}
2753+
return module;
2754+
};
27552755

27562756

27572757
/***/ }),

0 commit comments

Comments
 (0)