即使应用程序在后台 运行,如何使用通知信息更新 TableView?

How to update a TableView with the notification information even when the app is running in the background?

我想做的是将从收到的通知中获得的信息放入 UITableView。我已经设法做到了,但是只有在收到通知时用户在应用程序中时才会添加信息

即使用户不在应用程序中,如何让 table 重新加载通知信息? 我使用 Pushbots 配置我的推送通知。

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [Pushbots sharedInstanceWithAppId:@"--My App ID--"];
    [[Pushbots sharedInstance] receivedPush:launchOptions];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // This method will be called everytime you open the app
    // Register the deviceToken on Pushbots
    [[Pushbots sharedInstance] registerOnPushbots:deviceToken];
}
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Notification Registration Error %@", [error userInfo]);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //Handle notification when the user click it while app is running in background or foreground.
    [[Pushbots sharedInstance] receivedPush:userInfo];
    // you can get the required message as below
    NSLog(@"UserInfo: %@", userInfo);
    NSString *msg = [userInfo valueForKey:@"aps"];
    NSString *alertMsg = [msg valueForKey:@"alert"];
    NSLog(@"Push Notification:%@",alertMsg);

    [[NSUserDefaults standardUserDefaults]setObject:alertMsg forKey:@"ReceivedNotifications"];
    NSLog(@"Alert: %@", alertMsg);

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"TestNotification"
     object:nil]; //You can set object as nil or send the object you want to get from the ViewController
}

ViewController.m:

@implementation ViewController
{
    NSMutableArray *notif;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.notifTableView.dataSource = self;
    self.notifTableView.delegate = self;

    NSUserDefaults *prevNotifTable = [NSUserDefaults standardUserDefaults];
    NSMutableArray *prevNotif = [[prevNotifTable objectForKey:@"notifTableInfo"] mutableCopy];
    notif = prevNotif;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"TestNotification" object:nil];
}

- (void) receiveNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *checkAlert = [[NSString alloc] init];
    checkAlert = [defaults stringForKey:@"ReceivedNotifications"];
    NSLog(@"Alert Message: %@", checkAlert);
    if (notif == nil) {
        notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
    }
    else {
        [notif addObject:checkAlert];
    }        
    [self.notifTableView reloadData];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [notif count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text = [notif objectAtIndex:indexPath.row];

    return cell;
}

//For deleting a row
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            //Remove from NSMutableArray
            [notif removeObjectAtIndex:indexPath.row];

            //Remove from our table view
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
        //[notif removeObjectAtIndex:indexPath.row];
        //[tableView reloadData];
    }

//For inserting a new row into the table
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.notifTableView setEditing:editing animated:animated];

    //fill paths of insertion rows here
    if (editing) {
        [self.notifTableView insertRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
    }
    else {
        [self.notifTableView deleteRowsAtIndexPaths:notif withRowAnimation:UITableViewRowAnimationBottom];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

添加到 Avi 的评论中,

而不是使用

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

使用

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

这将启用后台通知处理。如果保留这两种方法,则后者优先于前者。所以根本不会调用。