gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:06:44 24 4
gpt4 key购买 nike

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

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

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

....

return self;
}

我怎样才能异步或在后台执行此操作...因为如果屏幕保持黑色会使用户感到困惑...

问候

更新:根据我的代码要求

- (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 也是问题所在?

亲切的阅读问候

最佳答案

在 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

关于iphone - 异步或在后台注册 Apple 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9393213/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com