Hey everybody,
I'm using FCM on android with nativescript-plugin-firebase to implement a push notification feature in my app. I initialized firebase inside the constructor of my major component in a setTimeOut like advised by Eddy Verbruggen :
setTimeout(() => {
firebase.init({
onPushTokenReceivedCallback: (token) => {
deviceToken = token;
console.log("Firebase push token: " + token);
},
onMessageReceivedCallback: (message) => {
that.ID = message.data.ID;
if (message.foreground) {
dialogs.confirm({
title: "Alerte article",
message: message.body,
okButtonText: "Voir l'article",
cancelButtonText: "Ignorer"
}).then(function(result) {
if (result) {
that.zone.run(() => {
that.goArticle(message.data.ID);
});
}
console.log("Dialog result: " + result);
});
} else {
dialogs.confirm({
title: "Alerte article",
message: message.title,
okButtonText: "Voir l'article",
cancelButtonText: "Ignorer"
}).then(function(result) {
if (result) {
that.zone.run(() => {
that.routerExtensions.navigate(["/article", message.data.ID]);
});
}
});
}
console.log(message.data.ID);
},
persist: false
}).then((instance) => {
firebase.subscribeToTopic("all");
console.log("firebase.init done");
}, (error) => {
console.log("firebase.init error: " + error);
});
}, 3000);
// END ANDROID SEGMENT
}
On foreground it works perfectly, and on background the article page is reached (I see console logs from constructor and ngOnInit) but not displayed. I tested a lot of possible solutions (externalize to a function, setTimeOut, ngZone, promise calls, a combination of all of these) but nothing worked.
What do you think @EddyVerbruggen.
Thank you all and I apologize for my approximative english.
@EddyVerbruggen did you already encountred this issue?
I'm not quite sure, but are you trying to show a confirm dialog when the app is not in the foreground?
You're best off asking this elsewhere atm because I'm on vacation. Please reopen in 2 weeks (with some repo I can reproduce this with) if this is still an issue then.
Hey guys, any solution or update to this?
I am having this problem using vanilla JS.
Using the getFrameByID method for the core modules and the navigate() method simply does not work when the app is in the background and the notification is tapped via the system tray.
See this page : https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md
This code worked for me:
firebase.addOnMessageReceivedCallback(
function(message) {
// ..
}
);
Did you put the firebase.addOnMessageReceivedCallback( function(message) { // .. } ); in the Firebase.init function?
No, I put it into the constructor of my main component.
Still giving me trouble unfortunately. Making an educated guess: It seems like it's more of a frame/navigation problem. When using the onMessageReceivedCallback in the init method it is actually being called but it will not navigate. It seems that when opening my app either through the notification tray, or the app icon, it seems that it's creating a new instance of the app, which messes up the frame navigation.
Ok, I'll give you my code in typescript:
`
const that = this;
firebase
.addOnMessageReceivedCallback(function(message) {
that.ID = message.data.ID;
if (message.foreground) {
dialogs
.confirm({
title: "Alerte article",
message: message.body.replace(/\\/g, ""),
okButtonText: "Voir l'article",
cancelButtonText: "Ignorer"
})
.then(function(result) {
if (result) {
that.zone.run(() => {
that.goArticle(message.data.ID);
});
}
console.log("Dialog result: " + result);
});
} else {
that.routerExtensions.navigate(["/article", message.data.ID]);
}
})
.then(
instance => {
firebase.subscribeToTopic("all");
console.log("firebase.init done");
},
error => {
console.log("firebase.init error: " + error);
}
);
`
Yeah still not working with vanilla JS, I appreciate your help though
I hope @EddyVerbruggen will take a look at you problem.
Yeah, for the record here is a snippet of my code
onMessageReceivedCallback: (message) => {
// if(application.ios) {
const notification = NotificationNavigator.parseNotification(message)
NotificationNavigator.handleNotification(notification);
// }
},
Handle Notification Method, where navigation takes place
if(navigationData.navigateTo === REQUEST_STATUS_PAGE) {
const frame = frameModule.getFrameById(frameID);
frame.parent.parent.selectedIndex = 1;
const navigationEntry = {
moduleName: REQUEST_STATUS_PAGE ,
context: notification.notificationData.request,
clearHistory: true
};
frame.navigate(navigationEntry);
Again everything works except the navigation. I tested adding functionality such as adding alert dialogs, and that works. But navigation does not work when the notification is tapped in the system notification tray when the app is in the background.
Adding
android:launchMode="singleTask"
to the main activity in AndroidManifest.xml solved this issue for me. Clicking the system notification would always restart the application until this line was added.
Most helpful comment
See this page : https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md
This code worked for me:
firebase.addOnMessageReceivedCallback( function(message) { // .. } );