Sunday, July 17, 2022

Android 12 Notification Permission

Notification permissions for apps targeting Android 12L (API level 32) or lower

Android automatically asks the user for permission the first time your app creates a notification channel, as long as the app is in the foreground. However, there are important caveats regarding the timing of channel creation and permission requests:


If your app creates its first notification channel when it is running in the background (which the FCM SDK does when receiving an FCM notification), Android will not allow the notification to be displayed and will not prompt the user for the notification permission until the next time your app is opened. This means that any notifications received before your app is opened and the user accepts the permission will be lost.

We strongly recommend that you update your app to target Android 13+ to take advantage of the platform’s APIs to request permission. If that is not possible, your app should create notification channels before you send any notifications to the app in order to trigger the notification permission dialog and ensure no notifications are lost. See notification permission best practices for more information. 

references:

https://firebase.google.com/docs/cloud-messaging/android/client

Android 13 - Notification Permissions

// Declare the launcher at the top of your Activity/Fragment:
private final ActivityResultLauncher<String> requestPermissionLauncher =
        registerForActivityResult
(new ActivityResultContracts.RequestPermission(), isGranted -> {
           
if (isGranted) {
               
// FCM SDK (and your app) can post notifications.
           
} else {
               
// TODO: Inform user that that your app will not show notifications.
           
}
       
});

// ...
private void askNotificationPermission() {
   
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
           
PackageManager.PERMISSION_GRANTED) {
       
// FCM SDK (and your app) can post notifications.
   
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
       
// TODO: display an educational UI explaining to the user the features that will be enabled
       
//       by them granting the POST_NOTIFICATION permission. This UI should provide the user
       
//       "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
       
//       If the user selects "No thanks," allow the user to continue without notifications.
   
} else {
       
// Directly ask for the permission
        requestPermissionLauncher
.launch(Manifest.permission.POST_NOTIFICATIONS);
   
}
}

 https://firebase.google.com/docs/cloud-messaging/android/client

Sunday, June 19, 2022

String concat() vs concat operators

 MDN has the following to say about string.concat():

It is strongly recommended to use the string concatenation operators (+, +=) instead of this method for perfomance reasons


Also see the link by @Bergi.


references:

https://stackoverflow.com/questions/16124032/js-strings-vs-concat-method


Pod Install on Apple M1 Chip

# Uninstall the local cocoapods gem
sudo gem uninstall cocoapods

# Reinstall cocoapods via Homebrew
brew install cocoapods

 references:

https://stackoverflow.com/questions/64901180/how-to-run-cocoapods-on-apple-silicon-m1

Installing MongoDB on Apple Mac M1 Pro

 Article on how to install mongodb on apple m1 using homebrew

Install homebrew from https://brew.sh/

Install xcode command line using

xcode-select --install

Now to install mongodb use

brew tap mongodb/brew

brew install mongodb-community@5.0

To check if mongodb has been installed use

mongo --version

to start mongoDB as macOS service use

brew services start mongodb-community@5.0

and to stop mongoDB to run as a background service use

brew services stop mongodb-community@5.0

Or, if you don't want/need a background service you can just run:

mongod --config /opt/homebrew/etc/mongod.conf

To run mongodb commands, open a new table and run mongo

To check your databases run show dbs

Full documentation here: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

references:

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

https://stackoverflow.com/questions/65357744/how-to-install-mongodb-on-apple-m1-chip

Sunday, June 12, 2022

AI/ML: Types of ensemble methods

BAGGing, or Bootstrap AGGregating. BAGGing gets its name because it combines Bootstrapping and Aggregation to form one ensemble model. Given a sample of data, multiple bootstrapped subsamples are pulled. A Decision Tree is formed on each of the bootstrapped subsamples. After each subsample Decision Tree has been formed, an algorithm is used to aggregate over the Decision Trees to form the most efficient predictor. The image below will help explain:


Random Forest Models. Random Forest Models can be thought of as BAGGing, with a slight tweak. When deciding where to split and how to make decisions, BAGGed Decision Trees have the full disposal of features to choose from. Therefore, although the bootstrapped samples may be slightly different, the data is largely going to break off at the same features throughout each model. In contrary, Random Forest models decide where to split based on a random selection of features. Rather than splitting at similar features at each node throughout, Random Forest models implement a level of differentiation because each tree will split based on different features. This level of differentiation provides a greater ensemble to aggregate over, ergo producing a more accurate predictor. Refer to the image for a better understanding.


Similar to BAGGing, bootstrapped subsamples are pulled from a larger dataset. A decision tree is formed on each subsample. HOWEVER, the decision tree is split on different features (in this diagram the features are represented by shapes).


References:

https://towardsdatascience.com/ensemble-methods-in-machine-learning-what-are-they-and-why-use-them-68ec3f9fef5f


AI/ML: Ensemble methods in machine learning

Ensemble methods is a machine learning technique that combines several base models in order to produce one optimal predictive model

A Decision Tree determines the predictive value based on series of questions and conditions. For instance, this simple Decision Tree determining on whether an individual should play outside or not. The tree takes several weather factors into account, and given each factor either makes a decision or asks another question.

When making Decision Trees, there are several factors we must take into consideration: On what features do we make our decisions on? What is the threshold for classifying each question into a yes or no answer? In the first Decision Tree, what if we wanted to ask ourselves if we had friends to play with or not. If we have friends, we will play every time. If not, we might continue to ask ourselves questions about the weather. By adding an additional question, we hope to greater define the Yes and No classes.

This is where Ensemble Methods come in handy! Rather than just relying on one Decision Tree and hoping we made the right decision at each split, Ensemble Methods allow us to take a sample of Decision Trees into account, calculate which features to use or questions to ask at each split, and make a final predictor based on the aggregated results of the sampled Decision Trees.

References:

https://towardsdatascience.com/ensemble-methods-in-machine-learning-what-are-they-and-why-use-them-68ec3f9fef5f