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

Commit 1003ff4

Browse files
committed
initial support for shared delegate
1 parent eff2cde commit 1003ff4

File tree

2 files changed

+12
-19
lines changed

2 files changed

+12
-19
lines changed

src/messaging/messaging.ios.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
IosInteractiveNotificationType
1111
} from "./messaging";
1212
import { MessagingOptions } from "../firebase";
13+
import { DelegateObserver, SharedNotificationDelegate } from "nativescript-shared-notification-delegate";
1314

1415
let _notificationActionTakenCallback: Function;
1516
let _pendingNotifications: Array<any> = [];
@@ -18,7 +19,7 @@ let _pushToken: any;
1819
let _receivedPushTokenCallback: Function;
1920
let _receivedNotificationCallback: Function;
2021
let _registerForRemoteNotificationsRanThisSession = false;
21-
let _userNotificationCenterDelegate: UNUserNotificationCenterDelegateImpl;
22+
let _userNotificationCenterDelegateObserver: FirebaseNotificationDelegateObserverImpl;
2223
let _messagingConnected: boolean = null;
2324
let _firebaseRemoteMessageDelegate: FIRMessagingDelegateImpl;
2425
let _showNotifications: boolean = true;
@@ -407,7 +408,7 @@ function _registerForRemoteNotifications() {
407408
});
408409

409410
if (_showNotifications) {
410-
_userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl.new().initWithCallback((unnotification, actionIdentifier?, inputText?) => {
411+
_userNotificationCenterDelegateObserver = new FirebaseNotificationDelegateObserverImpl((unnotification, actionIdentifier?, inputText?) => {
411412
// if the app is in the foreground then this method will receive the notification
412413
// if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
413414
// if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
@@ -434,7 +435,7 @@ function _registerForRemoteNotifications() {
434435
}
435436
});
436437

437-
curNotCenter.delegate = _userNotificationCenterDelegate;
438+
SharedNotificationDelegate.addObserver(_userNotificationCenterDelegateObserver);
438439
}
439440

440441
if (typeof (FIRMessaging) !== "undefined") {
@@ -546,25 +547,16 @@ function _addObserver(eventName, callback) {
546547
return NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(eventName, null, NSOperationQueue.mainQueue, callback);
547548
}
548549

549-
// see https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate?language=objc
550-
class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNotificationCenterDelegate {
551-
public static ObjCProtocols = [];
552-
553-
static new(): UNUserNotificationCenterDelegateImpl {
554-
if (UNUserNotificationCenterDelegateImpl.ObjCProtocols.length === 0 && typeof (UNUserNotificationCenterDelegate) !== "undefined") {
555-
UNUserNotificationCenterDelegateImpl.ObjCProtocols.push(UNUserNotificationCenterDelegate);
556-
}
557-
return <UNUserNotificationCenterDelegateImpl>super.new();
558-
}
550+
class FirebaseNotificationDelegateObserverImpl implements DelegateObserver {
551+
observerUniqueKey = "firebase-messaging";
559552

560553
private callback: (unnotification: UNNotification, actionIdentifier?: string, inputText?: string) => void;
561554

562-
public initWithCallback(callback: (unnotification: UNNotification, actionIdentifier?: string, inputText?: string) => void): UNUserNotificationCenterDelegateImpl {
555+
constructor(callback: (unnotification: UNNotification, actionIdentifier?: string, inputText?: string) => void) {
563556
this.callback = callback;
564-
return this;
565557
}
566558

567-
public userNotificationCenterWillPresentNotificationWithCompletionHandler(center: UNUserNotificationCenter, notification: UNNotification, completionHandler: (p1: UNNotificationPresentationOptions) => void): void {
559+
public userNotificationCenterWillPresentNotificationWithCompletionHandler(center: UNUserNotificationCenter, notification: UNNotification, completionHandler: (p1: UNNotificationPresentationOptions) => void, next: () => void): void {
568560
const userInfo = notification.request.content.userInfo;
569561
const userInfoJSON = firebaseUtils.toJsObject(userInfo);
570562

@@ -582,7 +574,7 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
582574
}
583575
}
584576

585-
public userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler(center: UNUserNotificationCenter, response: UNNotificationResponse, completionHandler: () => void): void {
577+
public userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler(center: UNUserNotificationCenter, response: UNNotificationResponse, completionHandler: () => void, next: () => void): void {
586578
// let's ignore "dismiss" actions
587579
if (response && response.actionIdentifier === UNNotificationDismissActionIdentifier) {
588580
completionHandler();

src/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@
127127
"dependencies": {
128128
"fs-extra": "~2.1.0",
129129
"nativescript-hook": "~0.2.4",
130+
"nativescript-shared-notification-delegate": "0.0.2",
130131
"prompt-lite": "~0.1.0",
131-
"xcode": "~0.9.0",
132-
"semver": "^5.0.0"
132+
"semver": "^5.0.0",
133+
"xcode": "~0.9.0"
133134
},
134135
"devDependencies": {
135136
"rimraf": "^2.6.2",

0 commit comments

Comments
 (0)