I have an AppleWatch App, which receives remote Notifications. I get a callback on my notificationCenter(_:didRecieve response:...) when handling my custom action "a1" and the default action. However, this func isn't called for the custom dismiss action...
class ExtensionDelegate: NSObject, WKExtensionDelegate, UNUserNotificationCenterDelegate {
func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound]) { (success, err) in
}
let a1 = UNNotificationAction(identifier: "a1", title: "Do Stuff", options: .foreground)
let c1 = UNNotificationCategory(identifier: "c1", actions: [a1], intentIdentifiers: [], options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([c1])
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("Action Identifier: \(response.actionIdentifier)"
completionHandler()
}
}
so my delegate seems to work and the categories too... what am I missing?
Fixed it: if you run your app on the Watch and dismiss it, the notificationCenter(_:didRecieve response:...) of your iOS App's UNUserNotificationCenterDelegate is called (in my case the AppDelegate)!
Probably because when you dismiss it on the watch it is dismissed everywhere...
Didn't find any resources tho.. if you did,
please share the link :)
Related
This app is intentionally tiny - the UI is just a button to initiate the api call. The appDelegate simply shows a console log proving the callback was executed. I did this because I can not get the Square payment system to work even thought the code and url setup etc looks right to me. I am wondering if I misunderstand something in the documentation.
My app ID is: sq0idp-TbgQGqSrC84qkfcXSTntNg
bundleID is: com.craig.POSSDKTest2
My info.plist setup is a screenshot:info.plist and URLScheme setup
My code in the api calling VC:
// ViewController.swift
// POSSDKTest2
//
// Created by Dr Craig E Curphey on 2022-06-14.
//
import UIKit
import SquarePointOfSaleSDK
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}//------------------------------------
#IBAction func doPaymentTapped(_ sender: Any) {
//------ API to Square right here ******-----------------
let callbackURL = URL(string: "POSSDKTest2Scheme://")!
SCCAPIRequest.setApplicationID("sq0idp-TbgQGqSrC84qkfcXSTntNg")
var money: SCCMoney?
do {
// Specify the amount of money to charge - $1.00
let todaysFeePennies = 100
money = try SCCMoney(amountCents: todaysFeePennies, currencyCode: "CAD")
} catch {
print("money conversion failed.")
}
let LoginID = "TestPatient"
//log values used to console:
print("Initiating payment api request")
print("callback: \(callbackURL)")
print("money: \(money!)")
print("LoginID: TestPatient")
print("Creating payment api request")
do
{
let apiRequest =
try SCCAPIRequest(
callbackURL: callbackURL,
amount: money!,
userInfoString: LoginID,
locationID: "",
notes: "TestingSDK2",
customerID: "tester2",
supportedTenderTypes: .all,
clearsDefaultFees: false,
returnsAutomaticallyAfterPayment: false,
disablesKeyedInCardEntry: false,
skipsReceipt: false
)
print("api initiating!")
try SCCAPIConnection.perform(apiRequest)
} catch let error as NSError {
print("Error detected: \(error.localizedDescription)")
}//end API
print("End of payment api function.")
}//------------------------------------
}
Here is my appDelegate code - I just want to see if I get a callback - I'll add the code to process the response later ...
// AppDelegate.swift
// POSSDKTest2
//
// Created by Dr Craig E Curphey on 2022-06-14.
//
var squareTokenID = "not specified - let it fail - I just want a callback!"
import UIKit
import SquarePointOfSaleSDK
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
//-------------------------------------------
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
print("Callback was executed!")
guard SCCAPIResponse.isSquareResponse(url) else {
print("\(SCCAPIResponse.isSquareResponse(url))")
return true
}
do {
let response = try SCCAPIResponse(responseURL: url)
if let error = response.error {
// Handle a failed request.
print(error.localizedDescription)
} else {
// Handle a successful request.
}
} catch let error as NSError {
// Handle unexpected errors.
print(error.localizedDescription)
}
return true
}//-------------------------------------------------------
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}//-------------------------------------------------------
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}//--------------------------------------------------------
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}//---------------------------------------------------------
}
Screenshot of console log from trying to do the api:
Console log
The work environment is Xcode 13.3, Swift, macOSMonterey 12.1 beta, iPad 10.2" (I think) mounted in a Square iPad kiosk with attached card reader, running iOS 15.5.
I would be very happy to send the whole project in a zip file to anyone who would be willing to take a look into why it is failing.
Using Xcode 13, typing any substring of print suggests printContent() first in the Xcode code completion list above the common Swift print() function(s).
printContent(_ sender: Any?)
Tells your app to print available content.
"Jump to Definition" displays the following declaration, new for iOS 15 in 2021:
public protocol UIResponderStandardEditActions : NSObjectProtocol {
// ...
#available(iOS 15.0, *)
optional func printContent(_ sender: Any?)
}
What is the printContent() function and how is it used?
Is it in any way a new better replacement for print(), justifying its prominent code completion location in Xcode?
If not, how can I go back to pre-Xcode 13 behavior and suggest the extremely common print() function first in the list?
If your app includes the UIApplicationSupportsPrintCommand key in its Info.plist file, people can print from your app using the keyboard shortcut Command-P, which calls printContent(_:). You can also set printContent(_:) as the action on other print-related controls such as a print button on a toolbar.
override func printContent(_ sender: Any?) {
let info = UIPrintInfo.printInfo()
info.outputType = .photo
info.orientation = .portrait
info.jobName = modelItem.title
let printInteractionController = UIPrintInteractionController()
printInteractionController.printInfo = info
printInteractionController.printingItem = modelItem.image
let completionHandler: UIPrintInteractionController.CompletionHandler = {
(controller: UIPrintInteractionController, completed: Bool, error: Error?) in
if let error = error {
Logger().error("Print failed due to an error: \(error.localizedDescription)")
}
}
if traitCollection.userInterfaceIdiom == .pad {
if let printButton = navigationItem.rightBarButtonItem {
printInteractionController.present(from: printButton, animated: true, completionHandler: completionHandler)
}
} else {
printInteractionController.present(animated: true, completionHandler: completionHandler)
}
}
Apple developer link
I want to write a project has a notification can print data which stored in main viewcontroller, but when notification fired, always get empty data.
In the ViewController
arrPerson = [String]()
override func viewDidLoad() {
super.viewDidLoad()
arrPerson.append("Peter")
arrPerson.append("Jack")
setNotification()
}
func printTheName() {
print("In printTheName")
print("arrPerson:\(arrPerson.count)")
for x in arrPerson {
print(x)
}
}
func setNotification() {
let notification = UNMutableNotificationContent()
notification.title = "title"
notification.subtitle = ""
notification.body = "body"
notification.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "check", content: notification, trigger: trigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
print("notificationCenter.add ERROR:\(error)")
// Handle any errors.
}
}
}
In the Appdelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("In userNotificationCenter")
ViewController().printTheName()
}
The code will print out "userNotificationCenter","In printTheName","arrPerson:0".
I need get data from notification. Why arrPerson.count is 0 when call printTheName from notification?
Try to programatically instantiate the viewController using a window in your appdelegate.
You will be able to fetch the count then in your notification center.Or since your viewController class is the landing screen, Call the userNotifationCenter method in your main Class and not here.
I created a code that receive remote push norification, this code is worked ok. Now I need to add two "buttons" that swip left and do a action. I know that this below code is used to indetify the action
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
if identifier == "optin1" {
//do something
}
else identifier == "option2" {
//do something
}
completionHandler()
}
But I dont knew how to create the buttons to swip left. How can I do it
This is my AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
let types:UIUserNotificationType = [.Alert, .Sound, .Badge]
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))
initLocationManager()
return true
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
//print(error)
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print(deviceToken)
}
func application(application: UIApplication, didReceiveRemoteNotificationuserInfo userInfo: [NSObject : AnyObject]) {
print(userInfo)
}
Update: new version Swift 4 compatible. This example was adapted from this amazing tutorial https://cocoacasts.com/actionable-notifications-with-the-user-notifications-framework
Configure User Notification Center
import UserNotifications
...
UNUserNotificationCenter.current().delegate = self
Define Actions
let actionReadLater = UNNotificationAction(identifier: Constants.Action.readLater,
title: "Read Later",
options: [])
Define Category
let tutorialCategory = UNNotificationCategory(identifier: Constants.Category.tutorial,
actions: [actionReadLater],
intentIdentifiers: [],
options: [])
Register Category
UNUserNotificationCenter.current().setNotificationCategories([tutorialCategory])
Schedule local notifications by instance from an IBAction
// Request Notification Settings
UNUserNotificationCenter.current().getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .notDetermined:
self.requestAuthorization(completionHandler: { (success) in
guard success else { return }
// Schedule Local Notification
self.scheduleLocalNotification()
})
case .authorized:
// Schedule Local Notification
self.scheduleLocalNotification()
case .denied:
print("Application Not Allowed to Display Notifications")
case .provisional:
print("provisional")
}
}
previous version
You can copy&paste this code in didFinishLaunchingWithOptions method:
Create the action
// NOTFICATION
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = "HI_ACTION"
incrementAction.title = "Hi!"
incrementAction.activationMode = UIUserNotificationActivationMode.Background
incrementAction.authenticationRequired = false
incrementAction.destructive = false
Create the category
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = "HELLO_CATEGORY"
Associate action and category
counterCategory.setActions([incrementAction],
forContext: UIUserNotificationActionContext.Default)
counterCategory.setActions([incrementAction],
forContext: UIUserNotificationActionContext.Minimal)
Registration
let categories = NSSet(object: counterCategory) as! Set<UIUserNotificationCategory>
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
The last step is only for demostration
let notification = UILocalNotification()
notification.alertBody = "Hey!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
notification.category = "HELLO_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
When the app is launched you have to push CMD+L
Here's a link to an example
I tried implementing the NSWindowRestoration protocol in Swift, in a non-document-based application. However, the method restoreWindowWithIdentifier is never called at application launch. Can anyone point out my mistake?
Here is a subset of the code (which compiles and runs fine):
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowRestoration {
var windowController : MyWindowController?
func applicationDidFinishLaunching(aNotification: NSNotification?) {
windowController = MyWindowController(windowNibName:"ImageSequenceView")
}
class func restoreWindowWithIdentifier(identifier: String!, state: NSCoder!, completionHandler: ((NSWindow!,NSError!) -> Void)!) {
NSLog("restoreWindowWithIdentifier: \(identifier), state: \(state)")
}
}
class MyWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad();
window.restorationClass = AppDelegate.self
}
}
Thanks in advance!
You need to set a restoration class and also an identifier:
class MyWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
self.window?.restorationClass = type(of: self)
self.window?.identifier = "MyWindow"
}
}
extension MyWindowController: NSWindowRestoration {
static func restoreWindow(withIdentifier identifier: String, state: NSCoder, completionHandler: #escaping (NSWindow?, Error?) -> Void) {
if identifier == "MyWindow" {
// Restore the window here
}
}
}
Of course you can also let another class restore the window, like you tried. You need to assign AppDelegate.self as the restorationClass in that case.
Also, be aware that the window restoration setting now defaults to "off", for whatever stupid reason.