看板 MacDev 關於我們 聯絡資訊
小弟最近剛寫了一隻小APP,關於IOS NOTIFICATION,在APP有開啟時 或在背景執行時,收到推播通知,點擊後會進入特定VIEWCONTROLLER(include webview、 navigation bar 、 image view),以上功能都能正常運作,但是當APP沒開啟時,點擊後 雖然也會轉定特定的VIEWCONTROLLER,但是WEBVIEW區塊卻白茫茫一片,navigation bar 、 image view都有正常顯示,此一WEBVIEW的網址,先寫死在WEBVIEW中(方便測試) 請各位高手前輩幫幫忙給點建議囉~謝謝各位前輩先進們~!!OTZ.... CODE 如下: pushview 為點擊推播訊息後所要顯示的VIEWCONTROLLER 網頁網址目前先寫死在pushview.m裏 AppDelegate.m -(void)postNotificationToPresentPushMessagesVC { [[NSNotificationCenter defaultCenter] postNotificationName:@"HAS_PUSH_NOTIFICATION" object:nil]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //註冊推播 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; } // app 如果在沒啟動的狀態下(前景/背景都無),點"推播通知"後,會將推播資料以 launchOptions 傳入。 if (launchOptions) { NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (userInfo) { [[NSNotificationCenter defaultCenter] postNotificationName:@"request_aps" object:nil userInfo:userInfo]; [self performSelector:@selector(postNotificationToPresentPushMessagesVC) withObject:nil afterDelay:1]; } } return YES; } ViewController.m -(void)LoadRequestFromAppDel:(NSNotification*)aNotif { //取得推播訊息中的URL url=[[[aNotif userInfo] objectForKey:@"aps"] objectForKey:@"url"]; //轉到pushview pushview *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"pushview"]; [self presentViewController:vc animated:YES completion:nil]; //將URL放入訊息中心 [[NSNotificationCenter defaultCenter] postNotificationName:@"request_url" object:url]; } -(void)presentMyViewOnPushNotification { NSString *url=@"https://www.google.com"; [[NSNotificationCenter defaultCenter] postNotificationName:@"request_url" object:url]; pushview *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"pushview"]; [self presentViewController:pvc animated:YES completion:nil]; } -(void) viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //調用LoadRequestFromAppDel取出 request_aps object [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoadRequestFromAppDel:)name:@"request_aps" object:Nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentMyViewOnPushNotification) name:@"HAS_PUSH_NOTIFICATION" object:nil]; } pushview. h #import <UIKit/UIKit.h> @interface pushview : UIViewController @property (weak, nonatomic) IBOutlet UIWebView *pushweb; @end pushview.m - (void) LoadUrl:(NSNotification*) notification { NSString *geturl = [notification object];//通過這個獲取到傳遞的對象 geturl=@"https://tw.yahoo.com/"; NSURL *url = [NSURL URLWithString:geturl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; //pushweb 為UIwebview名稱 [self.pushweb loadRequest:request]; } - (void)viewDidLoad { [super viewDidLoad]; //調用LoadRequestFromAppDel取出 request_device_token object [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoadUrl:) name:@"request_url"object:nil]; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.24.178.9 ※ 文章網址: http://www.ptt.cc/bbs/MacDev/M.1419174747.A.C52.html
howdiun: 這叫寫死在推播裡,推播沒點就沒有url 12/22 09:46
rainsky0617: 是的,但是點選推播後所顯示的VIEW,並拿不到那個URL 12/22 15:54
kidd0717: 建議你可以貼到github或其他可以貼code的地方 方便閱讀 12/22 16:01
rainsky0617: 謝謝樓上的回覆與建議,已解決囉~! 12/22 20:56
rainsky0617: 我不應該一直用訊息中心傳東西,將pushview 新增一個 12/22 20:57
rainsky0617: @property (nonatomic) NSURL *url在利用pvc.url=xxx 12/22 20:59