I have wrapped my PWA into an iOS native app with PWABuilder, and I have integrated Firebase messaging with both the Anvil app and iOS wrapper, and enabled push notification functionality in the iOS wrapper. I’ve been able to successfully get native push notifications working on the iOS app, even when the app is offline, by using a JavaScript “bridge” between the native app and Anvil (using postMessage to communicate between the webview and the iOS wrapper and store the device token with my users database).
However when I launch the app for the first time on my iOS device, I get an uncaught error in Anvil with no indication of the source (usually the Anvil logs show you the line in your code that is causing the error). The error is: : SyntaxError: Unexpected identifier 'GET'. Expected '}' to end an object literal.
It seems to occur when an error in the iOS app arises, namely Firebase not getting a valid APNS token before the call to generate a FCM token is made:
Firebase registration token: Optional("xxxxmytokenxxxxx")
Error fetching FCM registration token: Error Domain=com.google.fcm Code=505 "No APNS token specified before fetching FCM Token" UserInfo={NSLocalizedFailureReason=No APNS token specified before fetching FCM Token}
11.1.0 - [FirebaseMessaging][I-FCM002022] APNS device token not set before retrieving FCM Token for Sender ID '123456789'.Be sure to re-retrieve the FCM token once the APNS device token is set.
11.1.0 - [FirebaseMessaging][I-FCM002022] Declining request for FCM Token since no APNS Token specified
In a moment, a valid token is obtained, and the app can be used with the push notifications, but the big red error still appears on user’s screen. It does not occur on subsequent loads of the app, unless it is uninstalled/reinstalled. I have no idea how to handle it gracefully because I don’t know where it’s coming from. I’ve even commented out the JS addEventListeners but it still occurs. It seems to arise quite early in my app loading process, even before my various form_load print statements fire.
Does anyone have suggestions how to isolate the source of the error and catch it gracefully?
The syntax error is likely the result of a javascript script being injected into your app thats malformed.
I would check any script tags you’re using in native libraries, or custom html forms.
Commenting out script tags one by one may give you some information about which one is the culprit.
Are you able to catch this error in a default error handler?
from anvil.js import ExternalError
import anvil
def handle_error(e):
if isinstance(e, ExternalError):
print(repr(e))
original = e.original_error
msg = getattr(original, "message", None)
stack = getattr(original, "stack", None)
print(original, msg, stack)
# raise e # if you want to pass it to anvil's default error handler
else:
raise e
anvil.set_default_error_handling(handle_error)
Being able to see the browser console output is particularly useful. If you can connect your iPhone to a mac, you may be able to see the developer console through safari’s developer tools.
I tried implementing this default error handler in my app, in the startup module and in the first form that’s shown, but the red error popup still occurs and the default error handler doesn’t appear to get called.
I had a look at my native libraries but I couldn’t find anything that might be causing this.
When connecting the iPhone to a Mac and then opening up Safari, it only shows console output in certain situations, like when running a Home Screen Web App, but not when it’s a native app even if it’s using a webview. I think the only diagnostic data I have is the output from xCode logs and what’s shown in Anvil.
For anyone wondering, @stucork solved it with the following:
set the error handler before the import statements
# startup module
import anvil
def handle_error(e):
...
# do this before we import anything
# incase importing a module is causing the error
anvil.set_default_error_handling(handle_error)
print("default error handler set")
import anvil.server
... # rest of imports
# rest of module
I’m curious about the AppOfflineError s. Shouldn’t they be handled by the default error handler as well?
Oftentimes, when I get a native push notification to my iOS app (which then triggers an event-listener for push-notification in my PWA), I get AppOfflineError: Connection to server failed (error) with the big red popup.
Edit: Yep - looks like adding the try-except block around my on_push_notification function prevented the red Error popup from occurring.
Shouldn’t that be caught by the default event handler?
And why does the AppOfflineError occur often (but not always) during push notification events? In some cases, I run the app, then navigate away to my home screen, and in 5 seconds I get the notification, but still get the AppOfflineError after clicking on it and bringing the app back into focus.