Mac OS开发 DockTile以及消息通知提示

来源:互联网 发布:利润表数据分析 编辑:程序博客网 时间:2024/06/10 01:20
Mac OS开发 DockTile以及消息通知提示

        Mac OS开发中,涉及到消息推送的功能。比如QQ,当有消息的时候,Dock栏上的图标显示消息的个数,并且在右上角通知中心有消息提示。

        查阅Apple的文档,发现NSDockTile和NSUserNotificationCenter类可以完成此项功能。

        便于功能的重复使用,现将此需求单独封装成一个独立的功能模块。对于DockTitle我们只需要调用setBadgeLabel设置想要提示的信息即可在Dock栏上的图标显示了。 
NSUserNotification所需信息比较多,包括Title、subtitle、informativeText以及contentImage。还需要处理一些代理的回调,用来完成特定的任务。响应回调函数如下:

@protocol NSUserNotificationCenterDelegate <NSObject>
@optional

// Sent to the delegate when a notification delivery date has arrived. At this time, the notification has either been presented to the user or the notification center has decided not to present it because your application was already frontmost.
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification;

// Sent to the delegate when a user clicks on a notification in the notification center. This would be a good time to take action in response to user interacting with a specific notification.
// Important: If want to take an action when your application is launched as a result of a user clicking on a notification, be sure to implement the applicationDidFinishLaunching: method on your NSApplicationDelegate. The notification parameter to that method has a userInfo dictionary, and that dictionary has the NSApplicationLaunchUserNotificationKey key. The value of that key is the NSUserNotification that caused the application to launch. The NSUserNotification is delivered to the NSApplication delegate because that message will be sent before your application has a chance to set a delegate for the NSUserNotificationCenter.
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification;

// Sent to the delegate when the Notification Center has decided not to present your notification, for example when your application is front most. If you want the notification to be displayed anyway, return YES.
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;

@end

其余实现代码,如下:

#import "XFUserNotificationCenter.h"
#import <AppKit/AppKit.h>

static XFUserNotificationCenter *_instance = nil;

@interface XFUserNotificationCenter () <NSUserNotificationCenterDelegate>

@end

@implementation XFUserNotificationCenter

+ (XFUserNotificationCenter *)sharedUserNotificationCenter
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[XFUserNotificationCenter alloc] init];
});
return _instance;
}

- (void)showUserNotificationTitle:(NSString *)title withSubTitle:(NSString *)subTitle withInformativeText:(NSString *)informativeText withContentImage:(NSImage *)contentImage
{
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:title];
[notification setSubtitle:subTitle];
[notification setInformativeText:informativeText];
[notification setContentImage:contentImage];

NSUserNotificationCenter *userNotificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
userNotificationCenter.delegate = self;
[userNotificationCenter scheduleNotification:notification];
}

- (void)showNotificaitonInDockWithNumberText:(NSString *)numberText
{
[[[NSApplication sharedApplication] dockTile] setBadgeLabel:numberText];
}

github demo:https://github.com/LingLemon/XFUserNotification




原创粉丝点击