2012-02-22 39 views
3

在我的应用程序我使用推送通知,在我看来,注册推送请求需要一段时间,这阻止我的应用程序立即显示内容,只显示一个黑色的屏幕,直到设备令牌回来。注册苹果推送通知异步或在后台

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    .... 

    NSLog(@"Registering for push notifications...");  
    [[UIApplication sharedApplication] 
    registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeAlert | 
     UIRemoteNotificationTypeBadge | 
     UIRemoteNotificationTypeSound)]; 

    .... 

    return self; 
} 

我怎样才能做到这一点asynchrounus或在后台..因为它,如果屏幕保持黑色混淆用户...

问候

更新:根据要求我的代码

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

    self.webViewCon = [[[WebViewController alloc] initWithNibName:@"WebView_iPhone" bundle:[NSBundle mainBundle]] autorelease]; 

    application.applicationIconBadgeNumber = 0; 
    [window addSubview:[webViewCon view]]; 
    [self.window makeKeyAndVisible]; 

    // register for push 
    NSLog(@"Registering for push notifications...");  
    [[UIApplication sharedApplication] 
    registerForRemoteNotificationTypes: 
    (UIRemoteNotificationTypeAlert | 
     UIRemoteNotificationTypeBadge | 
     UIRemoteNotificationTypeSound)]; 

    return YES; 
} 

然后在didRegisterForRemoteNotificationsWithDeviceToken

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken]; 
    NSLog(@"%@",str); 

    // get token & udidi 
    NSString* newToken = [deviceToken description]; 
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; 
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSString *udid = [UIDevice currentDevice].uniqueIdentifier; 

    // send request to server to save token 
    NSString *urlString = [NSString stringWithFormat:@"https://my.server.com/service/token?token=%@&udid=%@",newToken, udid]; 

    NSURL *url = [[NSURL alloc] initWithString:urlString]; 

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
    [url release]; 

    NSURLResponse *response; 
    [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]; 
} 

,并在我的WebViewController.m

- (void)viewDidLoad 
{ 
    // create and send POST request 
    NSURL *url = [NSURL URLWithString:myUrlAddress]; 
    NSString *udid = [UIDevice currentDevice].uniqueIdentifier; 
    NSString *requestString = [NSString stringWithFormat:@"udid=%@", udid]; 
    NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]]; 
    NSMutableURLRequest *requestObj = [[NSMutableURLRequest alloc] initWithURL:url]; 
    [requestObj setHTTPMethod:@"POST"]; 
    [requestObj setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; 
    [requestObj setHTTPBody: requestData]; 
    NSURLResponse *response; 
    NSError *err; 
    [NSURLConnection sendSynchronousRequest: requestObj returningResponse:&response error:&err]; 

    // set delegate 
    webView.delegate = self; 

    // set backgroundcolor 
    [webView setOpaque:NO]; 
    [webView setBackgroundColor:RGB(154, 148, 131)]; 

    // check internet and load 
    NetworkStatus currentStatus = [[Reachability reachabilityForInternetConnection] 
           currentReachabilityStatus]; 
    if(currentStatus != NotReachable) { 
     //Load the request in the UIWebView. 
     [webView loadRequest:requestObj]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] 
          initWithTitle:@"No Connection" 
          message:@"Check your Wi-Fi connection and restart the App." 
          delegate:self 
          cancelButtonTitle:nil 
          otherButtonTitles:@"Ok", nil]; 
     [alert show]; 
     [alert release]; 
    } 
    // prevent scrolling up & down 
    [[[webView subviews] lastObject] setScrollEnabled:NO]; 

    [super viewDidLoad]; 
} 

希望帮助...也可以Reachabilit是什么问题?

亲切的问候阅读

+0

你真的需要发送同步请求命中服务器? – iosfanboy9 2012-02-23 06:18:32

+0

@ krusty43可能不是。我应该尝试异步NSURLConnection?在我调用'initWithNibName'之后的 – spankmaster79 2012-02-23 14:36:09

回答

1

developer.apple.com,registerForRemoteNotificationTypes是异步的:

When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken: method; if registration doesn’t succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError: method.

可能是你做的不便耗时在委托的回调?

+0

它究竟在哪里声明这是异步的?通过提到应用程序委托被调用?嗯..实际上我在我的UIWebView中加载一个站点,但是webview已经有一个backgroundcolor ...所以这应该是可见的 – spankmaster79 2012-02-22 10:57:13

+1

该设备_initiates_注册过程与苹果推送服务。如果将NSLog输出放在registerForRemoteNotificationTypes后面并放入应用程序委托中的回调中,则会看到第一个NSLog比didRegisterForRemoteNotificationsWithDeviceToken/didFailToRegisterForRemoteNotificationsWithError的输出早出现。 – 2012-02-22 11:05:25

1

将您的代码-(BOOL)application:didFinishLaunchingWithOptions:,它应该工作的罚款。

+0

? – spankmaster79 2012-02-22 10:55:32

2

注册在AppDelegate中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions通知和

尝试实施这些方法,

// one of these will be called after calling -registerForRemoteNotifications 
- (void)application:(UIApplication *)application 
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
- (void)application:(UIApplication *)application 
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
- (void)application:(UIApplication *)application didReceiveRemoteNotification: 
(NSDictionary *)userInfo 
+0

thx,已经实现了方法 – spankmaster79 2012-02-22 11:07:28

+0

你在'application:didFinishLaunchingWithOptions:'中注册了APNS吗? 在scrren变黑之前调用了'didFailToRegisterForRemoteNotificationsWithError'?你使用断点测试过吗? – iosfanboy9 2012-02-22 11:21:43

+0

我在'initWithNibName'注册过,但是现在在'didFinishLaunchingWithOptions'中做了,并且'didFailToRegisterForRemoteNotificationsWithError'没有被调用。我确实得到了设备令牌,但在该请求之前屏幕保持黑屏 – spankmaster79 2012-02-22 13:44:07