NSUserNotificationCenter defaultUserNotificationCe

2019-03-02 15:36发布

问题:

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>