[问题] 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