How to query PWA offline state to inform user - angular-service-worker

I try to make a angular 8 PWA app. So far so good, The cache is working in offline mode both for static resources and APIs.
But I'd like to inform the user about the offline state and let them know that the information displayed (API) is not necessarily up to date.
I tried to find a guide or doc about querying the service worker from angular, but found nothing. What is the best way - if any - to know if the service worker uses cache?
Thx.

Browser generates events 'offline' and 'online' depending on the network connectivity, you could listen on this event to inform the user:
window.addEventListener('online', hideOfflineIndicator);
window.addEventListener('offline', showOfflineIndicator);
function showOfflineIndicator() {
// Your code to inform the user they are offline.
}
function hideOfflineIndicator() {
// Your code to inform the user they are back online.
}
This has good browser support
More information on these events can be found here

Related

QuickFIX/J - how to handle End Of Stream

I am new to QuickFIX/J.
Creating initiator using websocket(frontend-angular, backend-Springboot websocket). Using w.3.
I would like to handle session expired issue. When the FIX server session is expired, it is sending Logout with reason session deactivated. That works fine for already connected connection/session.
Now, after this, trying to initiate connection, it keeps calling back logout and toAdmin with repeating event/error on screen log (as event) "Disconnecting, End Of Stream encountered".
I would like to handle this scenario and want to capture this in my code so that proper message to UI will be sent.
I don't know which QuickFIX/J version you are using, but on versions up to 2.2.0 you could implement the quickfix.SessionStateListener in your application and utilize its onDisconnect() callback.
If you are even using version 2.3.0 you could also use onConnectException() which will handle some additional scenarios that the first callback does not cover.

How to detect result from authorization dialog Core Motion?

I'm working on an app that utilizes Core Motion. During the initial onboarding of the app, it asks the user for permission, and we basically want to update the UI based on the response of that popup (Allowed/Denied). Where for notifications and location services this seems easy to do, it doesn't like a request permission API exists for Core Motion, instead it just triggers the popup when starting updating on a manager like we do now:
let motionManager = CMMotionActivityManager()
motionManager.startActivityUpdates(to: OperationQueue.main) {
// do stuff
}
Ideally I want to be able to detect a change in CMMotionActivityManager.authorizationStatus(), but so far haven't been able to come up with a working solution other than implement a timer that checks this property, which I don't feel is a particularly nice solution.
I tried making either authorizationStatus() or the manager as a whole an observable using Combine but that doesn't seem to trigger any updates.
If the user has denied permission, she cannot then grant permission without going to the Settings app. So you don't need to check on a timer. It should suffice to check at app launch and on notifications applicationWillEnterForeground and didBecomeActiveNotification.

Upload data in the background depending on the connectivity

I am having problems with understanding how to approach this problem as I am really new to xamarin and android both.
My problem is the following: I need to develop an android app, which takes photos of things and uploads them to a rest server. The confusing part is that the users have really poor connection, so I need to check the internet connection constantly and try to upload the photos when the internet is there. This check needs to be done in the background so the user wont notice any lag.
When the user clicks on "save" the app should save the photo and metadata to a local database or file (json or sqlite) and upload them when the internet is there.
I have tried many different approaches but failed due to lack of understanding how android threading and services work. (Bound service, Foregroundservice, SQLite)
As I dont have much time to research and test all the posibilities, I am asking you guys: How can I do this ?
Thanks for your understanding.
There is one nuget plugin Xam.Plugin.Connectivity for check internet continuously, and you can manage your code in event of this plugin.
For background, you need to create one background thread and you can call it from ConnectivityChanged event.
CrossConnectivity.Current.ConnectivityChanged += (object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e) =>
{
if(e.IsConnected){
//your code here for fetching data when internet connected.
//Create task for background process
Task.Run(() =>
{
//your code for background
}).ConfigureAwait(false);
}
}

Token expiration in Twilio

I'm working on embedding a soft phone into a web page that will go into Odoo (web based ERP system). It will allow inbound and outbound calls for employees.
The token expires every hour. So this means the user will have to refresh the page every hour. I could do an http refresh but if the user is on a call when it does the refresh it will knock them off the call.
How do we get around this so we can build a fully working dialer?
Twilio evangelist here.
I'd suggest using JavaScript to do an asynchronous HTTP request to get a new token from your server and then updating the instance of client with it.
Hope that helps.
Another Twilio evangelist here!
You can actually listen for the offline event on the Twilio.Device object. From the documentation:
.offline( handler(device) )
Register a handler function to be called when the offline event is
fired. This is triggered when the connection to Twilio drops or the
device's capability token is invalid/expired. In either of these
scenarios, the device cannot receive incoming connections or make
outgoing connections. If the token expires during an active connection
the offline event handler will be called, but the connection will not
be terminated. In this situation you will have to call
Twilio.Device.setup() with a valid token before attempting or
receiving the next connection.
So you want something like:
Twilio.Device.offline(function(device) {
fetchTokenFromServer(function(token) {
device.setup(token);
});
});
where fetchTokenFromServer makes the HTTP request that Devin suggested in his answer.
Let me know if this helps.
I just ran into this issue so hopefully my solution can help you and others.
I was using twilio.js v1.3 and tried implementing my offline callback like #philnash recommended, but kept getting the error device.setup is not a function. I then tried using Twilio.Device.setup(newToken) and was able to get the capability token refreshed but also ended up getting a new error: Cannot read property 'setToken' of undefined.
I ended up having to use twilio.js v1.4 to make the error go away. My working solution looks like this:
Twilio.Device.offline(function(device) {
$.ajax(urlToGetNewToken, type: 'get').done(function(newToken) {
Twilio.Device.setup(newToken)
})
})

How does an iOS device identify itself to server without user log in

I am writing an iOS app with a Rails API backend. The Rails backend will serve JSON data to the app. I have the following requirements.
The app will be a free download
The app will show data on a map
The app will show data in the vicinity of the user
Upon loading the app the device should send some unique identifier to the server identifying itself as a device that is running this app.
There will be no authentication for the user as it is not required. The data is available to anyone who downloads the app. All the server needs to know is that the client is a device running the app. The server cannot serve data to any other client
I would like to run the data using SSL between the device and server
The user location will be sent to the server and the server returns the corresponding pieces of data that are in the vicinity of the user
The client receives the JSON and caches the data locally.
Question: Given these requirements, how to set up steps 4 & 5?
Also: If I want to search more on this topic what keywords should I be googling for?
Consider using OpenUDID or SecureUDID.
I give you 2 options.
First of all, the easy way. From some time, apple forbids access to the device ID. However, they give you a device token instead.
To get this unique token, the user must register for remote notification.
Upon application launching, call the following function:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
Then this callback will be called:
- (void)application:didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken.
Send the token to your server and you're done. Problems with this approach are obvious. Your user will have to register for remote notification.
Another approach is to use the MAC address of the wi-fi board.
To do this:
IPAddress.h
IPAddress.c
Import this files into your project.
Then use this function:
InitAddresses();
GetHWAddresses();
for (int i=0; i<MAXADDRS; ++i)
{
//There is a way you can obtain more info about the hw_addrs, but in general, it's the first.
NSLog(#"MAC: %s", hw_addrs[i]);
}
FreeAddresses();
Create a hash using the mac address above and you're done.
Hope it helps.
Upon first launch, the app sends a request to the server saying Hi, I'm a new client, give me an id! The server generates a new, random id and sends it back. The app saves the id locally and uses it henceforth to uniquely identify itself.

Resources