gpt4 book ai didi

ios - 单击 swift 2 中的推送通知时如何打开 url?

转载 作者:行者123 更新时间:2023-11-29 11:59:48 25 4
gpt4 key购买 nike

我实际上是在使用 Amazon Web Services SNS 在我的 IOS 应用程序中发送推送通知。当应用程序在后台运行时单击推送通知时,url(由推送通知发送)在 WebView(WKWebView) 中正确打开。我得到的唯一问题是,当应用程序关闭时,url 不会在 WebView 中打开。我该如何解决这个问题?

代码如下:

应用委托(delegate):

func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

NSNotificationCenter.defaultCenter().postNotificationName("ReceivedPushNotification", object: userInfo)

}

View Controller :

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.receivedUrlFromPushNotification(_:)), name: "ReceivedPushNotification", object: nil)

}

func receivedUrlFromPushNotification(notification: NSNotification){

let JSONData = notification.object!["aps"] as! NSDictionary
let dictionary: NSDictionary = JSONData
let v = dictionary.allValues[2] as! String
let url = "http://\(v)"
self.webView!.loadRequest(NSURLRequest(URL: NSURL(string:url)!))
}

在推送通知中发送 JSON 代码:

{
"APNS_SANDBOX":"{\"aps\":{\"alert\":\"test\",\"badge\":1,\"sound\":\"default\",\"url\":\"www.example_samplelink.com\"}}"
}

最佳答案

将此添加到您的 didFinishLaunchWithOptions:

   //handel push note if app is closed
//Sends it to the regular handler for push notifcaiton
//didrecivepushnotificaiton
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
{
self.application(application, didReceiveRemoteNotification: remoteNotification as [NSObject : AnyObject])
}


if launchOptions != nil
{
print(launchOptions)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(myPage)
window?.rootViewController = vc
}

其中 myPage 是一个字符串,它是您要打开的具有 Web View 初始化的 View 的标签/ Storyboard ID。

然后像这样添加这个方法

func application( application: UIApplication,
didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

if var alertDict = userInfo["aps"] as? Dictionary<String, String> {
let url = alertDict["url"]!
//store the url for the push control view
loginInformation.setObject(url, forKey: "URL")
self.loginInformation.synchronize()

}else{print("No go")}
application.applicationIconBadgeNumber = 0
//post notification.
NSNotificationCenter.defaultCenter().postNotificationName("PushReceived", object: nil, userInfo: userInfo)

}

其中 loginInformation 是一个 NSDefault 东西。然后在之前链接的 View Controller 中的 Web View 中,您可以将所需 url 的存储值传递到 web View url 的变量中。您可能需要稍微修改一下才能使其正常工作。但这确实有效。只需要一些设置

当您将此 json 传递给系统时,这将起作用。这正是我的做法。

[aps: {
alert = "Hello from APNs Tester.";
badge = 1;
url = "http://www.google.com";
}]

希望对你有帮助

这是您需要使用的 viewcontrller 类

class PushNotificationController: UIViewController {

var defualtUrl : String = "http://www.IStare@Butts.com"
var storedUrl : String = ""
var useUrl : String = ""
let loginInformation = NSUserDefaults.standardUserDefaults()

override func viewDidLoad() {
super.viewDidLoad()

//add observer for load request in webview when receive remote notification.
NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(PushNotificationController.PushReceiver(_:)), name: "PushReceived", object: nil)
/*
if let alertDict = userInfo["aps"] as? Dictionary<String, String> {
print("URL :", alertDict["url"]!)
}else{print("No go")}
*/

if loginInformation.objectForKey("URL") != nil
{
storedUrl = loginInformation.objectForKey("URL") as! String
print("Stored url: " + storedUrl )
if storedUrl.isEmpty
{
useUrl = defualtUrl
}else{
useUrl = storedUrl
}
}else
{
useUrl = defualtUrl
}


print("URL Using: " + useUrl)
let myUrl = NSURL (string: useUrl);
let requestObj = NSURLRequest(URL: myUrl!);
pushNotePlayer.loadRequest(requestObj)

//after it is loaded reset it to the defualt url if there is no other thing next time
loginInformation.setObject(defualtUrl, forKey: "URL")
self.loginInformation.synchronize()

}
@IBOutlet weak var pushNotePlayer: UIWebView!

//When post notification then below method is called.
func PushReceiver(notifi: NSNotification)
{
let dicNotifi: [NSObject : AnyObject] = notifi.userInfo!
NSLog("notificiation Info %@ \n", dicNotifi)
}

}

关于ios - 单击 swift 2 中的推送通知时如何打开 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com