Not working from cold app launch
Chippd opened this issue ยท 6 comments
Hi, firstly thank you for your plugin - it's a lifesaver for a side project I'm working on. I bought you a few coffees ๐
I may be missing something, but I have things working fine when the app is already open in the background, but from a cold start I'm not catching the sendIntentReceived event.
I'm using Nuxt3 and have put my event listener in app.vue which I believe is one of, if not the first page to load and it's not catching the event.
Is there something I'm missing perhaps?
Many thanks ๐
Hey, that is quite some coffee! Many thanks! ๐
Just to clarify: are you having this issue on Android or on iOS? On Android, SendIntent doesn't use the event at all (anymore). If you review the AndroidManifest.xml configuration in Readme, you will see that the intent is configured in a separate activity, which makes the event obsolete. I decided to do so, because using the MainActivity led to state problems in some cases. It also aligns better with the iOS behaviour, which uses a separate instance as well. If you got problems on iOS, let me know and I will do some investigations.
Cheers!
Hey, that is quite some coffee! Many thanks! ๐
My pleasure, more than happy to contribute something to a solution that works this well.
are you having this issue on Android or on iOS?
Apologies I meant to say, this is iOS. I fear it's something I'm not doing correctly but any direction you could provide would be amazing.
Pasting my dependencies below in case they provide a clue.
"dependencies": {
"@capacitor-firebase/app": "^0.5.0",
"@capacitor-firebase/authentication": "^0.5.0",
"@capacitor-firebase/messaging": "^0.5.0",
"@capacitor/cli": "^3.5.1",
"@capacitor/core": "^3.5.1",
"@capacitor/filesystem": "^1.1.0",
"@capacitor/ios": "^3.5.1",
"@capacitor/push-notifications": "^1.0.9",
"@capacitor/storage": "^1.2.5",
"@tauri-apps/api": "^1.0.1",
"date-fns": "^2.28.0",
"firebase": "^9.8.4",
"firebase-admin": "^10.3.0",
"lodash": "^4.17.21",
"nuxt-socket-io": "^3.0.6",
"posthog-js": "^1.24.0",
"sass": "^1.51.0",
"sass-loader": "^12.6.0",
"send-intent": "^3.0.11",
"socket.io": "^4.5.1",
"socket.io-client": "^4.5.1"
}
Thank you again ๐
Hmm, I've actually found a (probably hacky) workaround.
in my AppDelegate.swift, I'm delaying the triggering of the notification. I guess it just gives the app enough time to open and register the listener. Perhaps there's a better way or this is a sign that I'm doing something wrong?
...
let secondsToDelay = 1.0
DispatchQueue.main.asyncAfter(deadline: .now() + secondsToDelay) {
nc.post(name: Notification.Name("triggerSendIntent"), object: nil )
}
...
I think you explained it correctly. The event probably didn't get triggered because the listener wasn't there yet. I myself do a combination of a regular call on startup (which you will need for Android) and a listener (which is necessary on iOS). It looks like this:
useEffect(() => {
window.addEventListener("sendIntentReceived", () => {
checkIntent();
});
checkIntent();
}, [])
async function checkIntent() {
SendIntent.checkSendIntentReceived().then((result: any) => (async function (result: any) {
....
}
}
I wasn't aware that checking on startup has a case on iOS as well, so probably the Android way just got me going by accident. Please try this and let me know if this works for you! I will then try to point that out clearer in the Readme.
Ah, I didn't realise you could checkIntentReceived outside of the listener but of course you can ๐คฆ๐ปโโ๏ธ
I've adapted your example above to my Nuxt3 use case and it looks to have solved my problem, thank you for the quick responses and thanks again for your plugin. Great work. ๐
For anyone (including my future self), here's my code (Nuxt3 specific).
I created a file called sendIntentPlugin.client.ts
in my /plugins directory.
import { SendIntent } from "send-intent";
import { Capacitor } from "@capacitor/core";
export default defineNuxtPlugin(async () => {
const intentData = useSendIntent() // using shared state to share this intent data to other components
async function checkIntent() {
console.log("Checking Intent")
try {
let result = await SendIntent.checkSendIntentReceived();
if (result) {
console.log("SendIntent found", JSON.stringify(result));
}
if (result && result.url) {
let resultUrl = decodeURIComponent(result.url);
console.log("resultUrl:", resultUrl);
intentData.value = {
url: resultUrl
}
// I'M NOT HANDLING FILES JUST YET, BUT HERE'S WHERE WE'D DO THAT
// Filesystem.readFile({path: resultUrl})
// .then((content) => {
// console.log(content.data);
// })
// .catch((err) => console.error(err));
}
} catch (error) {
console.log("Error handing sendIntent:", error);
}
}
if (Capacitor.isNativePlatform()) {
checkIntent()
window.addEventListener("sendIntentReceived", async () => {
console.log('sendIntent received');
checkIntent()
});
}
});
You're welcome. Thanks for your code, looks good! I slightly edited the iOS manual with a link to this discussion and therefore will close this issue.