[問題] ios notification

作者: rainsky0617 (rainsky)   2014-12-21 23:12:24
小弟最近剛寫了一隻小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];
}
作者: howdiun (Howdiun)   2014-12-22 09:46:00
這叫寫死在推播裡,推播沒點就沒有url
作者: rainsky0617 (rainsky)   2014-12-22 15:54:00
是的,但是點選推播後所顯示的VIEW,並拿不到那個URL
作者: kidd0717 (momoyo)   2014-12-22 16:01:00
建議你可以貼到github或其他可以貼code的地方 方便閱讀
作者: rainsky0617 (rainsky)   2014-12-22 20:56:00
謝謝樓上的回覆與建議,已解決囉~!我不應該一直用訊息中心傳東西,將pushview 新增一個@property (nonatomic) NSURL *url在利用pvc.url=xxx

Links booklink

Contact Us: admin [ a t ] ucptt.com