$ npm install react-native-android-broadcast-receiver --save
$ react-native link react-native-android-broadcast-receiver
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.androidbroadcastreceiverforreferrer.RNAndroidBroadcastReceiverForReferrerPackage;
to the imports at the top of the file - Add
new RNAndroidBroadcastReceiverForReferrerPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-android-broadcast-receiver-for-referrer' project(':react-native-android-broadcast-receiver-for-referrer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-broadcast-receiver-for-referrer/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-android-broadcast-receiver-for-referrer')
- Insert the following lines inside the AndroidManifest.xml in
android/app/src/main/AndroidManifest.xml
:<manifest ....> ..... <application ...> ..... <receiver android:name="com.androidbroadcastreceiverforreferrer.ReferrerBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED"/> </intent-filter> </receiver> </application> </manifest>
import { DeviceEventEmitter } from "react-native";
//Add it in componentWillMount or somewhere where it will get executed at the start of app
DeviceEventEmitter.addListener('GReferrerBroadcastReceiver', function (map) {
console.log('Google Broadcast referrer data is: ' + map.referrer);
});
//Do not forget to remove the listener at componentWillUnmount
componentWillUnmount() {
DeviceEventEmitter.removeListener('GReferrerBroadcastReceiver');
}
//You can also get the referrer which is stored in the local variable by
import RNAndroidBroadcastReceiverForReferrer from 'react-native-android-broadcast-receiver-for-referrer';
//This will return the referrer value if we have got it other will return "NOT AVAILABLE"
let referrerValue = await RNAndroidBroadcastReceiverForReferrer.getReferrerData();