bsorrentino / cordova-broadcaster

Cordova Plugin to allow message exchange between javascript and native (and viceversa)
MIT License
113 stars 53 forks source link

[iOS] Broadcast doesn't work. #42

Closed ShivamPokhriyal closed 5 years ago

ShivamPokhriyal commented 5 years ago

I have to send broadcast from iOS. I used following code which is not working.

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "callback"), object: nil, userInfo: nil)

this.broadcaster = new Broadcaster();
          this.broadcaster.addEventListener("callback").subscribe((event) => {  // you can subscribe for multiple events from native
            console.log("received event : ");
         });

I even checked if something was wrong with the broadcast by listening to it in native side only and it was working perfectly fine there.

NotificationCenter.default.addObserver(self, selector: #selector(checkBroadcast(_:)), name: NSNotification.Name(rawValue: "callback"), object: nil)

func checkBroadcast(_ notif: NSNotification){
    print("Success");
}

The same is working fine when i am sending broadcasts from android side using following code:


Intent intent = new Intent("callback");
        intent.putExtras(bundle);
        LocalBroadcastManager.getInstance(context).sendBroadcastSync(intent);
bsorrentino commented 5 years ago

HI @ShivamPokhriyal

The follow code isn't correct

this.broadcaster = new Broadcaster();
this.broadcaster.addEventListener("callback").subscribe((event) => {  // you can subscribe for multiple events from native
            console.log("received event : ");
         });

try code below instead

window.broadcaster.addEventListener("callback").subscribe((event) => {  // you can subscribe for multiple events from native
            console.log("received event : ");
         });
ShivamPokhriyal commented 5 years ago

@bsorrentino I am using typescript and that code worked perfectly well in case of android. I made it work for iOS too, I did find one interesting thing though, it was not working when i had this code in constructor, but when i put that in one of the methods, it worked.

You can close this thread. Thankyou.

bsorrentino commented 5 years ago

Hi @ShivamPokhriyal thanks for feedback.

Probably the constructor run before cordova stuff has been setup