Ios “错误”';NSInvalidArgumentException';,原因:'-[u NSCFDictionary objectAtIndex:]:发送到实例0x134d0a3c0'的无法识别的选择器&引用;
我正在我的应用程序上实施苹果推送通知服务。收到通知后,我想获取信息并将其添加到uitableview中。我是这样做的: 在Ios “错误”';NSInvalidArgumentException';,原因:'-[u NSCFDictionary objectAtIndex:]:发送到实例0x134d0a3c0'的无法识别的选择器&引用;,ios,objective-c,uitableview,apple-push-notifications,Ios,Objective C,Uitableview,Apple Push Notifications,我正在我的应用程序上实施苹果推送通知服务。收到通知后,我想获取信息并将其添加到uitableview中。我是这样做的: 在AppDelegate.m文件中: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [Pushbots sharedInstanceWithAppId:@"--my app id--"];
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];
NSString *msg = [userInfo valueForKey:@"aps"];
NSLog(@"Push Notification:%@",msg);
[[NSUserDefaults standardUserDefaults]setObject:msg forKey:@"ReceivedNotifications"];
}
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
在myViewController.m
中:
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *notifTableView;
@end
@implementation ViewController
{
NSMutableArray *notif;
}
- (void)viewDidLoad {
[super viewDidLoad];
notif = [[NSUserDefaults standardUserDefaults] objectForKey:@"ReceivedNotifications"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [notif count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [notif objectAtIndex:indexPath.row];
return cell;
}
这是我的main.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];
NSString *msg = [userInfo valueForKey:@"aps"];
NSLog(@"Push Notification:%@",msg);
[[NSUserDefaults standardUserDefaults]setObject:msg forKey:@"ReceivedNotifications"];
}
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#导入
#导入“AppDelegate.h”
int main(int argc,char*argv[]){
@自动释放池{
返回UIApplicationMain(argc、argv、nil、NSStringFromClass([AppDelegate类]);
}
}
怎么了?=>在调用objectAtIndex时,请确保您的手上确实有一个数组:错误消息清楚地表明,
objectAtIndex
的接收者是一个NSDictionary
对象
它是从推送通知的aps
对象创建的,推送通知实际上始终是一个字典,并使用键ReceivedNotifications
以用户默认值保存
您必须解析字典以提取要显示的信息
PS:不要使用
valueForKey
——这是一种具有特殊行为的KVC方法——从字典中获取对象。指定的方法是objectForKey
如何将其更改为数组对象?这取决于您将要完成的任务。标准的aps
字典包含键alert
和badge
alert
本身是一个包含多个键的字典,badge
是一个整数。枚举字典并获取所需的值和键。把它放到一个数组中。你有一个示例代码还是什么?我正在尝试获取手机收到的通知文本,并将其放入表格中。阅读以查看有效负载的结构。然后它只是一个订阅链userInfo[“aps”][“alert”][“title”]或[“body”]
。当然,您必须将中间部分结果转换为有意义的内容,以满足编译器的要求。