配置推送通知 iOS

Configuring push notifications iOS

我使用 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;
}

在你的 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 收到通知后,post 向 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

尝试像 DilumN 在他的回答中所说的那样使用 NSNotificationCenter,当您收到通知时,然后用该方法重新加载 tableview。