Opening a url in my webview app from a notification not working when the app is coming from closed
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a webview ios app that receives notifications and I pass a url so that when the user clicks on the notification it will open the webview to that url.
When the app is in the foreground and background it works fine. If the user gets the notification when the app is closed and not currently running then the app opens but does not go to that url
In my didReceiveRemoteNotification
I detect the different states of the app but I thought that .background would work the same as not running but I guess it doesn't. How can I get the notification to open the url coming from when the app is closed?
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
//print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
else if state == .inactive {
// inactive
//print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
UPDATE
So with some help I have been able to use didFinishLaunchingWithOptions
to call my webview, but the notification when pressed is still not opening to the url.
I use viewController?.loadRequestnotification(for: url as! String)
in some other areas of my delegate that works fine. I am suspecting the return true
might be conflicting the call.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] as? [String : AnyObject] {
let url = object["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
return true
}
ios swift webview swift4.2
|
show 3 more comments
I have a webview ios app that receives notifications and I pass a url so that when the user clicks on the notification it will open the webview to that url.
When the app is in the foreground and background it works fine. If the user gets the notification when the app is closed and not currently running then the app opens but does not go to that url
In my didReceiveRemoteNotification
I detect the different states of the app but I thought that .background would work the same as not running but I guess it doesn't. How can I get the notification to open the url coming from when the app is closed?
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
//print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
else if state == .inactive {
// inactive
//print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
UPDATE
So with some help I have been able to use didFinishLaunchingWithOptions
to call my webview, but the notification when pressed is still not opening to the url.
I use viewController?.loadRequestnotification(for: url as! String)
in some other areas of my delegate that works fine. I am suspecting the return true
might be conflicting the call.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] as? [String : AnyObject] {
let url = object["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
return true
}
ios swift webview swift4.2
1
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
Have you checked thatviewController
isn'tnil
? You could make a check and if it is, store the url in a variable and pass it onviewController.didSet
– EmilioPelaez
Jan 3 at 4:49
when the app is closed state you receive notification inapplication(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49
|
show 3 more comments
I have a webview ios app that receives notifications and I pass a url so that when the user clicks on the notification it will open the webview to that url.
When the app is in the foreground and background it works fine. If the user gets the notification when the app is closed and not currently running then the app opens but does not go to that url
In my didReceiveRemoteNotification
I detect the different states of the app but I thought that .background would work the same as not running but I guess it doesn't. How can I get the notification to open the url coming from when the app is closed?
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
//print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
else if state == .inactive {
// inactive
//print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
UPDATE
So with some help I have been able to use didFinishLaunchingWithOptions
to call my webview, but the notification when pressed is still not opening to the url.
I use viewController?.loadRequestnotification(for: url as! String)
in some other areas of my delegate that works fine. I am suspecting the return true
might be conflicting the call.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] as? [String : AnyObject] {
let url = object["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
return true
}
ios swift webview swift4.2
I have a webview ios app that receives notifications and I pass a url so that when the user clicks on the notification it will open the webview to that url.
When the app is in the foreground and background it works fine. If the user gets the notification when the app is closed and not currently running then the app opens but does not go to that url
In my didReceiveRemoteNotification
I detect the different states of the app but I thought that .background would work the same as not running but I guess it doesn't. How can I get the notification to open the url coming from when the app is closed?
AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let data = userInfo as! [String: AnyObject]
let state = UIApplication.shared.applicationState
if state == .background {
// background
//print("==== Active Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
else if state == .inactive {
// inactive
//print("==== Inactive Running ====")
if let aps = data["aps"] {
let url = aps["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
UPDATE
So with some help I have been able to use didFinishLaunchingWithOptions
to call my webview, but the notification when pressed is still not opening to the url.
I use viewController?.loadRequestnotification(for: url as! String)
in some other areas of my delegate that works fine. I am suspecting the return true
might be conflicting the call.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
ConnectionManager.sharedInstance.observeReachability()
// Override point for customization after application launch.
FirebaseApp.configure()
registerForPushNotifications()
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] as? [String : AnyObject] {
let url = object["url"]
viewController?.loadRequestnotification(for: url as! String)
}
}
}
return true
}
ios swift webview swift4.2
ios swift webview swift4.2
edited Jan 3 at 6:58
Cesar Bielich
asked Jan 3 at 4:43


Cesar BielichCesar Bielich
2,23042652
2,23042652
1
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
Have you checked thatviewController
isn'tnil
? You could make a check and if it is, store the url in a variable and pass it onviewController.didSet
– EmilioPelaez
Jan 3 at 4:49
when the app is closed state you receive notification inapplication(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49
|
show 3 more comments
1
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
Have you checked thatviewController
isn'tnil
? You could make a check and if it is, store the url in a variable and pass it onviewController.didSet
– EmilioPelaez
Jan 3 at 4:49
when the app is closed state you receive notification inapplication(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49
1
1
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
Have you checked that
viewController
isn't nil
? You could make a check and if it is, store the url in a variable and pass it on viewController.didSet
– EmilioPelaez
Jan 3 at 4:49
Have you checked that
viewController
isn't nil
? You could make a check and if it is, store the url in a variable and pass it on viewController.didSet
– EmilioPelaez
Jan 3 at 4:49
when the app is closed state you receive notification in
application(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49
when the app is closed state you receive notification in
application(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49
|
show 3 more comments
3 Answers
3
active
oldest
votes
There is one scenario like your app is not running and user click on your app's notification then following the way you can get it.
Here is code you can get it
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: (dictionary)")
}
}
}
Your app can received some notification and it is in notification center but user can not click on any notification but they will open your app as normally then following is a way, you can get all notification which is received by your app.
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
add a comment |
This is the function that called when app receives any notification.
I have used this in my chatting app.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
If app is open and you want to perform some action when notification is received
Use this method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
You can also check weather the app is open from notification or not in
AppDelegate's didFinishLaunchingWithOptions
But it is recommended to keep the this didFinishLaunchingWithOptions
method light as possible.
I hope this will work for you
add a comment |
didReceiveRemoteNotification won't be called while app is closed.
Try this code when your app is closed to get notification data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming thatlaunchOptions
will contain the data from the notification when it was pressed?
– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016449%2fopening-a-url-in-my-webview-app-from-a-notification-not-working-when-the-app-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is one scenario like your app is not running and user click on your app's notification then following the way you can get it.
Here is code you can get it
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: (dictionary)")
}
}
}
Your app can received some notification and it is in notification center but user can not click on any notification but they will open your app as normally then following is a way, you can get all notification which is received by your app.
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
add a comment |
There is one scenario like your app is not running and user click on your app's notification then following the way you can get it.
Here is code you can get it
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: (dictionary)")
}
}
}
Your app can received some notification and it is in notification center but user can not click on any notification but they will open your app as normally then following is a way, you can get all notification which is received by your app.
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
add a comment |
There is one scenario like your app is not running and user click on your app's notification then following the way you can get it.
Here is code you can get it
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: (dictionary)")
}
}
}
Your app can received some notification and it is in notification center but user can not click on any notification but they will open your app as normally then following is a way, you can get all notification which is received by your app.
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
There is one scenario like your app is not running and user click on your app's notification then following the way you can get it.
Here is code you can get it
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let notification = launchOptions?[.remoteNotification] as? [String: Any] {
if let dictionary:NSDictionary = notification as? NSDictionary{
print("Dictionary Print in didFinishLaunching :: (dictionary)")
}
}
}
Your app can received some notification and it is in notification center but user can not click on any notification but they will open your app as normally then following is a way, you can get all notification which is received by your app.
UNUserNotificationCenter.current().getDeliveredNotifications { (notification) in
print(notification.count)
}
answered Jan 3 at 6:04


Urvish PatelUrvish Patel
6818
6818
add a comment |
add a comment |
This is the function that called when app receives any notification.
I have used this in my chatting app.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
If app is open and you want to perform some action when notification is received
Use this method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
You can also check weather the app is open from notification or not in
AppDelegate's didFinishLaunchingWithOptions
But it is recommended to keep the this didFinishLaunchingWithOptions
method light as possible.
I hope this will work for you
add a comment |
This is the function that called when app receives any notification.
I have used this in my chatting app.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
If app is open and you want to perform some action when notification is received
Use this method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
You can also check weather the app is open from notification or not in
AppDelegate's didFinishLaunchingWithOptions
But it is recommended to keep the this didFinishLaunchingWithOptions
method light as possible.
I hope this will work for you
add a comment |
This is the function that called when app receives any notification.
I have used this in my chatting app.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
If app is open and you want to perform some action when notification is received
Use this method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
You can also check weather the app is open from notification or not in
AppDelegate's didFinishLaunchingWithOptions
But it is recommended to keep the this didFinishLaunchingWithOptions
method light as possible.
I hope this will work for you
This is the function that called when app receives any notification.
I have used this in my chatting app.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if application.applicationState == .active {
//Application is currently active and user receive the notification
} else if application.applicationState == .background {
//app is in background, but not killed
} else if application.applicationState == .inactive {
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
//Load your URL into webView from here
}
}
If app is open and you want to perform some action when notification is received
Use this method
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNAuthorizationOptions.alert.rawValue | UIUserNotificationType.sound.rawValue | UIUserNotificationType.badge.rawValue)
}
You can also check weather the app is open from notification or not in
AppDelegate's didFinishLaunchingWithOptions
But it is recommended to keep the this didFinishLaunchingWithOptions
method light as possible.
I hope this will work for you
answered Jan 3 at 6:30


VIJAY SINGH RAGHAVVIJAY SINGH RAGHAV
309
309
add a comment |
add a comment |
didReceiveRemoteNotification won't be called while app is closed.
Try this code when your app is closed to get notification data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming thatlaunchOptions
will contain the data from the notification when it was pressed?
– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
|
show 3 more comments
didReceiveRemoteNotification won't be called while app is closed.
Try this code when your app is closed to get notification data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming thatlaunchOptions
will contain the data from the notification when it was pressed?
– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
|
show 3 more comments
didReceiveRemoteNotification won't be called while app is closed.
Try this code when your app is closed to get notification data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
didReceiveRemoteNotification won't be called while app is closed.
Try this code when your app is closed to get notification data.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if launchOptions != nil {
// opened from a push notification when the app is closed
let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any]
if userInfo != nil {
if let object = userInfo?["aps"] {
let url = object["url"]")
// Now set root controller here
}
}
} else {
// opened app without a push notification.
}
}
edited Jan 3 at 7:14
answered Jan 3 at 5:12


PunitPunit
849310
849310
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming thatlaunchOptions
will contain the data from the notification when it was pressed?
– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
|
show 3 more comments
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming thatlaunchOptions
will contain the data from the notification when it was pressed?
– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
take a look at my question. I have edited it. Got farther with your answer but the webview is still not opening the url. It just opens the app
– Cesar Bielich
Jan 3 at 7:00
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
are you getting url for webview in notification? and you have to set root controller (viewController -that contains webview) to open it.
– Punit
Jan 3 at 7:06
I am assuming that
launchOptions
will contain the data from the notification when it was pressed?– Cesar Bielich
Jan 3 at 7:09
I am assuming that
launchOptions
will contain the data from the notification when it was pressed?– Cesar Bielich
Jan 3 at 7:09
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you are doing something wrong with viewController?.loadRequestnotification(for: url as! String)
– Punit
Jan 3 at 7:10
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
you cant start viewcontroller like this,instead you have to set it as root controller search it.
– Punit
Jan 3 at 7:11
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016449%2fopening-a-url-in-my-webview-app-from-a-notification-not-working-when-the-app-is%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
AppDelegate.php ??? you are developing iOS app in PHP!!!
– SPatel
Jan 3 at 4:47
App was killed state, when notification is coming then your app in initialize and it's start from application lunch state, so that controller is not initialize before. it will never redirect to webview controller. first you need to identify your rootviewcontroller and then redirect to webviewcontroller. then it's work
– Hitesh
Jan 3 at 4:47
At this point of time check didFinishLaunchingWithOptions -> launchOptions, there should be some dictionary with information.Try debugging this more deep.When that dictionary contains that url, redirect to respective viewcontroller.
– Sakshi
Jan 3 at 4:48
Have you checked that
viewController
isn'tnil
? You could make a check and if it is, store the url in a variable and pass it onviewController.didSet
– EmilioPelaez
Jan 3 at 4:49
when the app is closed state you receive notification in
application(_: didLaunch option:_)
– SPatel
Jan 3 at 4:49