forked from simonhulu/ParentsCommunity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemNotificationObject.m
151 lines (142 loc) · 7.96 KB
/
SystemNotificationObject.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// SystemNotificationObject.m
// ParentsCommunity
//
// Created by qizhang on 14-3-18.
// Copyright (c) 2014年 张 诗杰. All rights reserved.
//
#import "SystemNotificationObject.h"
#import "AppDelegate.h"
@implementation SystemNotificationObject
+(BOOL)save:(SystemNotificationObject*)aMessage
{
AppDelegate *deletage = [[UIApplication sharedApplication]delegate] ;
NSError *error;
NSManagedObject *messages = [NSEntityDescription
insertNewObjectForEntityForName:@"SystemNotifications" inManagedObjectContext:deletage.managedObjectContext];
[messages setValue:aMessage.userId forKey:@"userId"];
[messages setValue:aMessage.content forKey:@"content"];
[messages setValue:[[NSNumber alloc]initWithInt:aMessage.notificationType]forKey:@"notificationType"];
[messages setValue:aMessage.notificationDate forKey:@"notificationDate"];
[messages setValue:aMessage.roomId forKey:@"roomId"];
[messages setValue:[[NSNumber alloc]initWithInt:aMessage.notificationStatus] forKey:@"notificationStatus"];
[messages setValue:aMessage.notificationFrom forKey:@"notificationFrom"];
[messages setValue:aMessage.notificationHead forKey:@"notificationHead"];
[messages setValue:aMessage.notificationId forKey:@"notificationId"];
if (![deletage.managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
return NO;
}else
{
//更新最新的消息时间
[[NSUserDefaults standardUserDefaults]setObject:aMessage.notificationDate forKey:kLastMessageDate];
//发送全局通知 有新邮件
[[NSNotificationCenter defaultCenter]postNotificationName:newNotificationCome object:nil];
return YES;
}
}
+(NSMutableArray*)fetchNotificationByOwnId:(NSString *)ownId
{
AppDelegate *delegate = [[UIApplication sharedApplication]delegate] ;
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSError *error;
// NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"(ownId == %@)",ownId];
// fetch.predicate = preTemplate;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"notificationDate" ascending:YES];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetch setEntity:[NSEntityDescription entityForName:@"SystemNotifications" inManagedObjectContext:delegate.managedObjectContext]];
NSArray *fetchObjects = [delegate.managedObjectContext executeFetchRequest:fetch error:&error];
NSMutableArray *notificationList=[[NSMutableArray alloc]init];
for (id obj in fetchObjects) {
SystemNotificationObject *notification = [[SystemNotificationObject alloc]init];
notification.notificationType = [[obj valueForKey:@"notificationType"]intValue];
notification.notificationDate = [obj valueForKey:@"notificationDate"];
notification.notificationStatus = [[obj valueForKey:@"notificationStatus"]intValue];
notification.content = [obj valueForKey:@"content"];
notification.roomId = [obj valueForKey:@"roomId"];
notification.userId = [obj valueForKey:@"userId"];
notification.notificationFrom = [obj valueForKey:@"notificationFrom"];
notification.notificationHead = [obj valueForKey:@"notificationHead"];
notification.notificationId = [obj valueForKey:@"notificationId"];
[notificationList addObject:notification];
}
return notificationList;
}
+(NSMutableArray*)fetchUnReadNotificationByOwnId:(NSString *)ownId
{
AppDelegate *delegate = [[UIApplication sharedApplication]delegate] ;
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSError *error;
// NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"(ownId == %@)",ownId];
// fetch.predicate = preTemplate;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"notificationDate" ascending:NO];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetch setEntity:[NSEntityDescription entityForName:@"SystemNotifications" inManagedObjectContext:delegate.managedObjectContext]];
NSArray *fetchObjects = [delegate.managedObjectContext executeFetchRequest:fetch error:&error];
NSMutableArray *notificationList=[[NSMutableArray alloc]init];
for (id obj in fetchObjects) {
if([[obj valueForKey:@"notificationStatus"]intValue] ==kWCSystmNotificationUnRead)
{
SystemNotificationObject *notification = [[SystemNotificationObject alloc]init];
notification.notificationType = [[obj valueForKey:@"notificationType"]intValue];
notification.notificationDate = [obj valueForKey:@"notificationDate"];
notification.notificationStatus = [[obj valueForKey:@"notificationStatus"]intValue];
notification.content = [obj valueForKey:@"content"];
notification.roomId = [obj valueForKey:@"roomId"];
notification.userId = [obj valueForKey:@"userId"];
notification.notificationFrom = [obj valueForKey:@"notificationFrom"];
notification.notificationHead = [obj valueForKey:@"notificationHead"];
notification.notificationId = [obj valueForKey:@"notificationId"];
[notificationList addObject:notification];
}
}
return notificationList;
}
+(SystemNotificationObject*)getLastSystemNotification:(NSString *)ownId
{
AppDelegate *delegate = [[UIApplication sharedApplication]delegate] ;
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSError *error;
// NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"(ownId == %@)",ownId];
// fetch.predicate = preTemplate;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"notificationDate" ascending:NO];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetch setEntity:[NSEntityDescription entityForName:@"SystemNotifications" inManagedObjectContext:delegate.managedObjectContext]];
NSArray *fetchObjects = [delegate.managedObjectContext executeFetchRequest:fetch error:&error];
if([fetchObjects count]>0)
{
SystemNotificationObject *notification = [[SystemNotificationObject alloc]init];
NSManagedObject *obj = fetchObjects[0];
notification.notificationType = [[obj valueForKey:@"notificationType"]intValue];
notification.notificationDate = [obj valueForKey:@"notificationDate"];
notification.notificationStatus = [[obj valueForKey:@"notificationStatus"]intValue];
notification.content = [obj valueForKey:@"content"];
notification.roomId = [obj valueForKey:@"roomId"];
notification.userId = [obj valueForKey:@"userId"];
notification.notificationFrom = [obj valueForKey:@"notificationFrom"];
notification.notificationHead = [obj valueForKey:@"notificationHead"];
return notification;
}
return nil;
}
+(BOOL)changeNotificationReadStatus:(NSInteger)status
{
AppDelegate *delegate = [[UIApplication sharedApplication]delegate] ;
NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
NSError *error;
// NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"(ownId == %@)",[WCUserObject getLoginUserId]];
// fetch.predicate = preTemplate;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"notificationDate" ascending:NO];
[fetch setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetch setEntity:[NSEntityDescription entityForName:@"SystemNotifications" inManagedObjectContext:delegate.managedObjectContext]];
NSArray *fetchObjects = [delegate.managedObjectContext executeFetchRequest:fetch error:&error];
NSMutableArray *notificationList=[[NSMutableArray alloc]init];
for (NSManagedObject *obj in fetchObjects) {
if([[obj valueForKey:@"notificationStatus"]intValue] ==kWCSystmNotificationUnRead)
{
[obj setValue:[NSNumber numberWithInt:status]forKeyPath:@"notificationStatus"];
}
}
return [delegate.managedObjectContext save:&error];
}
@end