NSUserNotificationCenter defaultUserNotificationCenter returns null in Qt application

南笙酒味 提交于 2019-12-02 05:44:02

问题


I have a simple test Qt application which tries to send user notification on OS X:

void Mac::notify(QString title, QString message) {
  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
  userNotification.title = title.toNSString();
  userNotification.informativeText = message.toNSString();
  NSUserNotificationCenter* center = [NSUserNotificationCenter defaultUserNotificationCenter];
  [center deliverNotification:userNotification];
}

The problem is NSUserNotificationCenter* center being null. I am using Qt 5.4.1, OS X 10.10. main function looks like this:

int main(int argc, char** argv) {
  QApplication a(argc, argv);
  App app;
  QQmlApplicationEngine engine;
  engine.rootContext()->setContextProperty("app", &app);
  engine.load(QUrl("qrc:/Main.qml"));
  return a.exec();
}

And I try to send notification on a mouse click

MouseArea {
  anchors.fill: parent
  onClicked: app.notify("Hello", "World")
}

Does anyone have an idea why it doesn't work?


回答1:


Finally I found the problem. My Info.plist file was missing CFBundleIdentifier. After I added it, application was able to register in Notification Center.

<key>CFBundleIdentifier</key>
<string>org.notifytestosx.app</string>


来源:https://stackoverflow.com/questions/29341643/nsusernotificationcenter-defaultusernotificationcenter-returns-null-in-qt-applic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!