Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch mail app with react-native [Android]

Is there a way to launch mail app using Linking on Android. The point is the scheme (Deep link) mesage: is working only on iOS.

Here is a little example which work on iOS but not on Android:

Linking.canOpenURL('message:0').then(supported => {
  if (!supported) {
    console.log('Can\'t handle url');
  } else {
    return Linking.openURL('message:0');
  }
});

Many posts official/non official talks about an activity Intent or about the scheme mailto: but I don't want to write an email. I would like to open the mail app than the user could check the email I sent him.

By the way, I'm using react-native.

like image 533
Liroo Pierre Avatar asked Dec 19 '22 07:12

Liroo Pierre


1 Answers

I resolved this problem with a Native Module in RN

Firstly the cross platform code in JS to open the mailbox:

openMailApp() {
    if (Platform.OS === 'android') {
      NativeModules.UIMailLauncher.launchMailApp(); // UIMailLauncher is the 
      return;
    }
    Linking.openURL('message:0'); // iOS
    return;
  }

Linking is a cross platform provided by React-Native. Anyway, the URL message:0 did not work on Android. The only solution that I found is to create an intern wrapper in my app and to create a ReactMethod in Java.

  @ReactMethod
  public void launchMailApp() {
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_APP_EMAIL);
      getCurrentActivity().startActivity(intent);
  }

If you already developed native code using React-Native framework, this is a basic ReactMethod where

  private static final String REACT_MODULE = "UIMailLauncher";

  @Override
  public String getName() {
      return REACT_MODULE;
  }
like image 149
Liroo Pierre Avatar answered Jan 14 '23 19:01

Liroo Pierre