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

Commit e1520c7

Browse files
authored
Merge pull request #1 from Danziger/feature/disable-auto-notification-creation-n-add-global-showwheninforeground
Disable automatic notification creation and always handle when in foreground options
2 parents 2efd4b0 + 24b3378 commit e1520c7

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
@@ -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/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)