I'm trying to implement $broadcast
and $on
in >ionic2
and I found this solution here in Stackoverflow
using $on and $broadcast with angular js 2 , well I thought it was working perfect, since now.
I've implemented oneSignal
like this :
var iosSettings = {};
iosSettings["kOSSettingsKeyAutoPrompt"] = true;
iosSettings["kOSSettingsKeyInAppLaunchURL"] = false;
window["plugins"].OneSignal.startInit("123123", "123123")
.inFocusDisplaying(window["plugins"].OneSignal.OSInFocusDisplayOption.None)
.iOSSettings(iosSettings)
.handleNotificationReceived(function (jsonData) {
alert(JSON.stringify(jsonData));
if (jsonData["payload"] != null && jsonData["payload"]["additionalData"] != null) {
alert("first"); //<------ FIRST ALERT
var data = jsonData["payload"]["additionalData"];
notificationOpenedCallback(data);
}
})
.handleNotificationOpened(function (jsonData) {
if (jsonData["notification"] != null && jsonData["notification"]["payload"] != null) {
var data = jsonData["notification"]["payload"]["additionalData"];
notificationOpenedCallback(data);
}
})
.endInit();
My notificationOpenedCallback(data)
has this on its body
var notificationOpenedCallback = function (jsonData) {
if (jsonData != null) {
if (jsonData["type"] == "example") {
alert("second"); //<------ SECOND ALERT
this.sharedService.broadcast({
name: 'example'
});
}
Since here is OK, the problem comes when I'm from the other page...
this.sharedService.on('example', (event) => {
alert("pewpew"); // <--- 3RD ALERT
});
I don't get what I'm doing wrong I have an Ionic1
app and I hace the same code, the only different thing is that I've created the sharedServiceProvider
to "emulate" the $broadcast
and $on
, this is my class
export class SharedServiceServiceProvider {
observable: any;
observer: any;
constructor() {
this.observable = Observable.create(observer => {
this.observer = observer;
}).share();
}
broadcast(event) {
this.observer.next(event);
}
on(eventName, callback) {
this.observable.filter((event) => {
return event.name === eventName;
}).subscribe(callback);
}
}
Problem
I'm able to see the FIRST, SECOND alert, but not the 3rd one.
EDIT
I've created a sample demo here on GitHub-oneSignal-ionic2
. The only thing you have to do (if you wanna play with that) is change the OneSignal key
and create a template for your notification.
UPDATE EDIT
I've changed the .handleNotificationOpened(function (jsonData) {
to .handleNotificationOpened((jsonData) => {
now, the alert()
is shown, BUT the problem is that I have something like this :
this.sharedService.on('example', (event) => {
alert("test");
this.showStatus();
this.getActiveGuy(function () {
});
});
But seems like those 2 methods are called but not doing the job, those methos do a refresh of the page and it does not... and I have to make the refresh (Scroll to down) and then it refreshes, BUT that's why on my onRefresh()
I have those methods....