I have two type of push notifications in my app Type A and Type B and according to each type I want to navigate the user to different view controls. right now the app show only the homeVC if user clicked on the
notification and can I pass value from the notifications object to the
view controls.?
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
userInfo["Type"] as? String == "TypeA" {
showNotificationA()
} else {
showNotificationB()
}
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// How to identify which notification the user clicked to navigate to the right view?
if UIApplication.shared.applicationState == .inactive {
}
completionHandler()
}
Basically, the handling of push notifications consists more or less of the following steps:
Enabling Push notifications in the capabilities section of project settings
Requesting the access to use remote and local notifications, and delegating incoming notifications to your notification observer entity (class, struct)
This is done in the AppDelegate class, in a similar manner to:
UNUserNotificationCenter
.current()
.requestAuthorization(options: [.alert, .badge]) { (granted, error) in
UNUserNotificationCenter.current().delegate = MyCustomDelegateEntity
}
Implementing the func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) in your delegate, which receives incoming notifications
Notifications can be distinguished by an identifier, which is located in the UNNotificationResponse entity. Based on the id, you can decide how to react accordingly.
In your particular case, you should instantiate a desired view controller from the NotificationDelegate in a similar manner to
let rootVC = MyViewController.instantiateFrom(storyboard: "StoryboardName")
let navigationController = UINavigationController(rootViewController: rootVC)
(UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController = navigationController
The ViewController.instantiateFrom(storyboard:) content looks something like:
return UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: String(describing: self)) as! MyViewController
You can redirect all your notification to one controller and in this basview controller check which notification type its came from and navigate accordingly.
You can pass data from notification payload and handle accordingly
Related
I have researched and came across a few articles about opening local notifications to a specific view and showing the information within the view controller. Unfortunately, I'm still unable to make the function work in my app.
I have a quote app and I send local and push notifications. When I have a scheduled notification, the user should be able to click the notfication and be directed to the "TopViewController" and the controller should display the notification information. Instead the app opens up to the last view controller the user was on.
Could someone tell me what I am doing wrong?
Here is the code the I have in the "TopViewController" ViewDidLoad
NotificationCenter.default.addObserver(self, selector: ("Scheduled:"), name:NSNotification.Name(rawValue: "Scheduled"), object: nil)
Here is the code I have in the AppDelegate:
#Published var quote = Quote(id: "", text: "", author: "")
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let application = UIApplication.shared
if(application.applicationState == .active){
print("user tapped the notification bar when the app is in foreground")
}
if(application.applicationState == .inactive)
{
print("user tapped the notification bar when the app is in background")
}
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
return
}
print("got active scene")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let conversationVC = storyboard.instantiateViewController(withIdentifier: "TopViewController") as? TopViewController,
let tabBarController = self.window?.rootViewController as? UITabBarController
{
conversationVC.shareQuote?.text = response.notification.request.content.body
conversationVC.shareQuote?.author = response.notification.request.content.subtitle
}
completionHandler()
}
The notification doesn't open up to the right view controller nor does it show the notification information.
Here are a few ideas I have, however, I cannot be certain if this is your fix but it was too much to put into the comments.
Are you setting the app delegate as the UNNotificationCenter's delegate ?
UNUserNotificationCenter.current().delegate = self
Does anything print to your console like print("got active scene") or print("user tapped the notification bar when the app is in foreground") ?
If you have done both of the above, can you add breakpoints inside this block of code
{
conversationVC.shareQuote?.text = response.notification.request.content.body
conversationVC.shareQuote?.author = response.notification.request.content.subtitle
}
This will tell you if there is some issue in your logic if it does not enter this block of code
Update
To manage notifications in the foreground, you need to also implement this UNUserNotificationCenterDelegate method:
// This will show the notification when the app is in
// the foreground. This is optional
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.list, .banner, .badge, .sound])
}
I'm configuring push notifications in Swift. So far I have 3 scenarios.
1 - App In Foreground
In the foreground, I think I did everything correct cus I did receive the push notification data.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("userNotificationCenter willPresent")
let content = notification.request.content
UIApplication.shared.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
completionHandler([.alert, .sound])
}
2 - User clicks on the Push Notification banner
This is also working fine.
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
print("userNotificationCenter didReceive")
defer {
completionHandler()
}
guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {
return
}
let content = response.notification.request.content
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
3 - App in background, then the user gets into the app
In this scenario, the push notification arrives at the user's phone. But, instead of clicking on the push notification itself, they get into the app. And I can't fetch any info from the push notification
Could anyone help on how to configure the 3rd scenario? Thank you.
you need to consider applicationState
UIApplication.State
//AppDelegate
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
switch UIApplication.shared.applicationState {
case .active:
print("Received push message from APNs on Foreground")
case .background:
print("Received push message from APNs on Background")
case .inactive:
print("Received push message from APNs back to Foreground")
}
}
When the app is background to foreground, UIApplication.State is inactive
inactive is 'The app is running in the foreground but is not receiving events.'
thus I think the best way to do the behavior you want is to write it yourself.
for example,
//AppDelegate
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
switch UIApplication.shared.applicationState {
case .active:
print("Received push message from APNs on Foreground")
case .background:
print("Received push message from APNs on Background")
case .inactive:
print("Received push message from APNs back to Foreground")
guard let nav = window?.rootViewController as? UINavigationController,
let currentVC = nav.viewControllers.last else {return}
if currentVC is 'youWantViewController' { //if you want ViewController, use notification post
let name = Notification.Name(rawValue: K.Event.pushRequest)
NotificationCenter.default.post(name: name, object: nil)
} else { //move to you want ViewController
let vc = 'yourViewController'()
root.navigationController?.pushViewController(vc, animated: true)
}
}
completionHandler(.newData)
}
I hope it will be of help.
UNUserNotificationCenterDelegate is not working when is remove from background. It is working fine while app stays in foreground and background.
Here is code
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let rootViewController = self.window!.rootViewController as! UINavigationController
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "SampleViewController") as! SampleViewController
rootViewController.pushViewController(profileViewController, animated: true)
completionHandler()
}
}
When app is remove from background delegate method is not called. Please help me out
When adding UserNotifications framework to a project, here are 5 things to check:
1) If you are dealing with remote notifications, be sure you have enabled that in your project's capabilities settings page of Xcode.
See figure 1
If that gives you an error, you probably need to log into your apple developer account and create a key with push notification credentials. The end result gives you a .p8 file to download. The creation of this file will clear up the error. The file itself is needed on the server that sends push notifications to your app/device. If you need help with this, check out this tutorial.
2) Add import line to the top of your AppDelegate file
import UserNotifications
3) Add UNUserNotificationCenterDelegate to your AppDelegate class declaration line.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {...
4) Set UNUserNotificationCenter delegate to self in the application:didFinishLaunchingWithOptions method.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//...
UNUserNotificationCenter.current().delegate = self
//...
return true
}
5) Add your delegate functions (if you want to use them)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
NSLog("Application delegate method userNotificationCenter:didReceive:withCompletionHandler: is called with user info: %#", response.notification.request.content.userInfo)
//...
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("userNotificationCenter:willPresent")
//...
completionHandler([.alert])
}
Hope that helps.
check didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? method. When user taps a notification banner (and the app is terminated) the app is started, this method is called and launchOptions contains notification info. Therefore you can understand (by checking the launchOptions) that the app is launched by tapping on notification banner and navigate user wherever you want to
I'm doing an app that schedules local notifications and saves an userInfo. That's part its ok.
But when the app is closed, if a Notification appears and the user clicks, the method is not called and I can't handle userInfo.
I saw that there's a new way to receive a notification with UNUserNotificationCenter. But is not working too.
I've tried it that way, but I did not succeed:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let yourData = userInfo["yourKey"] as? String {
// Handle your data here, pass it to a view controller etc.
}
}
That's my implementation in AppDelegate:
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let lNotification = UILocalNotification()
lNotification.userInfo = response.notification.request.content.userInfo
// Handle your data here, pass it to a view controller etc.
}
Anyone, to help me? I saw all the questions related here and didn't found anything.
Have you registered for notifications?
If not, add this to AppDelegate didFinishLaunchingWithOptions:
// Register Notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in
if granted {
print("User notifications are allowed")
} else {
print("User notifications are NOT allowed")
}
})
UNUserNotificationCenter.current().delegate = self
I have implemented local notification successfully for iOS 10.2.
But problem is that notification alert only comes if the app is in the background no alert come if the app is foreground.
Is it possible to get local notification in the foreground?
My code is here
func notificationNow(){
print("notification will be triggered in five seconds..Hold on tight")
let content = UNMutableNotificationContent()
content.title = "Intro to Notifications"
content.subtitle = "Lets code,Talk is cheap"
content.body = "Sample code from WWDC"
content.sound = UNNotificationSound.default()
//To Present image in notification
if let path = Bundle.main.path(forResource: "menu2", ofType: "png") {
let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "sampleImage", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("attachment not found.")
}
}
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription as Any)
}
}
}
In the documentation for UNUserNotificationCenterDelegate:
Important
You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching. For example, in an iOS app, you must assign it in the application(:willFinishLaunchingWithOptions:) or application(:didFinishLaunchingWithOptions:) method.
You appear to be setting the delegate much later — just before the notification is added to the notification center.
I created a simple Swift class singleton with an extension to conform to UNUserNotificationCenterDelegate. In the singleton's init method I assigned the delegate to self. I then initialize the singleton in the AppDelegate's willFinishLaunchingWithOptions method.
Write following code (extension) in the class where you want to observe the local notification
This will notify when you receive notification in foreground or user tapped on notification when the app is in background.
Hope this will solve your problem.
extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("Tapped in notification")
}
//This is key callback to present notification while the app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
//You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
//to distinguish between notifications
// if notification.request.identifier == requestIdentifier
// {
completionHandler( [.alert,.sound,.badge])
// }
}
}
When the app is running in Foreground. You need to capture the Local Notification through delegate methods.
So in your AppDelegate implement the listening for delegate in didFinishLaunchingWithOptions method:
It is important to set the delegate before your app finishes launching.
// Do NOT forget to retain your delegate somewhere
let notificationDelegate = UYLNotificationDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = notificationDelegate
// ...
return true
}
If you want to respond to actionable notifications or receive notifications while your app is in the foreground you need to implement the UNUserNotificationCenterDelegate. This protocol defines two optional methods:
userNotificationCenter(_:willPresent:withCompletionHandler:) is
called when a notification is delivered to a foreground app. You
receive the UNNotification object which contains the original
UNNotificationRequest. You call the completion handler with the
UNNotificationPresentationOptions you want to present (use .none to
ignore the alert).
userNotificationCenter(_:didReceive:withCompletionHandler:) is called
when a user selects an action in a delivered notification. You
receive the UNNotificationResponse object which includes the
actionIdentifier for the user action and the UNNotification object.
The system defined identifiers UNNotificationDefaultActionIdentifier
and UNNotificationDismissActionIdentifier are used when the user taps
the notification to open the app or swipes to dismiss the
notification.
In both cases you must call the completion handler once you finish.
#pragma mark - UNUserNotificationCenterDelegate Methods
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// Play sound and show alert to the user
completionHandler([.alert,.sound])
}
Use below method to configure notification to show in foreground,
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
You can refer Apple documentation for more details!