I am really a newbie in osX development environment. My application is developed for cross-platform, written in C++ without using any framework and non-gui. I want to add notification mechanism to my application for osX. My os version is Monterey now.
I tried to write an application by inspiring from this repo objective-c in c. But this repository is using a deprecated method that NSUserNotificationCenter. Apple recommends that method create any local notification.
After then wrote this code,
#define OBJC_OLD_DISPATCH_PROTOTYPES 1
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc-runtime.h>
int main(int argc, char** argv) {
Class contentClass = objc_getClass("UNMutableNotificationContent");
id content = objc_msgSend((id)contentClass, sel_registerName("alloc"), sel_registerName("init"));
Ivar titleIvar = class_getInstanceVariable(contentClass, "title");
object_setIvar((id)contentClass, titleIvar, objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "asdas"));
Ivar bodyIvar = class_getInstanceVariable(contentClass, "body");
object_setIvar((id)contentClass, bodyIvar, objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "asdasdasdasd"));
id notificationTrigger = objc_msgSend((id)objc_getClass("UNTimeIntervalNotificationTrigger"), sel_registerName("triggerWithTimeInterval:repeats:"), 5, true);
id notificationRequest = objc_msgSend((id)objc_getClass("UNNotificationRequest"), sel_registerName("requestWithIdentifier:content:trigger:"), objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "FiveSecond"), (id)contentClass, notificationTrigger);
id center = objc_msgSend((id)objc_getClass("UNUserNotificationCenter"), sel_registerName("currentNotificationCenter"));
objc_msgSend(center, sel_registerName("addNotificationRequest:"), notificationRequest);
return 0;
}
but it did not work. May I have misunderstood the usage of the runtime library of Objective-c.
I don`t want to migrate my application to any language, it should keep in C++.