DockTitle以及消息通知提示的研究
开发工作中,有时会涉及到消息推送的功能。比如类似QQ,当有消息的时候,Dock栏上的图标显示消息的个数,并且在右上角通知中心有消息提示。
查阅Apple的文档,发现NSDockTile和NSUserNotificationCenter类可以完成此项功能。
便于功能的重复使用,现将此需求单独封装成一个独立的功能模块。对于DockTitle我们只需要调用setBadgeLabel设置想要提示的信息即可在Dock栏上的图标显示了。
NSUserNotification所需信息比较多,包括Title、subtitle以及informativeText。还需要处理一些代理的回调,用来完成特定的任务。响应回调函数如下:
//是否开启显示消息通知
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
//用户点击消息提示时触发该回调
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
}
//消息正式发送成功时触发该回调
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification {
}
 完整的代码部分:
#import <Foundation/Foundation.h>
@protocol NotifyTipDelegate <NSObject>
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification;
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification ;
@end
/**
* @brief NotifyTip
* @note
* 消息提示:包括用户中心提示,以及dock提示
*/
@interface NotifyTip : NSObject <NSUserNotificationCenterDelegate>
+ (NotifyTip*)sharedNotifyTip;
@property (assign) id<NotifyTipDelegate>delegate;
- (void)showUserNotificationTitle:(NSString*)title
withSubTitle:(NSString*)subTitle
withInformativeText:(NSString*)informativeText;
- (void)showNotificaitonInDock:(NSString*)informativeText;
@end
#import "NotifyTip.h"
#import <AppKit/AppKit.h>
@implementation NotifyTip
@synthesize delegate;
+ (NotifyTip*)sharedNotifyTip {
static NotifyTip *_sharedNotifyTip = nil;
if (_sharedNotifyTip == nil) {
_sharedNotifyTip = [[self alloc]init];
}
return _sharedNotifyTip;
}
- (void)showUserNotificationTitle:(NSString*)title
withSubTitle:(NSString*)subTitle
withInformativeText:(NSString*)informativeText {
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:title];
[notification setSubtitle:subTitle];
[notification setInformativeText:informativeText];
NSUserNotificationCenter *userNotificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
userNotificationCenter.delegate = self;
[userNotificationCenter scheduleNotification:notification];
}
- (void)showNotificaitonInDock:(NSString*)informativeText {
[[[NSApplication sharedApplication] dockTile] setBadgeLabel:informativeText];
}
#pragma mark NSUserNotificationCenterDelegate
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
return YES;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
if (delegate && [delegate respondsToSelector:@selector(userNotificationCenter:didActivateNotification:)] == YES) {
[delegate userNotificationCenter:center didActivateNotification:notification];
}
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification {
if (delegate && [delegate respondsToSelector:@selector(userNotificationCenter:didDeliverNotification:)] == YES) {
[delegate userNotificationCenter:center didDeliverNotification:notification];
}
}
@end
最后附上github上的源码:https://github.com/FyhSky/NotificationCenterTips
转载请注明出处:http://blog.csdn.net/skynullcode