配置推送通知iOS

配置推送通知iOS,ios,objective-c,uitableview,notifications,apple-push-notifications,Ios,Objective C,Uitableview,Notifications,Apple Push Notifications,我曾经为我的应用程序配置推送通知。收到通知后,我可以将其放入UITableview。但是,该通知仅在用户重新启动应用程序后出现。是否有一种方法可以在用户收到通知文本后,或在用户单击通知时立即添加通知文本 在myAppDelegate.m中: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [Pushbots shar

我曾经为我的应用程序配置推送通知。收到通知后,我可以将其放入UITableview。但是,该通知仅在用户重新启动应用程序后出现。是否有一种方法可以在用户收到通知文本后,或在用户单击通知时立即添加通知文本

在my
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);
}
#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;
}
在my
ViewController.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);
}
#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;
}
#导入“ViewController.h”
@界面视图控制器()
@属性(弱、非原子)IBUITableView*notifTableView;
@结束
@实现视图控制器
{
NSMutableArray*notif;
}
-(无效)viewDidLoad{
[超级视图下载];
self.notifTableView.dataSource=self;
self.notifTableView.delegate=self;
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
NSString*checkAlert=[[NSString alloc]init];
checkAlert=[默认stringForKey:@“ReceivedNotifications”];
NSLog(@“警报消息:%@”,检查警报);
notif=[NSMUTABLEARRY arrayWithObjects:checkAlert,nil];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[notif count];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cell”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
cell.textlab.text=[notif objectAtIndex:indexath.row];
返回单元;
}

在您的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
name

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

尝试使用与Dillumn在回答中所说的相同的NSNotification Center,当您收到通知时,然后使用该方法重新加载tableview。

不要忘记删除DealLocation中的观察者此回答的意义是什么?它对您引用的答案没有任何补充。你应该删除这个答案,然后投票选出你喜欢的答案。