我使用Pushbots为我的应用配置推送通知。收到通知后,我可以把它放到UITableview中。但是,通知仅在用户重新启动应用程序后显示。有没有办法在用户获取后立即添加通知文本,或者用户点击通知?
在我的AppDelegate.m
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[Pushbots sharedInstanceWithAppId:@"--myAppid--"];
[[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];
//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);
}
在我的ViewController.m
:
#import "ViewController.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *notifTableView;
@end
@implementation ViewController
{
NSMutableArray *notif;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.notifTableView.dataSource = self;
self.notifTableView.delegate = self;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *checkAlert = [[NSString alloc] init];
checkAlert = [defaults stringForKey:@"ReceivedNotifications"];
NSLog(@"Alert Message: %@", checkAlert);
notif = [NSMutableArray arrayWithObjects:checkAlert, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [notif count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [notif objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:2)
在ViewController viewDidLoad方法中,开始听取NSNotification,如下所示,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"TestNotification"
object:nil];
将此receiveNotification
添加到ViewController中。在这个if条件中你可以重新加载TableView。
- (void) receiveNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
请勿忘记在取消分配ViewController
时删除通知[[NSNotificationCenter defaultCenter] removeObserver:self];
然后,在收到通知后,从您的AppDelegate发布通知,将通知发布到TestNotification
名称
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:nil]; //You can set object as nil or send the object you want to get from the ViewController
答案 1 :(得分:-2)
尝试使用NSNoticationCenter,就像DilumN在他的回答中所说,当你收到通知然后用该方法重新加载tableview。