So when my app is not currently open and it receives a push notification, the "didrecieveremotenotification" works perfectly fine when the banner appears and you press it. But when the app is open, no banner at all appears. I want the push notification to work when the app is open aswell.
I tried using UNNotificationCenter but lost push notifications in general.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted) {
UIApplication.shared.registerForRemoteNotifications()
} else{
print("Notification permissions not granted")
}
})
}
//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.sound, .alert, .badge])
}
//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Swift.Void) {
completionHandler()
}
Update: UNUserNotificationCenter.current().requestAuthorization runs
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
when permission is granted.
however when I don't use UNNotificationCenter and use old method it doesn't fail.
Why would one method fail to register for remote notifications but the other not?
Update 2: Was working in simulator lol, that's why it was failing. However now when I run the
UNUserNotificationCenterDelegate
methods only the
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive method works and not the
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
Update 3: I've ran out of possible things to try. What would be stopping the code from firing willPresent but not didReceive. This is brutal. Has anyone ever had this issue before?
Update 4: SOLVED. The service I'm using for the push notifications requires the user to be disconnected from the service to send the push notification. It automatically disconnects you when you're in the background, which is why that was working and it wasn't working in the foreground.
Weird way for them to configure that, I'll probably send an email.
Implement UNUserNotificationCenterDelegate delegate methods to get notification (tray at top) while app is in foreground. But it will only work with IOS 10.
In your didFinishLaunchingWithOptions method set UNUserNotificationCenterDelegate delegate like this.
UNUserNotificationCenter.current().delegate = self
Implement Delegate methods...
//Called when a notification is delivered to a foreground app.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Swift.Void) {
completionHandler([.sound, .alert, .badge])
}
//Called to let your app know which action was selected by the user for a given notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Swift.Void) {
completionHandler()
}
Related
When my app is not running and a user taps on the notification in the notification center, the didFinishLaunchingWithOptions function is called with push data passed into the launchOptions dictionary.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
//do stuff with push data in launchOptions dictionary
}
but also the userNotificationCenter didRecieve function is called
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
log.info()
let state = UIApplication.shared.applicationState
print(state)
if UIApplication.shared.applicationState != .active {
log.debug("exiting because app is not in active state")
return
}
let userInfo = response.notification.request.content.userInfo
//do stuff with userInfo
completionHandler()
}
In didRecieve function, how can i tell that the app was launched versus the app was already running in the background when the user tapped on the notification?
The application is active when the app is in the foreground but it is inactive when the app is in the background or was launched from the push, so i can't tell the difference.
The application didRecieveRemoteNotification looks ideal but it is only called when i send a silent push but my pushes aren't silent.
Do you have to try using willPresent function for handle your push in foreground case? Something like this
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping () -> Void) {
handlePushWhenForeground(notification?.request.content.userInfo) // implement this function
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if UIApplication.shared.applicationState == .inactive {
handlePushWhenBackground(response.notification.request.content.userInfo) // implement this function
} else {
handlePushWhenNotActivated(response.notification.request.content.userInfo) // implement this function
}
}
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 want to find out which method is triggered when a terminated application receives a notification from my server. The application is being developed through Swift 4, my Deployment Target is 10.3 and Firebase is used to send notifications to users.
I've configured my application in AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
}
Additionally, I created the extension:
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
}
}
When the app receives a notification and it's running or it's in background, the method "willPresent" is called and when the user taps on the notification and decide to open it, the method "didReceive" is called. No method is triggered if the application is terminated.
Am I doing something wrong?
I have implemented remote notifications in my app, however I have problems with didReceiveRemoteNotification:fetchCompletionHandler:.
When the phone's screen is locked, I am getting the notification. If the user swipes the notification, app comes back to foreground and didReceiveRemoteNotification:fetchCompletionHandler: is called.
But if the app is running in the foreground, didReceiveRemoteNotification:fetchCompletionHandler: is never called. What might be the reason?
I'm working on iOS 10.3.2
Which iOS version you are working on ?
There are different methods for iOS 9 and iOS 10.
Below are the ones for iOS 10
//Called when a notification is delivered to a foreground app.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
}
//Called to let your app know which action was selected by the user for a given notification.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
}
For iOS 9, make sure to register your notification in didBecomeActive
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
and in your didReceiveRemoteNotification
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
if application.applicationState == .active {
//foreground state
}
}
Best approach would be to put a debugger in didReceiveRemoteNotification and check further.
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.
}
}