[英]Configuring push notifications iOS
I used Pushbots to configure push notifications for my app. 我使用Pushbots为我的应用配置了推送通知。 After I get the notification, I am able to put it into a UITableview. 收到通知后,可以将其放入UITableview中。 However the notification only appears after the user restarts the app. 但是,该通知仅在用户重新启动应用程序后出现。 Is there a way to immediately add the notification text after the user gets it, or when the user clicks the notification? 有没有一种方法可以在用户获取通知文本后或在用户单击通知时立即添加通知文本?
In my AppDelegate.m
: 在我的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);
}
In my ViewController.m
: 在我的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;
}
In your ViewController viewDidLoad method start listen to a NSNotification as below, 在您的ViewController viewDidLoad方法中,开始监听NSNotification,如下所示:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"TestNotification"
object:nil];
Add this receiveNotification
to your ViewController as well. 也将此receiveNotification
通知添加到您的ViewController。 Inside this if condition you can reload the TableView. 在此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!");
}
Don't forget to remove the notification when you dealloc the ViewController, 当您取消分配ViewController时,请不要忘记删除通知,
[[NSNotificationCenter defaultCenter] removeObserver:self];
Then from your AppDelegate once you receive a notification, post a Notification to TestNotification
name 然后,在收到通知后,从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
尝试使用与DilumN在他的回答中说的相同的NSNoticationCenter,当您收到通知时,然后以该方法重新加载tableview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.