点击swift 2中的推送通知时如何打开网址?

时间:2016-05-25 17:10:50

标签: ios push-notification swift2 wkwebview

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

以下是代码:

App Delegate:

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

       UIApplication.sharedApplication().applicationIconBadgeNumber = 0

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

}

的ViewController:

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\"}}"
}

1 个答案:

答案 0 :(得分:0)

将此添加到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视图init的视图的Tag / Storyboardid。

然后像这样添加这个方法

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的东西。然后,在您之前链接的视图控制器中的Web视图中,您可以将所需URL的商店值传递到Web视图的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)
}

}