当我收到通知时,我应该把用于在 UITableView 中添加新行的代码放在哪里?
Where do I put the code to add a new row in UITableView when I receive a notification?
我正在尝试做的是将从收到的通知中获得的信息放入 UITableView。我已经设法做到了,但是它只更新了第一行。意思是,如果我向我的应用发送 2 个连续的通知,我只会看到第二个通知信息,因为它会覆盖第一个。
如何添加另一行来包含通知数据?
我使用 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);
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
[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;
}
在方法“- (void) receiveNotification:(NSNotification *) notification”中
更改下面的行
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
至
[notif addObject:checkAlert];
我正在尝试做的是将从收到的通知中获得的信息放入 UITableView。我已经设法做到了,但是它只更新了第一行。意思是,如果我向我的应用发送 2 个连续的通知,我只会看到第二个通知信息,因为它会覆盖第一个。
如何添加另一行来包含通知数据? 我使用 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);
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
[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;
}
在方法“- (void) receiveNotification:(NSNotification *) notification”中
更改下面的行
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
至
[notif addObject:checkAlert];