1.0.0-beta.1 • Published 1 year ago

expo-intent-receiver v1.0.0-beta.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

expo-intent-receiver

Expo module to receive Android intents. Supports only expo Android.

npm version License: MIT React-Native Expo

Installation

Add the package to your npm dependencies

npm install expo-intent-receiver

Configure Android Intent Filters in app.json

//app.json
{
    "android": {      
        "intentFilters": [
            {
                "action": "SEND_MULTIPLE",
                "category": [
                    "DEFAULT"
                ],
                "data": [
                    {
                    "mimeType": "image/*"
                    }
                ]
            },
            {
                "action": "SEND",
                "category": [
                    "DEFAULT"
                ],
                "data": [
                    {
                    "mimeType": "video/*"
                    }
                ]
            }        
        ]
    }
}

Usage

import React from 'react';
import { View, Text } from 'react-native';
import * as ExpoIntentReceiver from 'expo-intent-receiver';

export default function App() {
  const refIntent = React.useRef(ExpoIntentReceiver.getInitialIntent());
  const [data, setData] = React.useState<ExpoIntentReceiver.IntentInfo[]>([]);
  
  React.useEffect(() => {
    const subscription = ExpoIntentReceiver.addChangeListener(({ data }) => {
      setData((currentData) => [...currentData, ...data])
    })
    return () => subscription.remove();
  }, []);

  return (
    <View>
      <Text>Initial intent: {JSON.stringify(refIntent)}</Text>
      {
        data.map((d, i) => 
            <Text 
                key={`item-${i}`}>
                    {i} - {JSON.stringify(d)}
            </Text>
        )
      }
    </View>
  );
}

Module

MethodReturnDescription
addIntentListenerIntentReceiverPayloadWill fire when new filtered intent is received and app is in the background.
getInitialIntentIntentInfo[]Will return ÌntentInfo[] if filtered intent is available. To be used to capture filtered intent that launched the app.
clearIntentvoidClear intent.

Types

type IntentInfo = {
  contentUri: string;
  extension: string;
  fileName: string;
  mimeType: string;
}

type IntentReceiverPayload = {
  data : IntentInfo[];
}

Contributing

Contributions are very welcome!