expo-task-manager with expo-location error - expo

Please provide the following:
SDK Version: "expo": "^45.0.0",
IOS Emulator (atm) but all normally.
expo-task-manager & expo-location
Hello, We are setting up background location tracking in our App...
I am defining my background location task before is loaded
BackgroundLocation.ts
export const LOCATION_TASK_NAME = 'background-location-task'
function init() {
TaskManager.defineTask(LOCATION_TASK_NAME, _execute)
setTimeout(() => {
TaskManager.getRegisteredTasksAsync().then((tasks) =>
console.log('registered tasks', tasks) // logs empty array
)
}, 5000)
}
export default { init }
App.tsx
import BackgroundLocation from '../../......'
BackgroundLocation.init()
export default App() { ... }
Now in the App component hiararchy i have a Component called <LocationTracker />
import { LOCATION_TASK_NAME } from '../../BackgroundLocation'
...
React.useEffect(() => {
async function handleLocation() {
try {
if (backgroundLocationServicesEnabled) {
await Location.startLocationUpdatesAsync(
LOCATION_TASK_NAME,
{
accuracy: Location.Accuracy.Balanced,
distanceInterval: 20,
}
)
} else {
await Location.stopLocationUpdatesAsync(
LOCATION_TASK_NAME
).catch((e) => {
console.error(
'error calling stopLocationUpdatesAsync',
e
)
})
}
} catch (e) {
console.error('error in handleLocation', e)
}
}
handleLocation().then(() => {})
}, [backgroundServicesEnabled])
Background location is not enabled in my case so the error being fired is
error calling stopLocationUpdatesAsync, [Error: Task 'background-location-task' not found for app ID 'mainApplication'.]
at components/LocationTracker.tsx:70:37 in Location.stopLocationUpdatesAsync._catch$argument_0
its this part
[Error: Task 'background-location-task' not found for app ID 'mainApplication'.]
that is causing me confusion because i'm registering that task ID right at the begining of the app.
OP over here https://forums.expo.dev/t/expo-task-manager-with-expo-location-error/68515

Related

Cannot log after tests are done in jestjs

I have written test cases for signin API using jest. After completing all five test of a test suit jest give me following error in log.
Can any body tell Why it is So and how to fix it?
CODE:(signup.test.ts)
import request from 'supertest';
import { TYPES } from '../src/inversify.types'
import { Application } from '../src/app/Application'
import { container } from '../src/inversify.config'
import dotenv from 'dotenv'
import { RESPONSE_CODE } from '../src/utils/enums/ResponseCode'
import { RESPONSE_MESSAGES } from '../src/utils/enums/ResponseMessages'
import { UserSchema } from '../src/components/user/User';
// import jwt from 'jsonwebtoken';
var application: Application
describe("POST / - SIGNUP endpoint", () => {
// var testusers: any;
//This hook is executed before running all test cases, It will make application instance, make it to listen
// on it on port 3000 and add test document in DB
beforeAll(async () => {
// Make enviroment variables available throughout the application
dotenv.config();
// Getting application instance using iversify container
application = container.get<Application>(TYPES.Application);
// Initialize frontside of application
await application.bootstrap();
// Starting Application server on given port
await application.listen(3000);
});
afterAll(
//This hook is executed after running all test cases and delete test document in database
async () =>{
const res = await UserSchema.deleteMany({ Name: { $in: [ "Test User", "Test" ] } });
// `0` if no docs matched the filter, number of docs deleted otherwise
console.log('---------------------->>>>>>>>>>>>>>>>>>>', (res as any).deletedCount);
}
)
it("Signup for user that don\'t exists", async () => {
const response = await request(application.getServer()).post('/user/signup')
.send({
"Email": JSON.parse(process.env.TEST_USER).Email,
"Name": "Test User",
"Password": process.env.TEST_ACCOUNTS_PASSWORD
})
expect(response.status).toBe(RESPONSE_CODE.CREATED);
expect(JSON.parse(response.text)).toEqual(expect.objectContaining({
Message: RESPONSE_MESSAGES.ADDED_SUCESSFULLY,
Data: expect.objectContaining({
Name: 'Test User',
Country: '',
PhoneNumber: '',
// Password: '$2b$10$nIHLW/SA73XLHoIcND27iuODFAArOvpch6FL/eikKT78qbShAl6ry',
Dob: '',
Role: 'MEMBER',
IsEmailVerified: false,
IsBlocked: 'ACTIVE',
IsTokenSent: false,
twoFAStatus: false,
// _id: '5c812e2715e0711b98260fee',
Email: JSON.parse(process.env.TEST_USER).Email
})
})
);
console.log('*** Signup for user that don\'t exists *** response', response.text, 'response status', response.status);
});
it("Signup for user that exists", async () => {
const response = await request(application.getServer()).post('/user/signup')
.send({
"Email": JSON.parse(process.env.TEST_USER).Email,
"Name": "Test User",
"Password": process.env.TEST_ACCOUNTS_PASSWORD
})
expect(response.status).toBe(RESPONSE_CODE.CONFLICT);
expect(JSON.parse(response.text)).toEqual({
Message: RESPONSE_MESSAGES.ALREADY_EXISTS
})
console.log('*** Signup for user that don\'t exists *** response', response.text, 'response status', response.status);
});
});
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't
stopped in your tests. Consider running Jest with
--detectOpenHandles to troubleshoot this issue.
Cannot log after tests are done. Did you forget to wait for something
async in your test?
Attempted to log "{ accepted: [ 'unverifiedtestuser#abc.com' ],
rejected: [],
envelopeTime: 621,
messageTime: 867,
messageSize: 906,
response: '250 2.0.0 OK 1551945300 f6sm5442066wrt.87 - gsmtp',
envelope:
{ from: 'abc#gmail.com',
to: [ 'unverifiedtestuser#abc.com' ] },
messageId: '<45468449-b5c8-0d86-9404-d55bb5f4g6a3#gmail.com>' }".
at CustomConsole.log (node_modules/jest-util/build/CustomConsole.js:156:10)
at src/email/MailHandler.ts:2599:17
at transporter.send.args (node_modules/nodemailer/lib/mailer/index.js:226:21)
at connection.send (node_modules/nodemailer/lib/smtp-transport/index.js:247:32)
at callback (node_modules/nodemailer/lib/smtp-connection/index.js:435:13)
at stream._createSendStream (node_modules/nodemailer/lib/smtp-connection/index.js:458:24)
at SMTPConnection._actionSMTPStream (node_modules/nodemailer/lib/smtp-connection/index.js:1481:20)
at SMTPConnection._responseActions.push.str (node_modules/nodemailer/lib/smtp-connection/index.js:968:22)
at SMTPConnection._processResponse (node_modules/nodemailer/lib/smtp-connection/index.js:764:20)
at SMTPConnection._onData (node_modules/nodemailer/lib/smtp-connection/index.js:570:14)
I was using the react-native default test case (see below) when Cannot log after tests are done happened.
it('renders correctly', () => {
renderer.create(<App />);
});
Apparently, the problem was that the test ended but logging was still needed. So I tried to make the callback in the test case async, hoping that the test won't terminate immediately:
it('renders correctly', async () => {
renderer.create(<App />);
});
And it worked. However, I have very little clue what the inner working is.
If you are using async/await type in your code, then this error can occur when you are calling async function without await keyword.
In my case, I have defined a function like this below,
async getStatistics(headers) {
....
....
return response;
}
But I have called this method like getStatistics(headers) instead of await getStatistics(headers).
When I included await, it worked fine and the issue resolved.
In my case while using nodejs + jest + supertest the problem was that when I import app from "./app" to my test file to do some stuff with supertest (request(app)), I actually import with app.listen() , because when I'm exporting app, export takes in account app.listen() too, but we don't need app.listen() in tests and it throws an error
"Cannot log after tests are done.Did you forget to wait for something async in your test?"
Here is all in one file(that's the problem!)
const app = express();
app.use(express.json());
// ROUTES
app.get("/api", (req, res) => {
res.json({ message: "Welcome to Blog API!" });
});
app.use("/api/users", usersRoutes);
app.use("/api/blogs", blogsRouter);
// The server will start only if the connection to database is established
mongoose
.connect(process.env.MONGO_URI!)
.then(() => {
console.log("MongoDB est connecté");
const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`The server is running on port: ${port}`));
})
.catch(err => {
console.log(err);
});
export default app;
To solve this issue I created 2 separate folders:
// 1) app.ts
Where I put all stuff for my const app = express(), routes etc and export app
dotenv.config();
const app = express();
app.use(express.json());
// ROUTES
app.get("/api", (req, res) => {
res.json({ message: "Welcome to Blog API!" });
});
app.use("/api/users", usersRoutes);
app.use("/api/blogs", blogsRouter);
export default app;
// 2) index.ts
Where I put app.listen() and mongoose.connection() and import app
*import mongoose from "mongoose";
import app from "./app";
// The server will start only if the connection to database is established
mongoose
.connect(process.env.MONGO_URI!)
.then(() => {
console.log("MongoDB est connecté");
const port = process.env.PORT || 4000;
app.listen(port, () => console.log(`The server is running on port: ${port}`));
})
.catch(err => {
console.log(err);
});*
For me I needed to add an await before the expect() call also to stop this error (and an async before the test() callback function).
Also caused and fixed Jest not detecting coverage on the lines in the code throwing the error!
test("expect error to be thrown for incorrect request", async () => {
await expect(
// ^ added this
async () => await getData("i-made-this-up")
).rejects.toThrow(
"[API] Not recognised: i-made-this-up"
);
});
getData() returns an Axios call and in this case an error is caught by catch and re-thrown.
const getData = async (id) => {
return await axios
.get(`https://api.com/some/path?id=${id}`)
.then((response) => response.data)
.catch((error) => {
if (error?.response?.data?.message) {
console.error(error) // Triggered the error
throw new Error("[API] " + error.response.data.message);
}
throw error;
});
};
This happened to me because I had an infinite loop while (true). In my case, I was able to add a method for setting the value of the loop based on user input, rather than defaulting to true.
In my case, the error was caused by asynchronous Redis connection still online. Just added afterall method to quit Redis and could see the log again.
Working on Typescript 4.4.2:
test("My Test", done => {
let redisUtil: RedisUtil = new RedisUtil();
let redisClient: Redis = redisUtil.redis_client();
done();
});
afterAll(() => {
redisClient.quit();
});
I solved it with the env variables:
if (process.env.NODE_ENV !== 'test') {
db.init().then(() => {
app.listen(PORT, () => {
console.log('API lista por el puerto ', PORT)
})
}).catch((err) => {
console.error(err)
process.exit(1)
})
} else {
module.export = app
}
I faced same warnings. However the fix is bit weird:
The jest unit test script import a function (which is not export from src/). After I added the export to the function to be tested. The error disappears.
I had a similar issue:
Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "Warning: You seem to have overlapping act() calls, this is not supported. Be sure to await previous act() calls before making a new one. ".
It was due to a missing static keyword. This code caused the issue:
class MyComponent extends React.Component<Props, State> {
propTypes = {
onDestroy: PropTypes.func,
}
}
It should have been:
class MyComponent extends React.Component<Props, State> {
static propTypes = {
onDestroy: PropTypes.func,
}
}

Why does didenterregion triggers multiple times even I am already in that beacon region?

I have added one beacon region for monitoring.I am using the method didenterregion to identify when ever he enters into beacon region but the thing is even I am already in beacon region then also I am getting multiple times didenterregion signal.Can anyone help me why is this happening?
`
#Injectable()
export class BeaconProvider {
delegate: any;
region: any;
constructor(public platform: Platform, public events: Events,public IBeacon:IBeacon,public appService:AppService) {
}
initialise(): any {
let promise = new Promise((resolve, reject) => {
if (this.platform.is('cordova')) {
// Request permission to use location on iOS
this.IBeacon.requestAlwaysAuthorization();
this.IBeacon.getMonitoredRegions().then(data=>{
console.log(data);
});
this.IBeacon.getRangedRegions().then(data=>{
console.log(data);
});
this.delegate = this.IBeacon.Delegate();
// Subscribe to some of the delegate's event handlers
this.delegate.didEnterRegion()
.subscribe(
data => {
this.events.publish('this.delegate.didEnterRegion', data);
},
error => console.error()
);
this.delegate.didExitRegion()
.subscribe(
data => {
this.events.publish('this.delegate.didExitRegion', data);
},
error => console.error()
);
this.appService.getBeacons()
.subscribe(beacons => {
console.log(beacons);
var i=0;
for(i=0;i<beacons.length;i++){
// setup a beacon region – CHANGE THIS TO YOUR OWN UUID
this.region = this.IBeacon.BeaconRegion(beacons[i].beaconDetailes.beaconId,beacons[i].beaconDetailes.uuid,beacons[i].beaconDetailes.major,beacons[i].beaconDetailes.minor);
this.region.NotifyEntryStateOnDisplay = true;
this.region.NotifyOnEntry = true;
this.region.NotifyOnExit = true;
// start ranging
this.IBeacon.startMonitoringForRegion(this.region)
.then(
() => {
resolve(true);
console.log("monitor for region");
},
error => {
resolve(false);
}
);
}
}
);
} else {
resolve(false);
}
});
return promise;
}
}
`
This is the provider for beacon monitor.In some other pages I am triggering the method initialize to register beacon and start monitoring the beacons.Using events publish I am getting the beacon details when ever I entered into region in other pages.

Firebase + Phonegap plugin push: How to send silent messages but update app?

My current stack is:
Django using FCM to send push notifications to an Ionic app. The app uses the phonegap-plugin-push.
I have the problem, that the on notification handler doesn't get called.
Here is the data that I'm sending:
'message': {
'token': '<my-device-token>',
'data': {
'yup': 'okay'
},
'apns': {
'payload': {
'aps': {
'data': 'here is my data',
'badge': 1,
'content-available': 1
},
'notId': 2
}
}
}
The app gets the data, but somehow the on notificatoin handler doesn't get called.
Also here is my code in the app:
import { Injectable } from '#angular/core';
import { Push, PushObject, PushOptions } from '#ionic-native/push';
import { AlertController, Platform } from 'ionic-angular';
import { FcmDataProvider } from './fcm.data';
#Injectable()
export class FcmProvider {
/*
* FIREBASE CLOUD MESSAGING
*/
constructor(private push: Push,
private alertCtrl: AlertController,
private platform: Platform,
private fcmDataProv: FcmDataProvider) {
}
getPermission(): Promise<{isEnabled: boolean}> {
// Listen for res.isEnabled.
return this.push.hasPermission();
}
initPush() {
console.log("Init push!");
const options: PushOptions = this.initPushOptions();
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
if(this.platform.is('ios')) {
this.handleIOSNotification(notification, pushObject);
} else if(this.platform.is('android')) {
this.handleAndroidNotification(notification);
}
this.presentSuccessAlert(notification.message);
});
pushObject.on('registration').subscribe(
(registration: any) => {
console.log('Device registered', registration);
// TODO: Send registration.registrationId to server and update it.
}
);
pushObject.on('error').subscribe(
error => console.error('Error with Push plugin', error)
);
}
private initPushOptions(): PushOptions {
return {
android: {
sound: true,
vibrate: true,
clearBadge: true
},
ios: {
alert: true,
badge: true,
sound: true,
clearBadge: true
},
windows: {}, // Lol
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
}
private handleIOSNotification(data, push: PushObject) {
push.finish().then(
() => console.log("Finished processing push data")
).catch(() => console.error(
"Something went wrong with push.finish for ID=", data.additionalData.notId
));
}
private handleAndroidNotification(data) {
console.log(data.data);
}
private presentSuccessAlert(message: string): void {
let alert = this.alertCtrl.create({
title: "Neue Benachrichtigung",
message: message,
buttons: ["Ok"]
});
alert.present();
}
}
I'm testing on iOS but I would love to know how to handle it on android as well.
Edit:
Here is the console.log I receive from XCode:
Push Plugin notId 1
Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.
Notification received
Push Plugin key: content-available
Push Plugin key: data
Push Plugin key: badge
Wow this issue is super silly.
But here is what was wrong: You got to put notId first!
Like this:
"notId": 1, # notId HAS TO BE FIRST!!!
'aps': {
'data': 'here is my data',
'content-available': 1,
}

ionic 2 - Inappbrowser event fire after second call

I use inappbrowser plugin in ionic 2 application like this :
import {InAppBrowser} from 'ionic-native';
and use it like this :
launch(url){
this.browser = new InAppBrowser( url, "_blank", "EnableViewPortScale=yes,closebuttoncaption=Done" );
this.browser.on("exit")
.subscribe(
() => {
this.close_event=true;
},
err => {
console.log("InAppBrowser Loadstop Event Error: " + err);
});
}
and in html :
<button ion-button icon-right color="danger" (click)="launch('https://www.example.com')">launch
<ion-icon name="refresh"></ion-icon>
when click on launch button for first time and after close browser, exit event not fire but when for second time click on launch button and after close browser exit event is fire
Perhaps the first time you click on Launch button, the device platform is not ready yet?
Try to put the calls to any InAppBrowser methods inside Plaform.ready(), like so:
...
import { Platform } from 'ionic-angular';
import { InAppBrowser } from '#ionic-native/in-app-browser';
...
export class HomePage {
private iab: InAppBrowser;
private platform: Platform;
private browser;
...
var launch = function(url) {
this.platform.ready().then(
() => {
this.browser = this.iab.create( url, "_blank", "EnableViewPortScale=yes,closebuttoncaption=Done" );
this.browser.on("exit").subscribe(
(event) => {
this.close_event = true;
},
(err) => {
console.log("InAppBrowser Loadstop Event Error: " + err);
}
);
}
);
};
...
}

Delay in retrieving data from Ionic 2 storage

Before I launches the app I will check with local storage if any user data available, If yes I will navigation to Home page else Login page.
Here I'm unable to retrieve stored data, Any inputs please...
Currently using Ionic 2 SQlite plugin.
Note: In browser it's working fine but on Android device it's not working.
app.component.ts : checking user data
loadUser() {
this.userSettings.getUser().then(user => {
this.userObj = JSON.stringify(user);
if (user) {
console.log('App : ', this.userObj);
this.nav.setRoot(HomePage,this.userObj);
} else {
console.log('App : No user data');
this.rootPage = LoginPage;
}
});
}
login.ts : Saving user data
this.userSettings.addUser(
userData.employeeCode,
userData.password,
userData.role
);
user-settings.ts : Storage file in providers
getUser() {
if (this.sql) {
return this.sql.get('user').then(value => value);
} else {
return new Promise(resolve => resolve(this.storage.get('user').then(value => value)));
}
}
addUser(employeeCode, password, role) {
let item = { employeeCode: employeeCode, password: password, role: role };
if (this.sql) {
this.sql.set('user', JSON.stringify(item)).then(data => {
this.events.publish('userObj:changed');
});
} else {
return new Promise(resolve => {
this.storage.set('user', JSON.stringify(item)).then(() => {
this.events.publish('userObj:changed');
resolve();
});
});
}
}
app.module.ts:
providers: [
{ provide: ErrorHandler, useClass: IonicErrorHandler },
AuthService,
SqlStorage,
UserSettings,
Storage
]
Thanks in advance.
Problem solved
After calling the sqlite operation in ngAfterViewInit it's working fine.
ngAfterViewInit() {
this.storage.get('user').then((user: any) => {
if (user) {
this.userCredentials = JSON.parse(user);
this.nav.setRoot(HomePage, this.userCredentials);
}
else {
this.rootPage = LoginPage;
}
});
}
[Source] (https://github.com/driftyco/ionic-conference-app/blob/master/src/pages/account/account.ts)
Cheers :)
As you point out that your code is working in Chrome, but not on your device, you might be calling sqlite before cordova's device.ready() has fired.
In app.component.ts ensure you call this.loadUser() in the following manner: (platform.ready() should already be in the constructor)
platform.ready().then(() => {
this.loadUser();
});