4

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.

Liroo Pierre
  • 875
  • 2
  • 9
  • 25
  • Because message isn't a scheme. And even if it was- why would it open email? messaging apps and email apps are not usually the same thing. Either try a url with scheme mailto: , or use the email MIME type like in https://stackoverflow.com/questions/3312438/how-to-open-email-program-via-intents-but-only-an-email-program – Gabe Sechan May 27 '17 at 20:50
  • 1
    Also deep link and magi link are two distinct things, neither of which are what you're trying to do. – Gabe Sechan May 27 '17 at 20:51
  • I'm using React Native, I cannot use Intent API, only Linking API provided by react native. And `message` is a scheme on iOS... And it open the mail iOS application – Liroo Pierre May 27 '17 at 21:12
  • You can always drop from ReactNative into Native if it makes something easier. That's pretty much a requirement to make anything non-trivial in react native work. – Gabe Sechan May 28 '17 at 04:35
  • Yes, that's what I did, I made a native module just for Android, thanks for your previous answers ;) – Liroo Pierre May 28 '17 at 09:14

4 Answers4

10

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;
  }
Liroo Pierre
  • 875
  • 2
  • 9
  • 25
  • 1
    Thanks for sharing the useful info. I need `intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` on Java code. Email app launch in same activity of current app without that,. – asukiaaa Jul 19 '18 at 00:26
  • Based on this post I created an npm package that does this for you: https://github.com/verypossible/react-native-mail-launcher – neurosnap Oct 30 '18 at 16:21
1

https://github.com/verypossible/react-native-mail-launcher

This native-module will open the mail app

import * as React from "react";
import { View, Button } from "react-native";
import launchMailApp from "react-native-mail-launcher";

export default class Example extends React.Component {
  render() {
    return (
      <View>
        <Button onPress={launchMailApp}>Go to mail client</Button>
      </View>
    );
  }
}
neurosnap
  • 5,658
  • 4
  • 26
  • 30
0

To fire intents you can use the Linking.openURL API:

import { Linking } from 'react-native'
Linking.openURL("mailto:?to=blah@hotmail.com");

This works because from the docs:

any URL that can be opened with {@code Intent.ACTION_VIEW}.

ystal
  • 648
  • 5
  • 14
  • This open a mail compose where I wanted to send an email from my app and add the ability to open the mail app from my app that the user could check their emails ;) I made a native module for this job and it works pretty well – Liroo Pierre May 28 '17 at 09:16
  • 1
    That is very cool @LirooPierre - may you please share your native module. – ystal May 28 '17 at 10:06
-1

Try a "mailto" intent but with Intent.ACTION_VIEW instead of send:

http://www.gaanza.com/blog/email-client-intent-android/comment-page-1/

James Poag
  • 2,320
  • 1
  • 13
  • 20
  • I'm on React Native, I would like to use the `Linking` API, I already think about the `Intent.ACTION_VIEW` but this is no possible from `Linking` API – Liroo Pierre May 27 '17 at 21:11