Firebase * ref = nil;
NSInteger iid = [[API sharedInstance] userid];
NSString * path = [NSString stringWithFormat: @"http://example.firebaseIO.com/user/%d/conversations", iid];
ref = [[Firebase alloc] initWithUrl:path];
if(ref) {
NSString * path = [NSString stringWithFormat: @"http://example.firebaseIO.com/conversations"];
Firebase * conv = [[Firebase alloc] initWithUrl: path];
[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
// name of conversation
NSString * name = snapshot.name;
Firebase * ref1 = [conv childByAppendingPath: name];
[ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
{
FDataSnapshot * chatsnapshot = [snapshot childSnapshotForPath: @"chats"];
NSInteger numChatMessages = chatsnapshot.childrenCount;
numberOfTotalChatMessages += numChatMessages;
NSMutableDictionary *m = [snapshot.value mutableCopy];
[m setValue: snapshot.name forKey: @"ref_name"];
NSInteger current_user = [[API sharedInstance] userid];
NSString * userpath = [NSString stringWithFormat: @"users/%d", current_user];
FDataSnapshot * usersnapshot = [snapshot childSnapshotForPath: userpath];
if(usersnapshot.value != [NSNull null] && ![usersnapshot.value isKindOfClass: [NSString class]])
{
NSDictionary * userdict = usersnapshot.value;
NSInteger numUserMessagesRead = [userdict[@"numOfMessages"] intValue];
numberOfMessagesRead += numUserMessagesRead;
if(numberOfTotalChatMessages > numberOfMessagesRead) {
[m setValue: @"true" forKey: @"bubble"];
}
}
[self.chats addObject: m];
NSNumber * index = [NSNumber numberWithInt: self.chats.count - 1];
[read setValue: index forKey: snapshot.name];
PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
[rightPanel.tableView reloadData];
self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];
}
}];
}];
[ref observeEventType:FEventTypeChildChanged withBlock:^(FDataSnapshot *snapshot) {
NSString * name = snapshot.name;
Firebase * ref1 = [conv childByAppendingPath: name];
[ref1 observeSingleEventOfType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot)
{
if(snapshot.value != [NSNull null] && ![snapshot.value isKindOfClass: [NSString class]])
{
numberOfTotalChatMessages += 1;
NSMutableDictionary *m = [snapshot.value mutableCopy];
[m setValue: snapshot.name forKey: @"ref_name"];
[m setValue: @"true" forKey: @"bubble"];
[self.chats addObject: m];
if([read objectForKey: snapshot.name])
{
NSInteger index = [[read objectForKey: snapshot.name] intValue];
[self.chats removeObjectAtIndex: index];
NSNumber * index1 = [NSNumber numberWithInt: self.chats.count - 1];
[read setValue: index1 forKey: snapshot.name];
}
self.numChats = numberOfTotalChatMessages - numberOfMessagesRead;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: self.numChats];
PLRightMenuViewController * rightPanel = (PLRightMenuViewController *) self.viewController.rightPanel;
[rightPanel.tableView reloadData];
}
}];
}];
}
我有上面的代码,它基本上使用 firebase 检查任何新的聊天对话并更改应用程序徽章编号。如何在应用程序的后台运行代码,以便更改应用程序徽章编号,无论是否有人当前正在使用该应用程序?
基本上,我怎样才能在后台运行上面的代码?我应该在 Appdelegate 中更改什么?