@@ -2,6 +2,7 @@ import * as applicationSettings from "tns-core-modules/application-settings";
22import * as application from "tns-core-modules/application/application" ;
33import { device } from "tns-core-modules/platform/platform" ;
44import { MessagingOptions } from "../firebase" ;
5+ import { DelegateObserver , SharedNotificationDelegate } from "nativescript-shared-notification-delegate" ;
56import { firebase } from "../firebase-common" ;
67import { firebaseUtils } from "../utils" ;
78import { IosInteractiveNotificationAction , IosInteractiveNotificationCategory , IosInteractiveNotificationType } from "./messaging" ;
@@ -13,7 +14,7 @@ let _pushToken: any;
1314let _receivedPushTokenCallback : Function ;
1415let _receivedNotificationCallback : Function ;
1516let _registerForRemoteNotificationsRanThisSession = false ;
16- let _userNotificationCenterDelegate : UNUserNotificationCenterDelegateImpl ;
17+ let _userNotificationCenterDelegateObserver : FirebaseNotificationDelegateObserverImpl ;
1718let _firebaseRemoteMessageDelegate : FIRMessagingDelegateImpl ;
1819let _showNotifications : boolean = true ;
1920let _showNotificationsWhenInForeground : boolean = false ;
@@ -393,7 +394,7 @@ function _registerForRemoteNotifications(resolve?, reject?) {
393394 } ) ;
394395
395396 if ( _showNotifications ) {
396- _userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl . new ( ) . initWithCallback ( ( unnotification , actionIdentifier ?, inputText ?) => {
397+ _userNotificationCenterDelegateObserver = new FirebaseNotificationDelegateObserverImpl ( ( unnotification , actionIdentifier ?, inputText ?) => {
397398 // if the app is in the foreground then this method will receive the notification
398399 // if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
399400 // if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
@@ -425,7 +426,7 @@ function _registerForRemoteNotifications(resolve?, reject?) {
425426 }
426427 } ) ;
427428
428- UNUserNotificationCenter . currentNotificationCenter ( ) . delegate = _userNotificationCenterDelegate ;
429+ SharedNotificationDelegate . addObserver ( _userNotificationCenterDelegateObserver ) ;
429430 }
430431
431432 if ( typeof ( FIRMessaging ) !== "undefined" ) {
@@ -516,27 +517,22 @@ function _addObserver(eventName, callback) {
516517 return NSNotificationCenter . defaultCenter . addObserverForNameObjectQueueUsingBlock ( eventName , null , NSOperationQueue . mainQueue , callback ) ;
517518}
518519
519- // see https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate?language=objc
520- class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNotificationCenterDelegate {
521- public static ObjCProtocols = [ ] ;
522-
523- static new ( ) : UNUserNotificationCenterDelegateImpl {
524- if ( UNUserNotificationCenterDelegateImpl . ObjCProtocols . length === 0 && typeof ( UNUserNotificationCenterDelegate ) !== "undefined" ) {
525- UNUserNotificationCenterDelegateImpl . ObjCProtocols . push ( UNUserNotificationCenterDelegate ) ;
526- }
527- return < UNUserNotificationCenterDelegateImpl > super . new ( ) ;
528- }
520+ class FirebaseNotificationDelegateObserverImpl implements DelegateObserver {
521+ observerUniqueKey = "firebase-messaging" ;
529522
530523 private callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ;
531524
532- public initWithCallback ( callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ) : UNUserNotificationCenterDelegateImpl {
525+ constructor ( callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ) {
533526 this . callback = callback ;
534- return this ;
535527 }
536528
537- public userNotificationCenterWillPresentNotificationWithCompletionHandler ( center : UNUserNotificationCenter , notification : UNNotification , completionHandler : ( p1 : UNNotificationPresentationOptions ) => void ) : void {
529+ public userNotificationCenterWillPresentNotificationWithCompletionHandler ( center : UNUserNotificationCenter , notification : UNNotification , completionHandler : ( p1 : UNNotificationPresentationOptions ) => void , next : ( ) => void ) : void {
538530 const userInfo = notification . request . content . userInfo ;
539531 const userInfoJSON = firebaseUtils . toJsObject ( userInfo ) ;
532+ if ( ! ( notification . request . trigger instanceof UNPushNotificationTrigger ) ) { // not a firebase message!
533+ next ( ) ;
534+ return ;
535+ }
540536
541537 if ( _showNotificationsWhenInForeground || // Default value, in case we always want to show when in foreground.
542538 userInfoJSON [ "gcm.notification.showWhenInForeground" ] === "true" || // This is for FCM, ...
@@ -551,7 +547,11 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
551547 this . callback ( notification ) ;
552548 }
553549
554- public userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler ( center : UNUserNotificationCenter , response : UNNotificationResponse , completionHandler : ( ) => void ) : void {
550+ public userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler ( center : UNUserNotificationCenter , response : UNNotificationResponse , completionHandler : ( ) => void , next : ( ) => void ) : void {
551+ if ( ! ( response . notification . request . trigger instanceof UNPushNotificationTrigger ) ) { // not a firebase message!
552+ next ( ) ;
553+ return ;
554+ }
555555 // let's ignore "dismiss" actions
556556 if ( response && response . actionIdentifier === UNNotificationDismissActionIdentifier ) {
557557 completionHandler ( ) ;
0 commit comments