Created
November 30, 2020 18:22
-
-
Save Sunilkumarr/6d5628e580012738e6c68918ee83f888 to your computer and use it in GitHub Desktop.
Notification observer for iOS and local broadcast for android. in native script
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. | |
*/ | |
/* | |
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