NativeScript扩展UNUserNotificationCenter委托

时间:2016-09-26 11:07:30

标签: ios objective-c nativescript

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

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

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;

}

2 个答案:

答案 0 :(得分:1)

正确执行此操作的方法如下,将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;

}

答案 1 :(得分:1)

对于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;
}