DockTitle以及消息通知提示的研究

14 篇文章 0 订阅
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 {

}

       &#160完整的代码部分:

#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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值