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

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值