Get the contact of the owner in react native Expo contacts - react-native

I want to figure out how I can get the contact of the current user with expo contacts for iOS devices. In the iPhone contact book, my contact is indicated with a little "me" on the side. I want to figure out if I can identify this contact with expo contacts.

You can do it with this async function:
const contacts = await Expo.Contacts.getContactsAsync({
fields: [Expo.Contacts.PHONE_NUMBERS, Expo.Contacts.EMAILS],
pageSize: 0,
pageOffset: 0,
sort: sortType
});
sortType can be what you want - for example:
const sortType = Expo.Contacts.SortTypes.LastName

Related

React native agora detect all the users that is currently in the voice call

I am currently using react native agora to create a clubhouse like app and I was wondering is it possible to detect all the users that is currently in the voice call? I'm using the Live broadcasting type for my call.
engine.joinChannel(token,"room_name", null, user)
engine.setChannelProfile(ChannelProfile.LiveBroadcasting);
Any help would be appreciated
I was able to do this by the following lines of code
engine.enableAudioVolumeIndication(200, 3, true)
// res is an array with all users speaker volume
engine.addListener("AudioVolumeIndication", (res) => {
console.log('audio volume indication', res);
});
// return the speaker with highest volume for a given period of time
engine.addListener("ActiveSpeaker", (res) => {
console.log('active speaker', res);
});

React Native Forms Handling on Submit Button

I am new to React Native. I am building an app where there is a contact form in it. I want to collect all the forms field data and want to send these details entered by the users to a specific email on clicking Submit button.
How can I achieve this?
Thanks In advance
Am not sure if you can send email directly from react native app directly.
You can try using react-native-email package. Its quite easy to integrate. The package uses Linking API
import email from 'react-native-email'//imports the package
handleEmail = () => {//function to send email
const to = ['tiaan#email.com', 'foo#bar.com'] // string or array of email addresses
email(to, {
// Optional additional arguments
cc: ['bazzy#moo.com', 'doooo#daaa.com'], // string or array of email addresses
bcc: 'mee#mee.com', // string or array of email addresses
subject: 'Show how to use',
body: 'Some body right here'
}).catch(console.error)
}
}

appleAuthRequestResponse returning null for fullName and email

I am confused why me below snippet of code is showing null for email and fullName in console after user is authenticated successfully. I have read the documentation carefully and tried every possible thing I could. Any help would be highly appreciated.
async function onAppleButtonPress() {
// performs login request
const appleAuthRequestResponse = await appleAuth.performRequest({
requestedOperation: AppleAuthRequestOperation.LOGIN,
requestedScopes: [AppleAuthRequestScope.EMAIL, AppleAuthRequestScope.FULL_NAME],
});
//api getting current state of the user
const credentialState = await appleAuth.getCredentialStateForUser(appleAuthRequestResponse.user);
if (credentialState === AppleAuthCredentialState.AUTHORIZED) {
// user is authenticated
console.log("email is",appleAuthRequestResponse.email);
console.log("full name is",appleAuthRequestResponse.fullName);
}
}
You can still retrieve the e-mail from the identityToken provided by appleAuthrequestResponse with any jwt decoder like jwt-decode
const {identityToken, nonce, email} = appleAuthRequestResponse
const {email} = jwt_decode(identityToken)
console.log(email)
Apple only returns the full name and email on the first login, it will return null on the succeeding login so you need to save those data.
To receive these again, go to your device settings; Settings > Apple ID, iCloud, iTunes & App Store > Password & Security > Apps Using Your Apple ID, tap on your app and tap Stop Using Apple ID. You can now sign-in again and you'll receive the full name and `email.
Source here.

Expo in app purchases for React Native — how to tell if subscription is current / active?

Current app is Expo for React Native which is ejected to the bare workflow.
Using expo-in-app-purchases for IAP.
How can you tell if a subscription is active or not?
When I grab the purchase history via:
const { results } = InAppPurchases.connectAsync();
if you look at the results, a result returns the following fields:
purchaseTime
transactionReceipt
orderId
productId
acknowledged
originalPurchaseTime
originalOrderId
purchaseState
Now purchaseState is always an integer. I'm mostly seeing 3 (I think I've seen a 1 one time...) Not sure this actually tells me anything valuable as they are all 3s
Short of manually taking the most recent purchase and adding 30 days (this is a monthly subscription) then seeing if this date is in the past, I'm not sure how to find if current user has active subscription. Help!
Thanks in advance!
Apple gives you the receipt as a signed and base64-encoded string. In order to see the contents of the receipt (including the 'expires at' date), you need to send this receipt to Apple's receipt verification endpoint along with your app-specific password.
More info here: https://developer.apple.com/documentation/storekit/in-app_purchase/validating_receipts_with_the_app_store
A function similar to this worked for me. This retrieves the receipt info as a JSON object and inside there is expires_at_ms, which can be compared to today's date to see if the subscription has expired.
async validateAppleReceipt(receipt) {
const prodURL = 'https://buy.itunes.apple.com/verifyReceipt'
const stagingURL = 'https://sandbox.itunes.apple.com/verifyReceipt'
const appSecret = '1234...'
const payload = {
"receipt-data": receipt,
"password": appSecret,
"exclude-old-transactions": true,
}
// First, try to validate against production
const prodRes = await axios.post(prodURL, payload)
// If status is 21007, fall back to sandbox
if (prodRes.data && prodRes.data.status === 21007) {
const sandboxRes = await axios.post(stagingURL, payload)
return sandboxRes.data.latest_receipt_info[0]
}
// Otherwise, return the prod data!
return prodRes.data.latest_receipt_info[0]
}
Have you tried to use
const { responseCode, results } = await getPurchaseHistoryAsync(true);

Sorting contacts in alphabetical order in React-Native

Currently I'm working on a mobile app. I need to pull contacts from my phone into the app I'm creating (I use expo contacts). For some reason, instead of contacts being sorted in the alphabetical order, they show up in the random order.
I'm not quite sure how to sort the contacts to display them in the alphabetical order.
Can anyone please take a look and let me know what I'm doing wrong/what I should add?
The code that I use:
// load contacts from phone
_loadContacts = async () => {
try {
await Permissions.askAsync(Permissions.CONTACTS);
const { data: contacts } = await Contacts.getContactsAsync({ fields: [Contacts.Fields.PhoneNumbers] })
// for rendering contacts
_renderContact = ({ item: { firstName, lastName, name, phoneNumbers } }) => (
<View style={styles.contact}>
<Text style={styles.contactName}>{name || [firstName, lastName].join(" ")}</Text>
<Text style={styles.phoneNumbers}>📞 {phoneNumbers ? phoneNumbers.map(entry => entry.number).join(", ") : "undefined"}</Text>
</View>
)
It is possible to sort contacts using Expo. It was added in August 2018. https://expo.canny.io/feature-requests/p/sorting-options-for-contacts
If you check the documentation you can see that getContactsAsync takes a ContactQuery.
ContactQuery (see here) takes a SortType (see here), allowing you to sort the contacts by either FirstName or LastName.
A simple example would be the following.
const { data: contacts } = await Contacts.getContactsAsync({
fields: [Contacts.Fields.PhoneNumbers],
sort: Contacts.SortTypes.FirstName
});
It is worth noting that at the current time, SortType is only available for Android. So for iOS you would need to sort them in the order you want, yourself.