哪个推送通知导致应用程序从后台恢复

时间:2014-03-07 12:17:15

标签: ios objective-c apple-push-notifications

我的应用会收到APNS Push Notifications,当用户收到多个NSNotification时,他应该能够根据点击的NSNotification在特定视图中打开该应用。

所以在方法

- (void)application:(UIApplication *)application 
        didReceiveRemoteNotification:(NSDictionary *)userInfo 
        fetchCompletionHandler:
           (void (^)(UIBackgroundFetchResult))completionHandler

我添加了此代码以保存所有通知

if (self.notifications == nil) {
        self.notifications = [[NSMutableArray alloc] init];
    }

    [notifications addObject:userInfo];

每次应用再次变为活动状态时,都会执行此操作

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) 
    // while the application was inactive.
    // If the application was previously in the background, 
    // optionally refresh the user interface.
    [notifications removeAllObjects];
    application.applicationIconBadgeNumber = 0;
}

在删除所有对象并将徽章设置为零之前,我想处理哪个NSNotification让我的应用从后台打开。一旦我有推动NSNotification,我想将所有数据传递给特定视图。

3 个答案:

答案 0 :(得分:0)

如果您不使用新的iOS7后台提取通知。

在从通知数组中删除所有对象之前- (void)applicationDidBecomeActive:(UIApplication *)application,检查通知

NSDictionary * myNotification = [notifications lastObject]; 
if (myNotification)
{
 // is last notification 
}

是否有效,因为应用只收到用户点击它的通知

答案 1 :(得分:0)

使用您的推送通知,数据发送,并在您的应用收到它时使用它。

在此示例中,我正在使用图像文件和警报,并将其发送到为此通知注册的所有视图。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

    //Posting the notificaiton to the use, if its valid:

    NSDictionary *returnDic = [userInfo objectForKey:@"aps"];


    if (![returnDic objectForKey:@"alert"]) {
        return;
    }
    NSString *alert = [NSString stringWithFormat:@"%@",[returnDic objectForKey:@"alert"]];

    if (![returnDic objectForKey:@"MagnetID"]) {
        return;
    }

    NSString *magnetImage = [returnDic objectForKey:@"MagnetImage"];

    NSDictionary *dictionaryToSend = [NSDictionary dictionaryWithObjectsAndKeys:magnetImage,MAGNET_IMAGE,alert,MAGNET_ERROR_MESSEGE, nil];


    //Posting to the rest of the views, the messege:

    [[NSNotificationCenter defaultCenter] postNotificationName:USER_MESSEGE_RECEAVED object:nil userInfo:dictionaryToSend];

    NSLog(@"Notifications - userInfo=%@",userInfo);


}

您可以做的是保存您需要的数据,UserDefults或您喜欢的任何内容,并在“applicationDidBecomeActive”方法中,使用该数据显示正确的视图。

希望这有帮助

答案 2 :(得分:0)

根据您对UILocalNotification用法

的评论

UILocalNotification具有userInfo属性。当您从推送通知创建本地通知时,请将相应的信息设置到此属性中,然后当应用代理收到application:didReceiveLocalNotification:时,您可以使用该信息更新您的用户界面。