如何获得APNS推送通知的背景iOS6的? [英] How to get APNS push notification in background in iOS6?

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

问题描述

我在我的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.

我的code,我试过如下:

My code which I tried is as follows:

- (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 词典的警报文本时,应用程序是在这些国家中的一个,和你的$ C显示后,通知以及用户轻拍打开应用$ C仅调用。

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.

如果用户没有通过点击通知开你的应用程序,你的code将永远不会被调用。此外,如果应用程序是主动或在后台运行的 didReceiveRemoteNotification 方法只调用。如果它没有运行,不同的方法被称为 - 应用程序: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.

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

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