Jay
Jay

Reputation: 49

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

What I am attempting to do, is to place the info I get from the notification received into a UITableView. I have managed to do that, however the information is only added if the user is in the app when the notification is received

How do I get the table to reload with the notification info even if the user is not in the app? I used Pushbots to configure my push notifications.

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;
}

Upvotes: 0

Views: 1363

Answers (1)

Varun
Varun

Reputation: 759

Adding to Avi's comment,

instead of using

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

use

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

which will enable background notification handling. If you keep both the methods, the latter takes precedence over the former. So it won't be called at all.

Upvotes: 1

Related Questions