从推送通知获取对象并在模态视图控制器中显示

时间:2014-10-16 18:26:03

标签: ios objective-c

在我的应用程序中创建任务时,它会向我的设备发送推送通知,我需要在表格视图中显示包含所有数据的任务,我实际上是在通知中发送数据:

{     " aps":{         "警告":"测试",         "声音":"默认"     },     "任务":{         "负责任":" 39",         " userId":" 49",         " taskDescription":" test",         " topicId":" 2",         " companyId":" 1",         " dueDate":" 05/10 / 2014"     } }

但问题是我不知道如何获取所有任务信息并将其放入对象中,任何人都可以帮助我吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

首先,您需要注册推送通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{

    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
           // if ios is 8 then this will run
           [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
           [application registerForRemoteNotifications];
    }
    else
    {
          // if ios is 7.x or less this will run
          [application registerForRemoteNotificationTypes:
                     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }
return YES;
}

值得检查应用程序是否已成功注册

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken

如果出现错误,它会来到这里,你可以写出它没有注册的原因

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}

然后您可以从以下代理

获取字典
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

从此处您可以使用以下代码获取密钥

NSDictionary *aps_dictionary = [userInfo objectForKey:@"aps"];
NSDictionary *task_dictionary =  [userInfo objectForKey:@"task"];
NSString *responsible = [task_dictionary objectForKey:@"responsible"];