How to Create Custom Notifications in Swift 3

How do you create custom notifications in Swift 3?

You could also use a protocol for this

protocol NotificationName {
var name: Notification.Name { get }
}

extension RawRepresentable where RawValue == String, Self: NotificationName {
var name: Notification.Name {
get {
return Notification.Name(self.rawValue)
}
}
}

And then define your notification names as an enum anywhere you want. For example:

class MyClass {
enum Notifications: String, NotificationName {
case myNotification
}
}

And use it like

NotificationCenter.default.post(name: Notifications.myNotification.name, object: nil)

This way the notification names will be decoupled from the Foundation Notification.Name. And you will only have to modify your protocol in case the implementation for Notification.Name changes.

How to use Notification.Name extension from Swift to Objective-C?

Notification.Name doesn't exist in Objective-C. And the Objective-C type NotificationName is really just an NSString. To use Swift stuff in Objective-C, the class must be available in both, and can't be a Swift struct (like Notification or String, say).

To do what you want, then, you need to have two extensions:

  • one for the Swift Notification.Name, as you have; and,
  • one for an Objective-C object (NSString, say, or perhaps NSNotification if you prefer).

1) Add an Objective-C-compatible object extension to your Swift file:

public extension NSNotification {
public static let blahblahblah: NSString = "blahblahblah"
}

Note: in Swift 4, properties must be computed for Objective-C compatibility. That would look like:

@objc public extension NSNotification {
public static var blahblahblah: NSString {
return "blahblahblah"
}
}

Note the var in the computed property: computed properties can't be immutable, so can't use let.

2) In the Objective-C file, import Xcode's generated Swift header file (below any other imports):

#import "YourProjectName-Swift.h"

Note: replace YourProjectName with the actual name of your project. So, if your project is named "CoolGameApp", the Swift header would be "CoolGameApp-Swift.h". If your project name has spaces, like "Cool Game App", replace them with dashes: "Cool-Game-App-Swift.h"

3) Rebuild the project.

Now, you should be able to access the extension in Objective-C:

[[NSNotificationCenter defaultCenter] postNotificationName:NSNotification.blahblahblah object:self];

How we can make and put a custom Notification which fires up with changing a custom variable? (in SwiftUI or UIKit)

You can create a custom notification:

extension Notification.Name {
static let customNotification = Notification.Name("customNotification")
}

And use it like this:

struct ContentView: View {
@State var test = 0

var body: some View {
Button("Increment") {
test += 1
}
.onChange(of: test) { value in
NotificationCenter.default.post(name: .customNotification, object: value)
}
}
}

You may listen to this notification wherever you want. Here is an example for a SwiftUI view:

.onReceive(NotificationCenter.default.publisher(for: .customNotification)) { notification in
if let value = notification.object as? Int {
print(value)
}
}

Add Custom Local Notification in ios10 - swift 3

Update: As of iOS 10 beta 2, rich notifications are also available on pre-3D touch devices. Pull down on the regular notification to see it.

Make sure you are testing on a iPhone6s/iPhone6s plus simulator/device, it doesn't seem to work on pre-3D touch devices.

On a iPhone6 simulator, try to click and drag down on the stock notification you get and you should see your custom UI appear.

type of expression is ambiguous without more context for NSNotification

Swift 4 changed how you create notification names.

Change your code to:

private let visageNoFaceDetectedNotification = NSNotification(name: NSNotification.Name(rawValue: "visageNoFaceDetectedNotification"), object: nil)

How to customize short preview of notifications in iOS 10+

I just stumbled upon the following line in the Customizing the Appearance of Notifications article in the official documentation:

The system provides the interface for the abbreviated banner, but you can customize the full interface using a notification content app extension.

This confirms that currently there is no way of customizing the short preview ("abbreviated banner" in the docs).



Related Topics



Leave a reply



Submit