I want to show custom UI for notification when App is running but.(Like in whats app). How can i do this custom view. I tried do this in userNotificationCenter but its only showing default notification.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
I want to show notification like whats app when app is runing. Do you have any idea how to do this?
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
showUIViewController()
}
func showUIViewController() {
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "MyPage", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "ContactUsDetailViewController") as! ContactUsDetailViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
First of all, after calling showUIViewController() call the completionHandler with an empty array:
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
showUIViewController()
//1
completionHandler([])
}
1- This code will prevent the system to show the default iOS notification.
Then, Your piece of code to show custom UI for notifications like whatsapp does not make sense.
You should create a custom view and add/remove it as a subview of UIWindow instead of changing its rootViewController.
I think if you want to customize the notification when app is in foreground , I have used the third party and it's really cool and easy to implement, Check this out
https://github.com/souzainf3/RNNotificationView
Related
I have so many screens in my application. So when the user taps on the local notification , I'd like to deep-link one of the screens.
The could be not running or active on one of those screens or inactive or suspended state.
I couldn't see how to do that
If anyone could explain it'd be great
You can use DeepLinkKit for navigation to different screens. I'm using this library in my app, I have about 30-37 deeplinks for every screen in the app. I'm using this with Cordinators pattern for navigation. Let me know if you need any help with implementing.
Send Notification from anywhere.
func sendNotification(){
let info: [String: String] = ["vc_name": "CartViewController"]
NotificationCenter.default.post(name: Notification.Name("YourLocalNotification"), object: nil, userInfo: info)
}
Add Notification handler on Receive Notification in your root controller.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.youNotificationAction(notification:)), name: Notification.Name("YourLocalNotification"), object: nil)
}
Receive notification, Check your notified information and navigate to your respective controller.
#objc func youNotificationAction(notification: Notification) {
if let vcName = notification.userInfo?["vc_name"] as? String {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let toVC = storyBoard.instantiateViewController(withIdentifier: vcName)
navigationController?.pushViewController(toVC, animated: true);
}
}
Inherit UNUserNotificationCenterDelegate in appdelegate or viewcontroller
Implement below methods
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNNotificationPresentationOptions.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
completionHandler()
}
This is where you intercept the notification tap and call the appropriate modules either using OpenURL or doing a direct push or present or switch the tab of viewcontroller.
I want to show notification only in specific pages like if the userid obtained from payload of notification is not equal to the current userid. Where should I use this condition inside appdelegate? I have used willPresent delegate of userNotificationCenter to deliver the notification when app is in foreground and requesting permission to show notification method in didFinishLauchingWithOptions in AppDelegate.
Sample code of willPresent method is given below.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void){
if let userID = notification.request.content.userInfo["user_id"] {
self.userIDSelected = userID as! String
}
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.removeAllDeliveredNotifications()
if self.currentuserID != self.userIDSelected
{
completionHandler([.alert,.badge,.sound])
}
else
{
completionHandler([])
}
}
I am implementing a chat feature in an app. Right now every push I am using this to show notification while using app:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
I am wondering if it is possible to disable push notification from showing at top, while maintaining the buzzer for a specific view controller?
If yes, what should I do to create it?
Implement it like this:
UNUserNotificationCenter.current().delegate = self
...
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
if wantToShow {
completionHandler([.alert, .sound])
} else {
completionHandler([])
}
}
Then change wantToShow according to the current state.
I am using firebase for push notification. I want to show a notification to the user when the application is in the foreground.
Notifications are working fine when the application is closed. But I want to display a stock iOS notification banner even if the application is active.
I have referred below link:
Displaying a stock iOS notification banner when your app is open and in the foreground?
I want to show notification exactly the same way as shown in above link.
I have already implemented these two methods:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
completionHandler()
}
Can someone please give me a sample of code for showing push notifications when the application is in the foreground?
You can try
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
}
For more info check here Notifications
You can call this function, for push notification:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
print("\(userInfo)")
}
I have an app written in Swift that uses UNUserNotificationCenter and I have it presenting notifications when the app is in the foreground.
What I want to do is update the UI once the notification has been delivered and the app is in the foreground The following presents the notification just fine, but when it is presented, I also want to execute a function called updateUI() as the notification date is in my UI and I want to clear it as soon as the notification appears.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
I don't know how to add the call to updateUI() in the completion handler.
You can POST new notification from you AppDelegate and add Observer inside your controller file to change in UI.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
UIApplication.shared.applicationIconBadgeNumber = 0
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NewNotification") , object: nil, userInfo: response.notification.request.content.userInfo)
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert)
}
Add Notification Observer in your controller file :
NotificationCenter.default.addObserver(self, selector: #selector(pushNotificationHandler(_:)) , name: NSNotification.Name(rawValue: "NewNotification"), object: nil)
then call UI update method :
func pushNotificationHandler(_ notification : NSNotification) {
self.updateUI()
}