Get Action of Push Notification Allow Button - ios

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

Related

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 ?

Disable push notification in specific view controller in foreground?

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.

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

UNUserNotificationCenter need to willPresent code

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

Notify application when a notification has been received and not tapped on or dismissed

On iOS 10, is there any delegate to notify application that notification was not tapped on or it was cancelled?
I mean, how to get notification data even if the user has not tapped on it?
I have those two delegate methods which respond only when the user taps the notification:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (_ options: UNNotificationPresentationOptions) -> Void) {}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {}
Below method will be called when notification arrives.
func didReceive(_ notification: UNNotification)
see this apple doc for further information. Notifications apple

Resources