NativeScript 扩展了 UNUserNotificationCenter 委托

NativeScript extend the UNUserNotificationCenter delegate

我正在尝试扩展 UNUserNotificationCenter 的委托。 根据 Apple 文档,这必须在 didFinishLaunchingWithOptions 完成。 (https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate)

在应用程序的这一部分中,已经存在有关如何将代码获取到 运行 的文档。但是,我有点不确定我是否理解其工作原理的语义。在这里,我尝试扩展 UNUserNotificationCenterDelegate 并将其分配给当前的中心委托 属性,但是当我收到本地通知时,这两个函数都不是 运行:

if (application.ios) {


    var __extends = this.__extends || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };

    var appDelegate = (function (_super, _notiCenter) {
       __extends(appDelegate, _super);

       function appDelegate() {
           _super.apply(this, arguments);
       }

       function UNUserNotificationCenterDelegate(){
           _notiCenter.apply(this, arguments);
       }    

       appDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {

       UNUserNotificationCenterDelegate.prototype.userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler = function(center, notif, completion){
                console.log('We are here');
            }

            UNUserNotificationCenterDelegate.prototype.userNotificationCenterWillPresentNotificationWithCompletionHandler = function(center, notif, completion){
                console.log('We are here 2');   
            }      

            var center = utils.ios.getter(UNUserNotificationCenter, UNUserNotificationCenter.currentNotificationCenter);

            center.delegate = UNUserNotificationCenterDelegate;   

       };


       appDelegate.ObjCProtocols = [UIApplicationDelegate];
       return appDelegate;

    })(UIResponder, UNUserNotificationCenterDelegate);

   application.ios.delegate = appDelegate;

}

正确的做法如下,将 USUserNotificationCenterDelegate 添加到 appDelegate.ObjCProtocols 数组:

if (application.ios) {

    var __extends = this.__extends || function (d, b) {
        for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };

    var appDelegate = (function (_super ) {

        __extends(appDelegate, _super);

        function appDelegate() {
            _super.apply(this, arguments);
        }

        appDelegate.prototype.userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler = function(center, notif, completion){
            completion();
        }

        appDelegate.prototype.userNotificationCenterWillPresentNotificationWithCompletionHandler = function(center, notif, completion){
            completion(4);  
        }    

        appDelegate.prototype.applicationDidFinishLaunchingWithOptions = function (application, launchOptions) {

            var center = utils.ios.getter(UNUserNotificationCenter, UNUserNotificationCenter.currentNotificationCenter);
            center.delegate = this; 

        };

        appDelegate.ObjCProtocols = [UIApplicationDelegate, UNUserNotificationCenterDelegate];
        return appDelegate;

    })(UIResponder);

    application.ios.delegate = appDelegate;

}

对于main.ts

import application = require("tns-core-modules/application");
import { device } from "tns-core-modules/platform/platform";
import { ios as iOSUtils } from "tns-core-modules/utils/utils";

if (application.ios) {
    class MyDelegate extends UIResponder implements UIApplicationDelegate, UNUserNotificationCenterDelegate {
        public static ObjCProtocols = [UIApplicationDelegate, UNUserNotificationCenterDelegate];

        applicationDidFinishLaunchingWithOptions(application:UIApplication, launchOptions:NSDictionary<any, any>):boolean {
            console.log("-----> applicationWillFinishLaunchingWithOptions: " + launchOptions)

            if (parseInt(device.osVersion) >= 10) {
                const authorizationOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Badge;

                const curNotCenter = iOSUtils.getter(UNUserNotificationCenter, UNUserNotificationCenter.currentNotificationCenter);
                curNotCenter.delegate = this
                curNotCenter.requestAuthorizationWithOptionsCompletionHandler(authorizationOptions, (granted, error) => {
                if (!error) {
                    if (application === null) {
                        application = iOSUtils.getter(UIApplication, UIApplication.sharedApplication);
                    }
                    if (application !== null) {
                        application.registerForRemoteNotifications()
                    }
                } else {
                    console.log("Error requesting push notification auth: " + error);
                }
                });            
            } else {
                const notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationActivationMode.Background;
                const notificationSettings = UIUserNotificationSettings.settingsForTypesCategories(notificationTypes, null);
                application.registerForRemoteNotifications(); // prompts the user to accept notifications
                application.registerUserNotificationSettings(notificationSettings);
            }
            return true
        }

        applicationDidBecomeActive(application:UIApplication):void {
            console.log("-----> applicationDidBecomeActive: " + application)
        }

        applicationDidEnterBackground(application: UIApplication) {
            console.log("-----> applicationDidEnterBackground --->")
        }

        applicationHandleOpenURL(application: UIApplication, url: NSURL): boolean {
            console.log("-----> applicationHandleOpenURL --->")
            return true
        }

        applicationDidRegisterUserNotificationSettings() {
            console.log("-----> applicationDidRegisterUserNotificationSettings --->")
        }

        applicationDidFailToRegisterForRemoteNotificationsWithError(application: UIApplication, error: NSError) {
            console.log("-----> Error " + error)
        }

        applicationDidRegisterForRemoteNotificationsWithDeviceToken(application: UIApplication,deviceToken : NSData){
            console.log("Device token " + deviceToken)
        }

        applicationDidReceiveRemoteNotification(application: UIApplication , notification: any){
            console.log("applicationDidReceiveRemoteNotification"+ notification)
        }
        applicationContinueUserActivityRestorationHandler(application: UIApplication, userActivity: NSUserActivity, restorationHandler: (p1: NSArray<UIUserActivityRestoring>) => void): boolean {
            console.log("applicationContinueUserActivityRestorationHandler"+ userActivity)
            return true
        }

        userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler(center: UNUserNotificationCenter, response: UNNotificationResponse, completionHandler:()=>void) {
            completionHandler()
        }

        userNotificationCenterWillPresentNotificationWithCompletionHandler?(center: UNUserNotificationCenter, notification: UNNotification, completionHandler: (p1: UNNotificationPresentationOptions) => void) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound)
        }
    }
    application.ios.delegate = MyDelegate;
}