1.1 初始化NSUserNotification
//通知已经提交给通知中心
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
BOOL notificationStillPresent;
do {
notificationStillPresent = NO;
for (NSUserNotification *nox in [[NSUserNotificationCenter defaultUserNotificationCenter] deliveredNotifications]){
if ([nox.identifier isEqualToString:notification.identifier]) notificationStillPresent = YES;
}
if (notificationStillPresent) [NSThread sleepForTimeInterval:0.20f];
} while (notificationStillPresent);
dispatch_async(dispatch_get_main_queue(),^{
[self notificationHandlerForNotification:notification];
});
});
NSLog(@"收到通知");
}
//用户已经点击了通知
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification{
NSLog(@"用户点击");
NSDictionary *dict = notification.userInfo;
if (IsDictionaryNull(dict)) return;
if (notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
} else {
if ([notification.actionButtonTitle isEqualToString:@"关闭"]) { // 交易按钮
[[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];
}
}
if (notification.activationType == NSUserNotificationActivationTypeContentsClicked) { // 点击content
} else { // 点击按钮
if ([notification.actionButtonTitle isEqualToString:@"交易"]) { // 交易按钮
if ([notification respondsToSelector:@selector(_alternateActionIndex)]) {
NSNumber *alternateActionIndex = [(NSObject *)notification valueForKey:@"_alternateActionIndex"];
NSInteger index = [alternateActionIndex integerValue];
if (index > 2)
return;
if (index != 0 && index != 1)
return;
}
//returen YES;强制显示(即不管通知是否过多)
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification{
return YES;
}
- (void)sendStockWarningUserNotice{
NSUserNotification *localNotify = [[NSUserNotification alloc] init];
localNotify.title = @"";
localNotify.subtitle = @"";
localNotify.informativeText = @"";
if ([soundStr isEqualToString:@"1"]) {
localNotify.soundName = NSUserNotificationDefaultSoundName;
} else {
localNotify.soundName = nil;
}
[localNotify setValue:@YES forKey:@"_showsButtons"]; //需要显示按钮
//只有当用户设置为提示模式时,才会显示按钮.不设置的话,默认为yes
localNotify.hasActionButton = YES;
NSDictionary *dict = @{};
localNotify.userInfo = dict;
if (resp.remindType == 5) { /** 只显示打开关闭 */
localNotify.actionButtonTitle = @"打开";
localNotify.otherButtonTitle = @"关闭";
} else {
if (showColse) { // 只显示关闭按钮
localNotify.otherButtonTitle = @"关闭";
localNotify.hasActionButton = NO;
} else {//打开按钮显示二级目录
localNotify.actionButtonTitle = @"打开";
localNotify.otherButtonTitle = @"关闭";
[localNotify setValue:@[@"secondMenu", @"secondMenu1"] forKey:@"_alternateActionButtonTitles"];
[localNotify setValue:@YES forKey:@"_alwaysShowAlternateActionMenu"];
}
}
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setSecond:70];
localNotify.deliveryRepeatInterval = dateComponents;//重复通知时间间隔
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:localNotify];
//设置通知的代理
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:delegate];
}
网友评论