我正在嘗試在 C 代碼庫中的 MacOS 上實作推送通知。理想情況下,只有一個 Objective-C 檔案包含 (1) 一個我可以呼叫的公共 C 函式和 (2) 一些我可以用來拋出通知的 Objective-C 代碼。這樣,源檔案可以在構建程序中無縫編譯和鏈接。
為此,我一直在嘗試創建一個最小的示例,該示例可以僅使用單個.m檔案(不是整個 XCode 專案)拋出通知,就像NSUserNotificationCenter 中討論的不顯示通知一樣。但是,有兩個問題:
- 盡管嘗試了上述鏈接中的解決方案,但我仍然無法使代碼正常作業。它編譯并運行但不拋出通知。
- 如果可能,我們希望切換到新的用戶通知 API。不過,如果目前無法實作,也沒什么大不了的。
這是我迄今為止嘗試過的:
#import <Foundation/Foundation.h>
#import <Foundation/NSUserNotification.h>
@interface AppDelegate : NSObject <NSUserNotificationCenterDelegate>
@end
@implementation AppDelegate
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
- (void)throwNotification {
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";
printf("trying to throw {%s %s}\n", [[userNotification title] UTF8String], [[userNotification informativeText] UTF8String]);
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
}
@end
int main (int argc, const char * argv[]) {
AppDelegate *app = [[AppDelegate alloc] init];
[app throwNotification];
return 0;
}
這是用cc -framework Foundation -o app main.m.
任何見解將不勝感激!
uj5u.com熱心網友回復:
問題是為了顯示通知,您需要有一個合適的 bundle identifier。
我們將從這里獲取一些代碼,在那里我們等待通知顯示。我們可以將 Info.plist 檔案嵌入到編譯后的二進制檔案中,這將完成與名為 的檔案中的 swizzling 代碼相同的事情notify.m:
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject<NSUserNotificationCenterDelegate>
@property (nonatomic, assign) BOOL keepRunning;
@end
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *appdel = [[AppDelegate alloc] init];
app.delegate = appdel;
NSUserNotificationCenter *nc = [NSUserNotificationCenter defaultUserNotificationCenter];
nc.delegate = appdel;
appdel.keepRunning = TRUE;
NSUserNotification *userNotification = [[NSUserNotification alloc] init];
userNotification.title = @"Some title";
userNotification.informativeText = @"Some text";
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
while (appdel.keepRunning) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
return 0;
}
@implementation AppDelegate
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
self.keepRunning = NO;
}
@end
我構建了一個Info.plist包含以下內容的檔案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.apple.finder</string>
</dict>
</plist>
然后我使用以下方法編譯它:
clang -o notify -framework Cocoa notify.m -Wl,-sectcreate,_\_TEXT,__info_plist,Info.plist
\該構建行中有一個用于避免下劃線的降價決議,但實際構建命令列不需要它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/403868.html
標籤:
