我正在尝试扩展UNUserNotificationCenter的委托。
根据Apple的文档,这必须在didFinishLaunchingWithOptions
处完成。 (https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate)
已经存在有关如何使代码在应用程序的此部分中运行的文档。但是,我有点不确定我是否了解这种用法的语义。在这里,我尝试扩展UNUserNotificationCenterDelegate
并将其分配给当前的center委托属性,但是当我收到本地通知时,这两个函数均未运行:
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;
}
关于ios - NativeScript扩展UNUserNotificationCenter委托(delegate),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39701209/