我正在尝试使用 node.js 和 Apple 的 CloudKit.js 与我的 CloudKit 数据库进行交互。我成功地创建了服务器到服务器 key ,并且可以使用该程序添加、删除或编辑记录。添加订阅也有效。现在我希望它能够监视通知并在收到通知时打印一行。但是,启用通知对我来说失败了。我尝试注册的回调也没有被调用。
无论我打多少次电话registerForNotifications()
在我的容器上,打印属性 isRegisteredForNotifications
时,我总是收到false
.
我来自于以“桌面/移动/原生/嵌入式”为中心的编程语言背景,所以整个 Node.js、JavaScript、Promise 世界,尤其是 .then()
语法对我来说还是有点陌生。
这是我尝试用来接收通知的代码:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var fetch = require('node-fetch');
var CloudKit = require('./cloudkit');
var containerConfig = require('./config');
// A utility function for printing results to the console.
var println = function(key,value) {
console.log("--> " + key + ":");
console.log(value);
console.log();
};
//CloudKit configuration
CloudKit.configure({
services: {
fetch: fetch,
logger: console
},
containers: [ containerConfig ]
});
var container = CloudKit.getDefaultContainer();
var database = container.publicCloudDatabase; // We'll only make calls to the public database.
// Sign in using the keyID and public key file.
container.setUpAuth();
function renderNotification(notification) {
console.log('Got notification!')
};
CloudKit.getDefaultContainer().registerForNotifications();
CloudKit.getDefaultContainer().addNotificationListener(renderNotification);
console.log(CloudKit.getDefaultContainer().isRegisteredForNotifications); // Why does this print false?
即使在创建新订阅并使用 .then()
添加回调时也是如此,调用addNotificationListener
,该属性仍然显示 false
和renderNotification
永远不会被叫到。
这是 Apple CloudKit JS 中的错误还是我做错了什么?
此外,我想提一下,我能够使用我用 Python 封装的 JSON API ( here ) 成功完成相同的任务(创建、编辑、删除记录或订阅)。然而,似乎没有办法接收订阅通知,这就是为什么我在这里尝试使用node.js和Apple的CloudKit.js。如果我在这里遗漏了一些东西,并且实际上既不使用 CloudKit.js 也不使用 Cocoa API 来接收通知,我很乐意接受有关如何执行此操作的任何建议。
感谢您的帮助!
最佳答案
您可以通过以下方式注册通知并等待通知完成:
container.addNotificationListener(renderNotification);
container.registerForNotifications()
.then(function() {
console.log(container.isRegisteredForNotification) //should be true
})
.catch(function(error) {
console.error('there was an error:', error);
})
话虽如此,您还可以使用 WS 端点并进行所需的调用(例如使用 python)。启动并运行您的 Nodejs 方法可能仍然值得,以便您可以检查网络调用。
通过 WS 处理通知的相关文档位于此处:
关于python - CloudKit JS 通过 Node : Cannot register for notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349920/