My app has a function that pops up information in a notification in a few seconds when a button is clicked.
It used to work before, but these days it does not.
I checked and the permission request window does not appear.
Do I need to fix something? Code is below:
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound]) { (result, Error) in
print("result = \(result)")
}
AppDelegate:
UNUserNotificationCenter.current().delegate = self
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions)
-> Void) {
completionHandler([.alert, .badge, .sound])
}
Related
I'm trying to listen user clear push notification action. I've the code below, It's working when the app is on the foreground but when i put the app on the background it doesn't go into that function. Is there any way that i can do some coding when the user clear the notification on the background state?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if(response.actionIdentifier == UNNotificationDismissActionIdentifier){
...
//I need to do some coding here!
}
}
I use this for receiving remote notifications behind the scenes:
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
// Process this userInfo dictionary
processNotification(dictionary: userInfo)
}
In my willFinishLaunchingWithOptions in AppDelegate I call this:
fileprivate func setupAPN(application: UIApplication) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
MobilePlatformPush.setRemoteDelegate(delegate: self)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
application.registerForRemoteNotifications()
}
Is it possible to get the UNNotificationRequest identifier of a tapped local notification button or any other information about the notification? Maybe via a delegate?
Thankyou
The identifier of the notification is obtained like so:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let identifier = response.notification.request.identifier
switch response.actionIdentifier {
case "snoozeAction":
print ("Snooze Tapped - identifier :", identifier)
nc.post(name: Notification.Name("didTapSnooze"), object: nil)
default:
break
}
completionHandler()
}
I'm working on an app that handles push notifications.
For some reason,
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
Works as expected, but
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
is never called.
Anyone ever come across this situation?
All the usual suspects are in place:
UNUserNotificationCenter.current().delegate = self
is set in didFinishLaunchingWithOptions
let center = UNUserNotificationCenter.current()
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
center.requestAuthorization(options:options) { (granted, error) in
if error == nil{
DispatchQueue.main.async() {
UIApplication.shared.registerForRemoteNotifications()
completion(granted)
}
}
}
is returning true (permission granted)
Other items worth mentioning:
The Push notifications are sent via SendBird
When the app is in the background, everything works as expected.
Times and again, read the documentation.
Apple states for func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void):
Asks the delegate how to handle a notification that arrived while the app was running in the foreground.
That is, only when your app is running in FORGROUND and you get a notification will the system call this method to ask you whether this notification should still be shown to the user since they're already inside your app. You can call the completionHandler and pass in options like alert and sound so the user will still see the banner and hear the sound. If you only pass back sound, then the user sees no banner and only hears the notification sound.
Here are the details of the error when app throw error to register the notification:
<EXPR>:3:1: error: expected member name or constructor call after type name
Error
^
<EXPR>:3:1: note: use '.self' to reference the type object
Error
^
.self
Here is the code which is running successfully in iOS 10.1
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Failed to register Notification Error = ", Error)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print("Device Token = ", Data)
}
func registerForRemoteNotification()
{
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge])
{ (granted, error) in
if error == nil
{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else
{
if #available(iOS 8.0, *)
{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
else
{
UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}
}
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
{
if let dicAps = notification.request.content.userInfo["aps"] as? NSDictionary
{
AppUtilities.showAlertWithMessage(title: APP_TITLE as NSString, message: "\(dicAps.object(forKey: "msg")!)" as NSString)
}
completionHandler([.alert, .badge, .sound])
}
//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)
{
print("User Info = ",response.notification.request.content.userInfo)
}
I am trying to implement an app which will run on iOS 8 to 10.1., it works fine in iPhone 5s with iOS 10.1.1. but when I run the app in the simulator with iOS 8.1, it always throws an error which is described above, please correct me if I missed anything.
Thank you.
Sorry to say, but you'll need to find some hardware to test this functionality.
Push notifications are not available in the simulator. They require a provisioning profile from iTunes Connect, and thus are required to be installed on a device. That also means you'll probably have to be accepted into the apple iPhone developer program and pay your $99.
Source : How can I test Apple Push Notification Service without an iPhone?
Yesterday I updated 2 of my apps to swift 3.
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
UIApplication.shared.registerForRemoteNotifications()
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print(error.localizedDescription)
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Save Installation if registered successfully
}
//MARK: Recieved Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
print("received a notification")
}
so background push notification works fine on both apps, but in one of my app its not receiving any pushes in foreground that is, didReceiveRemoteNotification is never getting called
Things I have checked, Push Notifications enabled in Capabilities
Using this code to register for push notifications on both apps
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil))
Not only that, I have tried UNUserNotificationCenterDelegate for iOS 10 but still none of its delegate functions get called.
This only doesn't work on iOS 10 phones, iOS 8 and 9 works like charm.
So I'm not really sure why its never calling didReceiveRemoteNotification in only one of my 2 swift 3 apps on ios 10 when the app is open
This is my iOS 10 code I tried
//Added in didFinishLaunchingWithOptions
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
//Delegates
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("push2")
}
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("push1")
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.initializeNotificationServices()
}
func initializeNotificationServices() -> Void {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted {
//self.registerCategory()
//self.scheduleNotification("test", interval: 3)
//self.scheduleNotification("test2", interval: 5)
let types : UIUserNotificationType = [.Badge, .Sound, .Alert]
let mySettings : UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
}
else {
// Fallback on earlier versions
let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// This is an asynchronous method to retrieve a Device Token
// Callbacks are in AppDelegate.swift
// Success = didRegisterForRemoteNotificationsWithDeviceToken
// Fail = didFailToRegisterForRemoteNotificationsWithError
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
completionHandler([.Badge, .Alert, .Sound])
}
#available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
self.didReceiveBookingAppRemoteNotification(response.notification.request.content.userInfo)
print("didReceive == >> \(response.notification.request.content.userInfo)")
completionHandler()
}
//for Lower to ios 10 version
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
self.getNotifInfo(userInfo)
}
Make sure you have to enable push notification from project's
Target => Capabilities and you have to add it's framework UserNotifications.framework