Created
October 27, 2020 10:41
-
-
Save Sunilkumarr/6a71050b172f62a3dcc6fc55dd87dec7 to your computer and use it in GitHub Desktop.
app.js for activate and deactivate callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In NativeScript, the app.js file is the entry point to your application. | |
You can use this file to perform app-level initialization, but the primary | |
purpose of the file is to pass control to the app’s first module. | |
*/ | |
/// START APPLOZIC NativeScript | |
const { Application} = require("@nativescript/core"); | |
const { Utils} = require("@nativescript/core"); | |
let launchListener; | |
let exitListener; | |
var broadcastManager; | |
var broadcastManagerReceivedCallback = undefined; | |
/// END APPLOZIC NativeScript | |
/* | |
Do not place any code after the application has been started as it will not | |
be executed on iOS. | |
*/ | |
/// START APPLOZIC NativeScript | |
launchListener = (args) => { | |
if (Application.android != undefined) { | |
broadcastManager = global.androidx.localbroadcastmanager.content.LocalBroadcastManager.getInstance(Utils.ad.getApplicationContext()); | |
console.log("We are running on Android device!"); | |
const cb = android.content.BroadcastReceiver.extend({ | |
onReceive: (context, data) => { | |
if (data != null) { | |
if (data.getAction() == "USER_ACTIVATED") { | |
console.log("Login user ACTIVATED on Android device!"); | |
} else if (data.getAction() == "USER_DEACTIVATED") { | |
console.log("Login user DEACTIVATED on Android device!"); | |
} | |
} | |
} | |
}); | |
broadcastManagerReceivedCallback = new cb(); | |
broadcastManager.registerReceiver(broadcastManagerReceivedCallback, new android.content.IntentFilter("USER_ACTIVATED")); | |
broadcastManager.registerReceiver(broadcastManagerReceivedCallback, new android.content.IntentFilter("USER_DEACTIVATED")); | |
} else if (Application.ios) { | |
Application.ios.addNotificationObserver("ALLoggedInUserDidChangeDeactivateNotification", (notification) => { | |
const userInfo = notification.userInfo; | |
if (userInfo != null) { | |
if (userInfo.valueForKey("DEACTIVATED") == "true") { | |
console.log("Login USER is Deactivited"); | |
} else{ | |
console.log("Login USER is activited"); | |
} | |
} | |
}); | |
} | |
}; | |
Application.on(Application.launchEvent, launchListener); | |
exitListener = (args) => { | |
if (Application.android != undefined) { | |
if (broadcastManagerReceivedCallback != undefined && broadcastManager != null){ | |
broadcastManager.unregisterReceiver(broadcastManagerReceivedCallback); | |
broadcastManagerReceivedCallback = undefined; | |
} | |
} | |
}; | |
Application.on(Application.exitEvent, exitListener); | |
/// END APPLOZIC NativeScript | |
Application.run({ moduleName: "app-root" }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment