thanks for opening the issue!
I have looked at fixing in your github code and seems I made the same thing but it still gives me this error. If you have any idea why, I would be thankful! It works in useEffects, try catch block
Code:
function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
/[xy]/g,
function (c) {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
}
);
}
async function copyAssetToAppDocs(): Promise<string> {
const uuid = generateUUID();
const asset = Asset.fromModule(
require('../../assets/images-notifications/image0.png')
);
await asset.downloadAsync();
if (!asset.localUri) {
console.error('Asset localUri is missing');
throw new Error('Asset localUri is missing');
}
console.log('Asset localUri:', asset.localUri);
console.log(asset.type, 'asset.type');
const docsDir = FileSystem.documentDirectory;
if (!docsDir) {
console.error('Document directory is missing');
throw new Error('Document directory is missing');
}
const targetPath = `${docsDir}${uuid}.png`; // random unique file
console.log(targetPath, 'targetPath');
await FileSystem.copyAsync({
from: asset.localUri.startsWith('file://')
? asset.localUri
: `file://${asset.localUri}`,
to: targetPath,
});
const fileInfo = await FileSystem.getInfoAsync(targetPath);
if (fileInfo.exists) {
console.log('File exists, using existing path:', targetPath);
}
console.log('File copied to:', targetPath);
return targetPath.substring('file://'.length);
}
const attachmentUrl = await copyAssetToAppDocs();
if (!attachmentUrl) {
console.error('Failed to copy asset to app docs');
return;
}
console.log(attachmentUrl, 'attachmentUrl');
setImageurl(attachmentUrl);
console.log(imgurl, 'imgurl');
await Notifications.scheduleNotificationAsync({
content: {
title: 'Finish your profile',
body: 'Ready to find your perfect match? Complete your profile now and start your journey to love!',
attachments: [
{
identifier: 'lalala',
url: attachmentUrl,
type: 'image/png',
typeHint: 'public.png',
hideThumbnail: false,
},
],
data: { route: 'imageScreen' },
},
trigger: {
type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL,
seconds: 5,
},
});
console.log('Notification scheduled');