如何使用swift打开新的WebView推送通知
我正在开发IOS混合应用程序,其中我已经开发IOS推送通知,并且在点击通知后,链接应该在webView中打开,但这不是哈哈,所以请帮我处理这种情况如何使用swift打开新的WebView推送通知
我做了一个发现,找不到“NotificationIdentifier”在文件中两次。
然后我做了你的代码的hexdump都:
hexdump -C file
00000000 66 75 6e 63 20 61 70 70 6c 69 63 61 74 69 6f 6e |func application|
00000010 28 61 70 70 6c 69 63 61 74 69 6f 6e 3a 20 55 49 |(application: UI|
00000020 41 70 70 6c 69 63 61 74 69 6f 6e 2c 20 64 69 64 |Application, did|
00000030 52 65 63 65 69 76 65 52 65 6d 6f 74 65 4e 6f 74 |ReceiveRemoteNot|
00000040 69 66 69 63 61 74 69 6f 6e 20 75 73 65 72 49 6e |ification userIn|
00000050 66 6f 3a 20 5b 4e 53 4f 62 6a 65 63 74 20 3a 20 |fo: [NSObject : |
00000060 41 6e 79 4f 62 6a 65 63 74 5d 29 20 7b 20 0a 20 |AnyObject]) { . |
00000070 20 4e 53 4e 6f 74 69 66 69 63 61 74 69 6f 6e 43 | NSNotificationC|
00000080 65 6e 74 65 72 2e 64 65 66 61 75 6c 74 43 65 6e |enter.defaultCen|
00000090 74 65 72 28 29 2e 70 6f 73 74 4e 6f 74 69 66 69 |ter().postNotifi|
000000a0 63 61 74 69 6f 6e 4e 61 6d 65 28 22 4e 6f 74 69 |cationName("Noti|
000000b0 66 69 63 61 74 69 6f 6e 49 64 65 6e 74 69 66 69 |ficationIdentifi|
000000c0 65 e2 80 8c e2 80 8b 72 22 2c 20 6f 62 6a 65 63 |e......r", objec|
000000d0 74 3a 20 6e 69 6c 29 0a 7d 20 0a 0a 6f 76 65 72 |t: nil).} ..over|
000000e0 72 69 64 65 20 66 75 6e 63 20 76 69 65 77 44 69 |ride func viewDi|
000000f0 64 4c 6f 61 64 28 29 20 7b 20 0a 20 20 20 20 73 |dLoad() { . s|
00000100 75 70 65 72 2e 76 69 65 77 44 69 64 4c 6f 61 64 |uper.viewDidLoad|
00000110 28 29 20 0a 20 20 20 20 4e 53 4e 6f 74 69 66 69 |() . NSNotifi|
00000120 63 61 74 69 6f 6e 43 65 6e 74 65 72 2e 64 65 66 |cationCenter.def|
00000130 61 75 6c 74 43 65 6e 74 65 72 28 29 2e 61 64 64 |aultCenter().add|
00000140 4f 62 73 65 72 76 65 72 28 73 65 6c 66 2c 20 73 |Observer(self, s|
00000150 65 6c 65 63 74 6f 72 3a 20 22 6d 65 74 68 6f 64 |elector: "method|
00000160 4f 46 52 65 63 65 69 76 65 64 4e 6f 74 69 63 61 |OFReceivedNotica|
00000170 74 69 6f 6e 3a 22 2c 20 6e 61 6d 65 3a 22 4e 6f |tion:", name:"No|
00000180 74 69 66 69 63 61 74 69 6f 6e 49 64 65 6e 74 69 |tificationIdenti|
00000190 66 69 65 72 22 2c 20 6f 62 6a 65 63 74 3a 20 6e |fier", object: n|
000001a0 69 6c 29 20 0a 7d 0a 0a 66 75 6e 63 20 6d 65 74 |il) .}..func met|
000001b0 68 6f 64 4f 46 52 65 63 65 69 76 65 64 4e 6f 74 |hodOFReceivedNot|
000001c0 69 63 61 74 69 6f 6e 28 6e 6f 74 69 66 69 63 61 |ication(notifica|
000001d0 74 69 6f 6e 3a 20 4e 53 4e 6f 74 69 66 69 63 61 |tion: NSNotifica|
000001e0 74 69 6f 6e 29 20 7b 20 0a 20 20 76 61 72 20 77 |tion) { . var w|
000001f0 65 62 56 69 65 77 20 3d 20 57 4b 57 65 62 56 69 |ebView = WKWebVi|
00000200 65 77 28 29 20 0a 20 20 76 61 72 20 75 72 6c 20 |ew() . var url |
00000210 3a 20 4e 53 55 52 4c 20 3d 20 4e 53 55 52 4c 28 |: NSURL = NSURL(|
00000220 73 74 72 69 6e 67 3a 20 e2 80 9c 6d 2e 78 79 7a |string: ...m.xyz|
00000230 2e 63 6f 6d e2 80 9d 29 20 0a 20 20 76 61 72 20 |.com...) . var |
00000240 72 65 71 75 65 73 74 20 3d 20 4e 53 55 52 4c 52 |request = NSURLR|
00000250 65 71 75 65 73 74 28 55 52 4c 3a 75 72 6c 29 20 |equest(URL:url) |
00000260 0a 20 20 77 65 62 56 69 65 77 2e 6c 6f 61 64 52 |. webView.loadR|
00000270 65 71 75 65 73 74 28 72 65 71 75 65 73 74 29 20 |equest(request) |
00000280 0a 20 20 73 65 6c 66 2e 76 69 65 77 2e 61 64 64 |. self.view.add|
00000290 53 75 62 76 69 65 77 28 77 65 62 56 69 65 77 29 |Subview(webView)|
000002a0 20 0a 7d 20 | .} |
000002a4
你的第一个字符串包含在最后的“e”一个奇怪的字符。 我认为你的处理程序可能不会被调用,因为通知标识符是不同的。你可以通过在methodOFReceivedNotication和application:didReceiveRemoteNotification中加入一个日志来验证:?
- >解决方法:手工写“NotificationIdentifier”
而且应用:didReceiveRemoteNotification:当应用程序已经启动,才会调用,因为你在viewDidLoad中注册您的ViewController这是罚款。
但是,如果您不知道,当通知打开应用程序(不会将其置于前景但打开它)时,应用程序didReceiveRemoteNotification:不会被调用,并且通知将在launchOptions字典中提供。 我邀请您到look at this blog article about notifications for more details。
- >解决方案:确保在发送通知时检查应用程序中的UIApplicationLaunchOptionsRemoteNotificationKey项:didFinishLaunchingWithOptions:launchOptions。
无论如何,请确保您在发送任何通知之前注册了您的观察员以接收通知。
解决方案: - >日志registerObserver和postNotification呼叫可能有助于
Thank you for reply.problem is不是NotificationIdentifier拼写错误。如果我在methodOFReceivedNotication方法中显示警报,但它工作正常,但webview加载不起作用。 – 2015-02-12 13:02:46
是否显示webview?或没有加载? 你有没有尝试设置Web视图框架? 'webView.frame = self.view.bounds;' – kamidude 2015-02-12 13:17:19
谢谢。这是工作 。 – 2015-02-13 05:25:24
你能告诉我们一些你目前的代码,以便我们可以帮助你纠正它吗? 您还可以显示推送通知的转储吗? (无效)应用程序:(UIApplication *)应用程序didReceiveRemoteNotification:(NSDictionary *)userInfo {NSLog(@“%@”,userInfo); }' – kamidude 2015-02-12 08:47:46
FUNC申请(申请:UIApplication的, didReceiveRemoteNotification USERINFO:[NSObject的:AnyObject]) { NSNotificationCenter.defaultCenter()postNotificationName( “NotificationIdentifier”,对象:无)。 } 倍率FUNC viewDidLoad中() { super.viewDidLoad() NSNotificationCenter.defaultCenter()的addObserver(个体,选择器: “methodOFReceivedNotication:”,名称: “NotificationIdentifier”,对象:无)。 } – 2015-02-12 09:25:38
FUNC methodOFReceivedNotication(通知:NSNotification) { 变种web视图= WKWebView() var url:NSURL = NSURL(s tring:“http://m.xyz.com”) var request = NSURLRequest(URL:url) webView.loadRequest(request) self.view.addSubview(webView) } – 2015-02-12 09:26:02