don’t remove the regular firebase init which you do in your app.ts/app.js/main.ts
That firebase init, will initialize firebase when the app is opened normally.
And this firebase init, inside the broadcast receiver’s call back, can be made directly inside the callback function
if (app.android) {
app.android.registerBroadcastReceiver(android.content.Intent.ACTION_BOOT_COMPLETED,
function onReceiveCallback(context: android.content.Context, intent: android.content.Intent) {
console.log('broadcast ok');
let firebase = require("nativescript-plugin-firebase");
console.log('firebase ok');
firebase.init({
onPushTokenReceivedCallback: (token) => {
//save token to app settings storage
appSettings.setString('Token', token);
if (appSettings.hasKey('Token')) {
this.dev_token = token;
this.platformInfo.device_token = token;
this.registerDevice();
}
},
onMessageReceivedCallback: (message) => {
if (message.website !== undefined) {
if (appSettings.hasKey('Login')) {
console.log(message.website);
dialogs.confirm({
title: "New mention about you",
message: "You were mentioned in " + message.website,
okButtonText: "Open Link",
cancelButtonText: "Cancel"
}).then(result => {
if (result == true) {
this.browseWeb(message.website);
}
});
} else {
Toast.makeText('Please login to view news about you!').show();
}
}
},
//persist should be set to false as otherwise numbers aren't returned during livesync
persist: false,
}).then(function (instance) {
console.log("-------Firebase.init done------");
}), function (error) {
console.log("firebase.init error: " + error);
};
});
}
Also for this broadcast receiver to get attached first, you need to open the app once.