异步或在后台注册Apple推送通知

时间:2012-02-22 10:39:04

标签: iphone objective-c ipad apple-push-notifications

在我的应用程序中我使用推送通知,在我看来,注册推送的请求需要一段时间,这会阻止我的应用程序立即显示内容并且只显示黑屏直到设备令牌返回。

- (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];
}

希望有帮助......可以恢复也是问题吗?

亲切的问候阅读

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

将代码放在-(BOOL)application:didFinishLaunchingWithOptions:中,它应该可以正常工作。

答案 2 :(得分:1)

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

  

发送此消息时,设备会使用Apple Push Service启动注册过程。如果成功,则应用程序委托在应用程序中接收设备令牌:didRegisterForRemoteNotificationsWithDeviceToken:method;如果注册不成功,则通过应用程序通知代理:didFailToRegisterForRemoteNotificationsWithError:method。

你可能在委托的回调中耗费时间吗?