如何在 iOS6 后台获取 APNS 推送通知? [英] How to get APNS push notification in background in iOS6?

查看:16
本文介绍了如何在 iOS6 后台获取 APNS 推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 iOS 应用程序中实现 APNS.我在前台收到苹果推送通知,但是当我的应用进入后台或非活动模式时,我的应用没有收到任何推送通知.

I am implementing APNS in my iOS application. I am getting apple push notifications in foreground, but when my app goes in background or inactive mode, my app does not receive any push notification.

我尝试的代码如下:

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

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        NSLog(@"active");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Active" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }else if (state == UIApplicationStateBackground){
        NSLog(@"background");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Background" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    }else{
        NSLog(@"Inactive");
         NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
         NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Inactive" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

请告诉我哪里出错或遗漏了什么.

Please tell me where I am going wrong or missing something.

推荐答案

当应用处于后台或非活动状态时,您不应以编程方式显示通知.当应用程序处于这些状态之一时,iOS 会自动显示 aps 字典的 alert 文本,并且只有在用户显示并点击通知后才会调用您的代码打开您的应用.

You are not supposed to display the notification programatically when the app is in the background or inactive. iOS displays the alert text of the aps dictionary automatically when the app is in one of these states, and your code is called only after the notification is displayed and tapped by the user to open your app.

如果用户没有通过点击通知打开您的应用,您的代码将永远不会被调用.此外,didReceiveRemoteNotification 方法仅在应用程序处于活动状态或在后台运行时调用.如果它没有运行,则调用不同的方法 - application:didFinishLaunchingWithOptions.

If the user doesn't open your app by tapping the notification, your code will never be called. In addition, the didReceiveRemoteNotification method is only called if the app is active or running in the background. If it is not running, a different method is called - application:didFinishLaunchingWithOptions.

这篇关于如何在 iOS6 后台获取 APNS 推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆