title: SwiftUI with NotificationCenter publishers
body: <p>I want to listen to notifications when the app goes to the background and comes back. I'm trying to use the NotificationCenter publishers and have the SwiftUI view listen to them.<br/>
  I can use a few methods to do it and I'm trying to use two of them but the interesting thing is, that although all seem legit when I put the subscriber in the <code>init()</code> method, it just does not work. <br/>
  I tried to put it on the <code>main</code> thread but still no success.<br/>
  Does anyone have any idea why?<br/>
  Here's my code:</p>
  
  <pre><code>struct ContentView: View {
      @State var isActive = true
      @State var cancellables = Set&lt;AnyCancellable&gt;()
      var body: some View {
          ZStack {
              Image("background")
                  .resizable()
                  .scaledToFill()
                  .edgesIgnoringSafeArea(.all)                        
          }
          .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
              self.isActive = false
          }
          .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification), perform: {_ in
              self.isActive = true
          })
      }
  
      init() {
          NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)
           //   .receive(on: RunLoop.main)
              .sink(receiveValue: { _ in
                  print("init")
              }
              .store(in: &amp;cancellables)
      }
  }
  </code></pre>
  
  <p>Strangely the listener in the <code>onReceive</code> modifier works like a charm. In the <code>init()</code> the <code>print("init")</code> never gets called.</p>