React Native Android - custom BroadcastReceiver/Service is being killed after some hours - javascript

I'm building an app using React Native which requires a service that detects missed calls and sends that on the server and then shows a notification in phone status bar.
I decided to write my own extension that will handle that because I didn't found any node module that will be sufficient for my needs. Unfortunately, service is being killed after some hours and I can't handle with that. Basically, I'm JavaScript developer and native code in Java is for me like a black hole so I'll be very grateful for any help.
The app is using Headless JS for sending data to the server, basically all extension was based on articles:
http://www.learn-android-easily.com/2013/06/detect-missed-call-in-android.html
https://codeburst.io/simple-android-call-recorder-in-react-native-headlessjs-task-614bcc56efc4
I've found some similar topics:
Android service process being killed after hours
https://fabcirablog.weebly.com/blog/creating-a-never-ending-background-service-in-android
And tried to follow the instructions described there, but all of these solutions are related only with native code without using React Native and Headless JS so I don't know if that solutions will be ok for app that using React Native or probably (for sure) I'm doing something wrong.
Here is my AndroidManifest part responsible for Service and BroadcastReceiver:
(...)
<service android:name="com.app.service.CallLogService" />
<receiver android:name="com.app.receiver.CallLogReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
(...)
My CallLogService class:
package com.app.service;
import android.content.Intent;
import android.os.Bundle;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
import javax.annotation.Nullable;
public class CallLogService extends HeadlessJsTaskService {
#Nullable
protected HeadlessJsTaskConfig getTaskConfig(Intent intent) {
Bundle extras = intent.getExtras();
return new HeadlessJsTaskConfig(
"CallLog",
extras != null ? Arguments.fromBundle(extras) : null,
5000,
true
);
}
}
My CallLogReceiver class:
package com.app.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.app.service.CallLogService;
import com.facebook.react.HeadlessJsTaskService;
public final class CallLogReceiver extends BroadcastReceiver {
public final void onReceive(Context context, Intent intent) {
(...)
callerPhoneNumber = intent.getStringExtra("incoming_number");
Intent callIntent = new Intent(context, CallLogService.class);
callIntent.putExtra("phone_number", callerPhoneNumber);
context.startService(callIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
}
}
I'm using React Native 0.50.3
At the end I have an additional question. I noticed that after restart phone the service is also killed. How can I prevent such situation too?
Edit:
I noticed that if app is in background then after sending request the response is not recorded.
But after firing a new request the application is getting response from previous request. I'm using axios for doing ajax.
Eg.
let callAjax = function(counter){
console.log('Request ' + counter);
axios.get('/user?ID=12345')
.then(function (resp) {
console.log('Response ' + counter);
})
};
callAjax(1);
setTimeout(() => {
callAjax(2);
}, 5000);
When app is in background then I have:
Request 1
After 5 sec
Response 1 Request 2
When app is in foreground then everything is ok:
Request 1 Response 1
After 5 sec
Request 2 Response 2

Related

export java methods to react native from an aar library

I'm trying to integrate a SDK in my react native app, and i'm little bit confused, as documentation says, i know that i have to write a bridge in order to use the native library in react native.
here is what i've done:
I put the sdk-file.aar in android/app/libs.
I created libraryModule.java and libraryPackage.java
with the folowing code
libraryModule.java
package com.myapp.android;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import java.util.Map;
import java.util.HashMap;
import com.facebook.react.bridge.Promise;
import com.ekassir.mirpaysdk.client.MirApp;
public class MirPayModule extends ReactContextBaseJavaModule {
MirPayModule(ReactApplicationContext context) {
super(context);
}
#Override
public String getName() {
return "MirPaySdk";
}
#ReactMethod
{
}
}. // I think some methods should be here
My question is: should i recreate all methods present in the library ?
I'm receiving the sdk from a bank, should i recreate all methods with #ReactMethod tag in order to have the possibility to access them in react native ?
Am i missing something in the process of integration ?

Office Add-ins excel extension is not working on IE11 with 'import axios' statement

I'm currently developing a Excel extension for my company with Office Add-ins and Reactjs.
Lately I've been using Axios in order to make http requests to get data from a remote back-end service. The issue is that the statement "import axios from 'axios'" is not handled in IE11 and the application runs into an exception when this statement is present in a script file. The exception is :
Office.js has not fully loaded. Your app must call "Office.onReady()" as part of it's loading sequence (or set the "Office.initialize" function). In itself that doesn't say a lot except that "the code does not compile".
After a few researches on my own I discovered that IE11 needs polyfill in order to make it work since it doesn't natively support the most recent js scripts (ES6, promises by example).
I've tried many kinds of combinations with babel/polyfill, react-app-polyfill, es-promise with no result so far. Happily, in a another application I've been working on recently (VueJs with axios), I met the same problem and just adding 'import babel/polyfill' did the trick.
I would like to know if anyone has succeeded in doing what I am trying to do for a few days and if not, any help will be appreciated. Some of my coworkers are using Windows 7 or Windows Server 2019 and I really need this excel extension to function with IE11.
Thank you for your help.
Edit on 06/29 :
Here is what I have in my index.js :
import "office-ui-fabric-react/dist/css/fabric.min.css";
import App from "./components/App";
import { AppContainer } from "react-hot-loader";
import { initializeIcons } from "office-ui-fabric-react/lib/Icons";
import * as React from "react";
import * as ReactDOM from "react-dom";
/* global AppCpntainer, Component, document, Office, module, React, require */
initializeIcons();
let isOfficeInitialized = false;
const title = "Contoso Task Pane Add-in";
const render = Component => {
ReactDOM.render(
<AppContainer>
<Component title={title} isOfficeInitialized={isOfficeInitialized} />
</AppContainer>,
document.getElementById("container")
);
};
Office.initialize = () => {
isOfficeInitialized = true;
render(App);
};
render(App);
if (module.hot) {
module.hot.accept("./components/App", () => {
const NextApp = require("./components/App").default;
render(NextApp);
});
}
commands.js : You can see that I have a commented import statement in this file regarding axios. My intent is to make it work in the dataservice instead but I put that statement here just for testing purpose. The dataservice class itself has only one dependency : axios.
import "babel-polyfill";
// import axios from 'axios';
import DataService from '../server/dataservice';

React PWA - enforce update on click

community,
I am doing "programmatic presentations" using React (CLI) and PWA (register()). Everything works just fine, but anytime some changes are made, the URL of the final app needs to be changed so all changes are loaded.
The whole mechanism works like this:
The final app is sent to Github,
this private repo is connected to Netlify,
Netlify generates a unique URL,
users visit this Netlify URL and hit "add to home screen" on iPad,
the whole app runs under the Safari engine.
If any change in the code is made, I have to change the link in Netlify and send this new link to a people.
The process mentioned above works just fine, but honestly, it would be nice to have some kind of functionality that allows request latest update on demand - let's say - on click of a button.
Is something like that possible?
Thank you for comments!
Kind Regards
Linc
At serviceWorker.js file can find this code
if (config && config.onUpdate) {
config.onUpdate(registration);
}
So implement the config.onUpdate funtion
Create a file swConfig.js
export default {
onUpdate: registration => {
registration.unregister().then(() => {
window.location.reload()
})
},
}
At index.js send the implement function to serviceWorker
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import swConfig from './swConfig'
ReactDOM.render(<App />,
document.getElementById('root'));
serviceWorker.register(swConfig);
Check out this repo
https://github.com/wgod58/create_react_app_pwaupdate
If you want to control the update with a button click, I did using the following snippet:
Note: If your app must work offline, you should add some extra logic to verify if the user has internet connection, as the following code would break the app if it's unable to fetch the service-worker.
import React from 'react';
function App(){
const updateApp = () => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.ready.then((registration) => {
registration.unregister().then(() => {
window.location.reload();
});
});
}}
return(
<div style={{margin:"auto"}}>
<button onClick={updateApp}>
Update App
</button>
</div>
);
}
https://gist.github.com/juliomilani/6492312d1eb657d06b13c9b87d5ad023

Cannot instantiate pubnub sdk in angular 2 + typescript application

I am trying to use the pubnub javascript 4.0 sdk within my Angular 2 application that is using Typescript. I have successfully installed the library through npm and linked the module using SystemJS, and I can import it into my service that I'm using for chat. The problem comes when I try to access it within the service itself, I get a Error: CompileMetadataResolver. There are no type definitions for this library, so I found an open source .d.ts file someone posted online. Am I missing a type file (hence the metadata resolve)?
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import {PUBNUB} from 'pubnub';
export class ChatService {
private pnService: PUBNUB.PUBNUB;
constructor(private pubNub: PUBNUB){
//trying to access or create object here results in error
this.pnService = new
}
initialize(subkey:string, pubKey:string) {
//this.pubNub.init({subscribe_key : subKey, publish_key : pubKey, ssl: true, origin: 'pubsub.pubnub.com'});
}
}

How to import Parse JS sdk into ionic 2 beta

am trying to import JS sdk into ionic 2 app, but i keep getting parse is undefined
In ionic 1.x ,parse js sdk is loaded via
<script ..parse.js </script>
and exposed as a global var, how do import in ionic 2 ,am using the npm module ,and tried
import * as parse from 'parse'
Do npm install parse --save in your project directory
Then import parse using
import { Parse } from 'parse';
It is better to create an parse provider.
You can use this starter template as a guide. It is a simple GameScores application in ionic to get you started.
https://github.com/Reinsys/Ionic-Parse
It shows how to create and read data from parse server. I also includes paging with ion-infinite-scroll scrolling.
After searching for a solution I came up with my own.
After installing the package and the typings, I opened the index.js of the node-module ionic-gulp-scripts-copy and added 'node_modules/parse/dist/parse.min.js' to the defaultSrc array.
Then, in my index.html, I included the script above the cordova.js.
Now I just need to declare var Parse: any; in every Component I want to use the SDK in.
For example, in my app.ts:
import {Component} from '#angular/core';
import {Platform, ionicBootstrap} from 'ionic-angular';
import {StatusBar} from 'ionic-native';
import {TabsPage} from './pages/tabs/tabs';
import{LoginPage} from './pages/login/login';
declare var Parse: any;
#Component({
template: '<ion-nav [root]="rootPage"></ion-nav>',
})
export class MyApp {
private rootPage: any;
private parse;
constructor(private platform: Platform) {
//this.rootPage = TabsPage;
this.rootPage = LoginPage;
platform.ready().then(() => {
console.log("Platform ready!");
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Parse.initialize('myStartUp', 'someKey');
Parse.serverURL = 'http://localhost:1337/parse';
});
}
}
ionicBootstrap(MyApp);
I do not think this is the way it should be used, but in the end I can use the SDK pretty easy and without much lines of implementation code.

Categories

Resources