2017-05-27 40 views
2

有沒有辦法在Android上使用Linking啓動郵件應用程序。 關鍵是方案(深層鏈接)mesage:只適用於iOS。使用react-native啓動郵件應用程序[Android]

這裏是一個在iOS而非Android上運行一個小例子:

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

很多帖子官方/非官方談判有關的活動意圖或有關該計劃mailto:但我不想寫電子郵件。我想打開郵件應用程序比用戶可以檢查我發給他的電子郵件。

順便說一下,我正在使用react-native。

+0

因爲消息不是方案。即使是這樣 - 爲什麼它會打開電子郵件?通訊應用程序和電子郵件應用程序通常不是同一件事。或者嘗試使用mailto :,或使用電子郵件MIME類型,如https://stackoverflow.com/questions/3312438/how-to-open-email-program-via-intents-but-only-an-email -program –

+1

深度鏈接和magi鏈接是兩個截然不同的東西,這兩者都不是你想要做的。 –

+0

我正在使用React Native,我無法使用Intent API,只能通過反應原生提供的鏈接API。 而'message'是iOS上的一個方案......並且它打開郵件iOS應用程序 –

回答

2

我與本機模塊在RN

解決了這個問題首先在JS跨平臺的代碼來打開郵箱:

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

鏈接是通過陣營天然提供了一種跨平臺。無論如何,URL message:0在Android上無法使用。 我發現的唯一解決方案是在我的應用程序中創建一個實習包裝並在Java中創建ReactMethod。

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

如果您已經使用了陣營,本地框架開發的本機代碼,這是一個基本ReactMethod其中

private static final String REACT_MODULE = "UIMailLauncher"; 

    @Override 
    public String getName() { 
     return REACT_MODULE; 
    } 
2

要發射的意圖,你可以使用Linking.openURL API:

import { Linking } from 'react-native' 
Linking.openURL("mailto:[email protected]"); 

這工作,因爲從文檔:

任何可以用Intencode打開的URL t.ACTION_VIEW}。

+0

這打開郵件撰寫我想從我的應用程序發送電子郵件,並添加從我的應用程序打開郵件應用程序,用戶可以檢查他們的電子郵件;)我爲這項工作做了一個本地模塊,它工作得很好 –

+1

這很酷@LirooPierre - 可否請你分享你的本地模塊。 – ystal

相關問題