@@ -2,6 +2,7 @@ import * as applicationSettings from "tns-core-modules/application-settings";
2
2
import * as application from "tns-core-modules/application/application" ;
3
3
import { device } from "tns-core-modules/platform/platform" ;
4
4
import { MessagingOptions } from "../firebase" ;
5
+ import { DelegateObserver , SharedNotificationDelegate } from "nativescript-shared-notification-delegate" ;
5
6
import { firebase } from "../firebase-common" ;
6
7
import { firebaseUtils } from "../utils" ;
7
8
import { IosInteractiveNotificationAction , IosInteractiveNotificationCategory , IosInteractiveNotificationType } from "./messaging" ;
@@ -13,7 +14,7 @@ let _pushToken: any;
13
14
let _receivedPushTokenCallback : Function ;
14
15
let _receivedNotificationCallback : Function ;
15
16
let _registerForRemoteNotificationsRanThisSession = false ;
16
- let _userNotificationCenterDelegate : UNUserNotificationCenterDelegateImpl ;
17
+ let _userNotificationCenterDelegateObserver : FirebaseNotificationDelegateObserverImpl ;
17
18
let _firebaseRemoteMessageDelegate : FIRMessagingDelegateImpl ;
18
19
let _showNotifications : boolean = true ;
19
20
let _showNotificationsWhenInForeground : boolean = false ;
@@ -393,7 +394,7 @@ function _registerForRemoteNotifications(resolve?, reject?) {
393
394
} ) ;
394
395
395
396
if ( _showNotifications ) {
396
- _userNotificationCenterDelegate = UNUserNotificationCenterDelegateImpl . new ( ) . initWithCallback ( ( unnotification , actionIdentifier ?, inputText ?) => {
397
+ _userNotificationCenterDelegateObserver = new FirebaseNotificationDelegateObserverImpl ( ( unnotification , actionIdentifier ?, inputText ?) => {
397
398
// if the app is in the foreground then this method will receive the notification
398
399
// if the app is in the background, and user has responded to interactive notification, then this method will receive the notification
399
400
// if the app is in the background, and user views a notification, applicationDidReceiveRemoteNotificationFetchCompletionHandler will receive it
@@ -425,7 +426,7 @@ function _registerForRemoteNotifications(resolve?, reject?) {
425
426
}
426
427
} ) ;
427
428
428
- UNUserNotificationCenter . currentNotificationCenter ( ) . delegate = _userNotificationCenterDelegate ;
429
+ SharedNotificationDelegate . addObserver ( _userNotificationCenterDelegateObserver ) ;
429
430
}
430
431
431
432
if ( typeof ( FIRMessaging ) !== "undefined" ) {
@@ -516,27 +517,22 @@ function _addObserver(eventName, callback) {
516
517
return NSNotificationCenter . defaultCenter . addObserverForNameObjectQueueUsingBlock ( eventName , null , NSOperationQueue . mainQueue , callback ) ;
517
518
}
518
519
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" ;
529
522
530
523
private callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ;
531
524
532
- public initWithCallback ( callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ) : UNUserNotificationCenterDelegateImpl {
525
+ constructor ( callback : ( unnotification : UNNotification , actionIdentifier ?: string , inputText ?: string ) => void ) {
533
526
this . callback = callback ;
534
- return this ;
535
527
}
536
528
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 {
538
530
const userInfo = notification . request . content . userInfo ;
539
531
const userInfoJSON = firebaseUtils . toJsObject ( userInfo ) ;
532
+ if ( ! ( notification . request . trigger instanceof UNPushNotificationTrigger ) ) { // not a firebase message!
533
+ next ( ) ;
534
+ return ;
535
+ }
540
536
541
537
if ( _showNotificationsWhenInForeground || // Default value, in case we always want to show when in foreground.
542
538
userInfoJSON [ "gcm.notification.showWhenInForeground" ] === "true" || // This is for FCM, ...
@@ -551,7 +547,11 @@ class UNUserNotificationCenterDelegateImpl extends NSObject implements UNUserNot
551
547
this . callback ( notification ) ;
552
548
}
553
549
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
+ }
555
555
// let's ignore "dismiss" actions
556
556
if ( response && response . actionIdentifier === UNNotificationDismissActionIdentifier ) {
557
557
completionHandler ( ) ;
0 commit comments