如何将收到的通知信息放入应用中的UITableView中?
How to put the information of the notification received into a UITableView in the app?
我正在尝试在我的应用程序上实施 Apple 推送通知服务。我关注了 Pushbots tutorial 但是否可以从通知中获取信息并将其放入 table?
我添加到项目中的唯一代码在 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];
}
每当收到通知时,didReceiveRemoteNotification 将使用负载(即用户信息)调用。您可以使用解析负载并将其显示到 table 视图
请将以下代码添加到您的项目中。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:[[userInfo objectForKey:@"aps"] objectForKey: @"id"] forKey:@"pushstring"];
[[NSUserDefaults standardUserDefaults]synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
NSLog(@"value of array is %@",userInfo);
//NSMutableArray *arr=[[NSMutableArray alloc]init];
// [arr addObject:[[userInfo valueForKey:@"aps"]valueForKey:@"category"]];
NSLog(@"value of array is %d",[[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue]);
// for (arr in userInfo) {
// NSLog(@"key: %@, value: %@", arr, [userInfo objectForKey:arr]);
// }
// NSLog(@"value of alert is %@",[arr objectAtIndex:2]);
// NSLog(@"value of category is %@",[[arr objectAtIndex:0]objectAtIndex:2]);
}
希望对您有所帮助。
是的,当在 didReceiveRemoteNotification 方法中收到通知时,您将从 userinfo 获取有效负载,以便您可以使用数据修改 table。
userInfo
是包含推送通知的字典。 aps
是推送通知正文的键。
将负载保存在 NSUserDefaults 中,稍后在 ViewController 中使用它来填充 tableView。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// you can get the required message as below
NSString *msg = [userInfo valueForKey:@"aps"];
NSLog(@"Push Notification:%@",msg);
[[NSUserDefaults standardUserDefaults]setObject:msg forKey:@"ReceivedNotifications"];
//This is how you save object in NSUserDefaults
}
现在在你的 ViewController.m
{
NSMutableArray *notificationArray= [NSMutableArray alloc]init];
notificationArray= [[NSUserDefaults standardUserDefaults]objectForKey:@"ReceivedNotifications"];
}
使用此 notificationArray
加载您的 tableView。
我正在尝试在我的应用程序上实施 Apple 推送通知服务。我关注了 Pushbots tutorial 但是否可以从通知中获取信息并将其放入 table?
我添加到项目中的唯一代码在 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];
}
每当收到通知时,didReceiveRemoteNotification 将使用负载(即用户信息)调用。您可以使用解析负载并将其显示到 table 视图
请将以下代码添加到您的项目中。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:[[userInfo objectForKey:@"aps"] objectForKey: @"id"] forKey:@"pushstring"];
[[NSUserDefaults standardUserDefaults]synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
NSLog(@"value of array is %@",userInfo);
//NSMutableArray *arr=[[NSMutableArray alloc]init];
// [arr addObject:[[userInfo valueForKey:@"aps"]valueForKey:@"category"]];
NSLog(@"value of array is %d",[[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue]);
// for (arr in userInfo) {
// NSLog(@"key: %@, value: %@", arr, [userInfo objectForKey:arr]);
// }
// NSLog(@"value of alert is %@",[arr objectAtIndex:2]);
// NSLog(@"value of category is %@",[[arr objectAtIndex:0]objectAtIndex:2]);
}
希望对您有所帮助。
是的,当在 didReceiveRemoteNotification 方法中收到通知时,您将从 userinfo 获取有效负载,以便您可以使用数据修改 table。
userInfo
是包含推送通知的字典。 aps
是推送通知正文的键。
将负载保存在 NSUserDefaults 中,稍后在 ViewController 中使用它来填充 tableView。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// you can get the required message as below
NSString *msg = [userInfo valueForKey:@"aps"];
NSLog(@"Push Notification:%@",msg);
[[NSUserDefaults standardUserDefaults]setObject:msg forKey:@"ReceivedNotifications"];
//This is how you save object in NSUserDefaults
}
现在在你的 ViewController.m
{
NSMutableArray *notificationArray= [NSMutableArray alloc]init];
notificationArray= [[NSUserDefaults standardUserDefaults]objectForKey:@"ReceivedNotifications"];
}
使用此 notificationArray
加载您的 tableView。