Skip to content

Instantly share code, notes, and snippets.

@Sunilkumarr
Created November 30, 2020 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sunilkumarr/6d5628e580012738e6c68918ee83f888 to your computer and use it in GitHub Desktop.
Save Sunilkumarr/6d5628e580012738e6c68918ee83f888 to your computer and use it in GitHub Desktop.
Notification observer for iOS and local broadcast for android. in native script
/*
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.
*/
/*
Do not place any code after the application has been started as it will not
be executed on iOS.
*/
/*
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
import * as utils from "tns-core-modules/utils/utils";
const application = require("tns-core-modules/application");
var launchListener;
var 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) {
console.log("We are running on Android iOS @");
application.ios.addNotificationObserver("ALLoggedInUserDidChangeDeactivateNotification", (notification) => {
const userInfo = notification.userInfo;
console.log("ALLoggedInUserDidChangeDeactivateNotification @");
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
/// NOTE: Make sure to put the code above the application.run else method wont execute in IOS
application.run({ moduleName: "app-root" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment