Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 用于本地和推送通知的Rx\U Swift_Ios_Swift_Reactive Programming_Rx Swift - Fatal编程技术网

Ios 用于本地和推送通知的Rx\U Swift

Ios 用于本地和推送通知的Rx\U Swift,ios,swift,reactive-programming,rx-swift,Ios,Swift,Reactive Programming,Rx Swift,当收到本地通知和推送通知时,如何实现可观察的本地通知和推送通知。 应用程序内代理,我们将在 didReceiveLocalNotification 及 如何在其他屏幕上侦听这些通知?我曾使用NotificationCenter进行通知,但现在我想使用RX Swift。我试过这种方法,但不起作用 extension UILocalNotification { var notificationSignal : Observable<UILocalNotification> {

当收到本地通知和推送通知时,如何实现可观察的本地通知和推送通知。 应用程序内代理,我们将在

didReceiveLocalNotification

如何在其他屏幕上侦听这些通知?我曾使用NotificationCenter进行通知,但现在我想使用RX Swift。我试过这种方法,但不起作用

  extension UILocalNotification {
   var notificationSignal : Observable<UILocalNotification> {
        return Observable.create { observer in
            observer.on(.Next(self))
            observer.on(.Completed)
            return NopDisposable.instance
        }
    }
}
然后观察任何其他课程

    NotificationClass.bus.asObserver()
        .subscribeNext { notification in
            if let notification : UILocalNotification = (notification as! UILocalNotification) {
                print(notification.alertBody)
            }
    }
    .addDisposableTo(disposeBag)

这个类最好的东西是我们可以通过它发射和消费任何对象。

像这样的东西怎么样

// this would really be UILocalNotification
struct Notif {
    let message: String
}

class MyAppDelegate {
    let localNotification = PublishSubject<Notif>()

    // this would really be `application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)`
    func didReceiveLocalNotification(notif: Notif) {
        localNotification.on(.Next(notif))
    }
}

let appDelegate = MyAppDelegate() // this singleton would normally come from `UIApplication.sharedApplication().delegate`

class SomeClassA {
    let disposeBag = DisposeBag()

    init() {
        appDelegate.localNotification
            .subscribe { notif in
                print(notif)
            }
            .addDisposableTo(disposeBag)
    }
}

let a = SomeClassA()

appDelegate.didReceiveLocalNotification(Notif(message: "notif 1"))

let b = SomeClassA()

appDelegate.didReceiveLocalNotification(Notif(message: "notif 2"))
//这确实是UILocalNotification
结构Notif{
让消息:字符串
}
类MyAppDelegate{
让localNotification=PublishSubject()
//这实际上是`应用程序(应用程序:UIApplication,didReceiveLocalNotification通知:UILocalNotification)`
func direceivelocalnotification(notif:notif){
localNotification.on(.Next(notif))
}
}
让appDelegate=MyAppDelegate()//此单例通常来自'UIApplication.sharedApplication().delegate'`
A类{
设disposeBag=disposeBag()
init(){
appDelegate.localNotification
.订阅{notif in
打印(notif)
}
.addDisposableTo(disposeBag)
}
}
设a=SomeClassA()
appDelegate.didReceiveLocalNotification(Notif(消息:“Notif 1”))
设b=SomeClassA()
didReceiveLocalNotification(Notif(消息:“Notif 2”))

我还在学习RxSwift,所以这可能不是最好的方法。但是,它是有效的。

您应该将您的更新答案从问题移动到完整答案,然后接受您移动的完整答案作为对OP问题的回答。
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    NotificationClass.send(notification)
}
    NotificationClass.bus.asObserver()
        .subscribeNext { notification in
            if let notification : UILocalNotification = (notification as! UILocalNotification) {
                print(notification.alertBody)
            }
    }
    .addDisposableTo(disposeBag)
// this would really be UILocalNotification
struct Notif {
    let message: String
}

class MyAppDelegate {
    let localNotification = PublishSubject<Notif>()

    // this would really be `application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)`
    func didReceiveLocalNotification(notif: Notif) {
        localNotification.on(.Next(notif))
    }
}

let appDelegate = MyAppDelegate() // this singleton would normally come from `UIApplication.sharedApplication().delegate`

class SomeClassA {
    let disposeBag = DisposeBag()

    init() {
        appDelegate.localNotification
            .subscribe { notif in
                print(notif)
            }
            .addDisposableTo(disposeBag)
    }
}

let a = SomeClassA()

appDelegate.didReceiveLocalNotification(Notif(message: "notif 1"))

let b = SomeClassA()

appDelegate.didReceiveLocalNotification(Notif(message: "notif 2"))