Disable push notification in specific view controller in foreground? - ios

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.

Related

Remote Notification will present not working ios 15.5

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.sound,.banner,.badge])
}
In application did-finish
UNUserNotificationCenter.current().delegate = self
I see the notification when the app in the background but do not see it in the foreground.

How to save push notification data into local db when application closed in iOS swift

Able to store push notification data when app is in foreground and background(when launch notification).
But not on application killed or inactive state in iOS.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
defer { completionHandler() }
guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else { return }
// perform action here
}
WillPresent is calling when application is in open state,
did receive is calling when user taps on notification.
In these scenarios able to save notification data.
How to save data When application killed and when no interaction with notification.
How to achieve this ?

Get Action of Push Notification Allow Button

I'm working on a ios Application,
now I want to get the action of push notification's allow button,
the user will be push to second view controller once user select one of push notification option(don't allow, allow).
You need to call these methods in AppDelegate Class.
//Ios 10 delegates for Push Notifications
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping ( _ options: UNNotificationPresentationOptions) -> Void){
print("Handle push from foreground")
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void){
print("Handle push from background or closed")
}

How to show firebase notifications when application is in foreground

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

How to use UNNotificationPresentationOptions

In iOS 10 , there is an option for presenting the notification when the app is in foreground using UNNotificationPresentationOptions,
but i couldn't find any sample on how to use this, please suggest some idea about how to implement this feature
I have implemented the foreground notification by
Adding the below code in my viewController
extension UIViewController: UNUserNotificationCenterDelegate {
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler( [.alert, .badge, .sound])
}
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Swift.Void) {
print("Do what ever you want")
}
}
In my Appdelegate on didFinishLaunchingWithOptions
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
if !accepted {
print("Notification access denied")
}
}
The new iOS 10 UNUserNotificationCenterDelegate now has a single set of methods for handling both remote and local notifications.
UNUserNotificationCenterDelegate protocol:
userNotificationCenter(_:didReceive:withCompletionHandler:)
Called to let your app know which action was selected by the user for a given notification.
userNotificationCenter(_:willPresent:withCompletionHandler:)
Delivers a notification to an app running in the foreground.
Two methods. And what’s better, is that now that they are moved into their own protocol, the iOS 10
UNUserNotificationCenterDelegate so this will help clean up your existing UIApplicationDelegate by being able to refactor all that old notification handling code into a shiny, new, cohesive protocol of it’s own.
Here’s an example:
extension NotificationManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
switch response.actionIdentifier {
// NotificationActions is a custom String enum I've defined
case NotificationActions.HighFive.rawValue:
print("High Five Delivered!")
default: break
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
// Delivers a notification to an app running in the foreground.
}
}

Resources