iOS - 当新数据发布到NSMutableArray时触发通知?

问题描述:

我已经在我的服务器上设置了推送通知,并且一切正常。 E.g.我可以随时向所有用户发送通知(因为所有设备令牌都存储在我的数据库中)。也就是说,我想在应用程序中镜像的服务器上的数据更新时向设备发送推送通知(,例如,当新消息发布到服务器时)?iOS - 当新数据发布到NSMutableArray时触发通知?

更具体地说:查看下面我用来在tableview中显示所有收到的消息的代码。我该如何做到这一点,以便登录的用户在self.messages更新时收到通知?

ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *PointsTableIdentifier = @"MyMessagesCell"; 

    MyMessagesCell *cell = (MyMessagesCell *)[tableView dequeueReusableCellWithIdentifier:PointsTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyMessagesCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

    } 


    NSDictionary *receivedSubjectLine = [self.messages objectAtIndex:indexPath.row]; 

    NSString *messageSubject = [receivedSubjectLine objectForKey:@"node_title"]; 


    [cell.subjectLine setText:messageSubject]; 



    NSDictionary *fromUser = [self.messages objectAtIndex:indexPath.row]; 

    NSString *userName = [fromUser objectForKey:@"name"]; 

    [cell.senderName setText:userName]; 



    NSDictionary *receivedBody = [self.messages objectAtIndex:indexPath.row]; 

    NSString *messageBody = [receivedBody objectForKey:@"body"]; 

    [cell.fullMessage setText:messageBody]; 


    NSDictionary *receivedTime = [self.messages objectAtIndex:indexPath.row]; 

    NSString *timeStamp = [receivedTime objectForKey:@"swaptime"]; 

     NSLog(@"The timestamp is %@", timeStamp); 

    [cell.swapTime setText:timeStamp]; 

    return cell; 

} 
+0

你只需要在你更新数组的地方编写你需要的代码。 – Paulw11

+0

当应用程序本身更新时,为什么会向应用程序发送通知?这完全没有任何意义。如果实际上您希望在应用更新时将推送发送到其他设备,或者在应用中镜像的服务器上的数据更新时向设备发送推送,那么这两种方法都有意义,但这不是什么你的问题问。 – Gruntcakes

+0

@SausageModulationMatrix对不起,这就是我的意思:我想在应用程序中镜像的服务器上的数据更新时,向设备发送推送通知。我该如何去做这件事? – BBL18

我在我的应用程序聊天不使用网络插座(我们还没有拥有它执行的时间),所以我们做的是刷新使用通知中心的UITableView。

在AppDelegate中我有这样的代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil]; 
} 

然后在我的聊天类我有这样的代码登记在viewDidLoad中通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"reloadTheTable" object:nil]; 

然后选择调用服务器并刷新显示的数据:

- (void)reloadTable:(NSNotification *)notification 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    [self checkJson]; //This function loads the data and when it finish calls another method call loadTable 
} 

- (void)loadTable { 
    [_tvTable reloadData]; //this simply reload your UITableView 
} 

我也使用这个技术来重新加载时,应用程序来到前景:

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTheTable" object:nil]; 
} 

我希望这可以帮助你。

+0

我不明白 - 当新数据发布时,上述操作将如何向我的用户设备发送推送通知? – BBL18

+0

@ BBL18据我的理解你的问题,你希望当服务器数据库更新时,你的设备将收到一个推,然后你需要更新设备上的数据,所以当你的服务器更新数据库时,你必须打电话给你的发送推送方法在你的服务器和我的代码,你会收到推送并发送通知到系统,这将更新您的表。 –