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

Commit e33cc23

Browse files
Merge pull request #1272 from edusperoni/shared-delegate
Use notification shared delegate on iOS
2 parents ff82b33 + 3b36a9f commit e33cc23

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/messaging/messaging.ios.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as applicationSettings from "tns-core-modules/application-settings";
22
import * as application from "tns-core-modules/application/application";
33
import { device } from "tns-core-modules/platform/platform";
44
import { MessagingOptions } from "../firebase";
5+
import { DelegateObserver, SharedNotificationDelegate } from "nativescript-shared-notification-delegate";
56
import { firebase } from "../firebase-common";
67
import { firebaseUtils } from "../utils";
78
import { IosInteractiveNotificationAction, IosInteractiveNotificationCategory, IosInteractiveNotificationType } from "./messaging";
@@ -13,7 +14,7 @@ let _pushToken: any;
1314
let _receivedPushTokenCallback: Function;
1415
let _receivedNotificationCallback: Function;
1516
let _registerForRemoteNotificationsRanThisSession = false;
16-
let _userNotificationCenterDelegate: UNUserNotificationCenterDelegateImpl;
17+
let _userNotificationCenterDelegateObserver: FirebaseNotificationDelegateObserverImpl;
1718
let _firebaseRemoteMessageDelegate: FIRMessagingDelegateImpl;
1819
let _showNotifications: boolean = true;
1920
let _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();

src/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
"dependencies": {
136136
"fs-extra": "~2.1.0",
137137
"nativescript-hook": "~0.2.5",
138+
"nativescript-shared-notification-delegate": "1.0.0",
138139
"prompt-lite": "~0.1.1",
139140
"xcode": "~0.9.3",
140141
"semver": "~5.7.1"

0 commit comments

Comments
 (0)