Mac OS开发 DockTile以及消息通知提示
来源:互联网 发布:利润表数据分析 编辑:程序博客网 时间:2024/06/10 01:20
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
- Mac OS开发 DockTile以及消息通知提示
- DockTitle以及消息通知提示的研究
- 通知消息提示
- OS X 消息通知:NSUserNotification
- Mac OS X: 系统通知软件
- [MAC OS]第一次接触MAC OS开发
- mac os开发资料
- 【Android开发】消息提示框与对话框-使用Notification在状态栏上显示通知
- android系统通知栏提示消息
- 消息通知声音和振动提示
- Toast消息提示和Notification通知
- Android消息通知栏开发
- 【数据库开发】Redis消息通知
- IOS开发报错-"Mac OS X"想要进行更改,多次提示
- Mac OS 修改终端提示前缀
- Mac OS 文件系统开发框架
- Mac OS 开发环境设置
- mac os开发环境搭建
- Unity 给代码添加命名空间
- Electron 快速入门
- 深度学习发展历程
- Kubernetes,微服务以及 Service Mesh
- Abaqus中输出场文件odb的数据结构及Python读取
- Mac OS开发 DockTile以及消息通知提示
- boost::asio::deadline_timer(理解)
- struts2
- 计算机基础---电脑的原理
- Xgboost原理详解
- 安卓xml布局中 android:paddingBottom="@dimen/activity_vertical_margin"是什么意思?
- sessionID一直变化。
- Hello, World
- UVa11178