作者rainsky0617 (rainsky)
看板MacDev
标题[问题] ios notification
时间Sun Dec 21 23:12:24 2014
小弟最近刚写了一只小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://webptt.com/cn.aspx?n=bbs/MacDev/M.1419174747.A.C52.html
1F:推 howdiun: 这叫写死在推播里,推播没点就没有url 12/22 09:46
2F:→ rainsky0617: 是的,但是点选推播後所显示的VIEW,并拿不到那个URL 12/22 15:54
3F:推 kidd0717: 建议你可以贴到github或其他可以贴code的地方 方便阅读 12/22 16:01
4F:→ rainsky0617: 谢谢楼上的回覆与建议,已解决罗~! 12/22 20:56
5F:→ rainsky0617: 我不应该一直用讯息中心传东西,将pushview 新增一个 12/22 20:57
6F:→ rainsky0617: @property (nonatomic) NSURL *url在利用pvc.url=xxx 12/22 20:59