why is ipcRenderer.sendSync() causing crashing the application? - electron

In the onbeforeunload-Event, I use in my renderer-process ipcRenderer.sendSync() to notify the main-process to show a dialog:
window.onbeforeunload = (e) => {
const response = ipcRenderer.sendSync('askForSavingChanges');
if (response == 0) {
// save ...
} else if (response == 2) {
e.returnValue = false;
}
}
Here is the code in the main-process:
app.whenReady().then(() => {
initSettings().then(() => {
ipcMain.on('askForSavingChanges', (event) => event.returnValue = require('electron').dialog.showMessageBoxSync(this,
{
type: 'question',
buttons: ['Yes', 'No', 'Cancel'],
title: 'Closing',
message: 'Do you want to save changes?'
}));
})
createWindow();
});
Now, when I close the window, the event onbeforeunload will be called, but when calling ipcRenderer.sendSync() the window will be closed immediately. Do I something wrong?

Related

How to pass geolocation permission to react-native-webview?

When using the geolcoation api in a react-native-webview, I am asked twice whether the app is allowed to use my current location. How is it possible to forward the already (not) granted permission to the webview?
I am currently using react-native 0.68 and react-native-webview 11.22.
First prompt:
Second prompt:
I should only be asked once for permission to use my current geolocation.
In case somebody faces the same problem, I solved this issue with the following workaround. I injected a custome javacscript into the webview to override the used geolocation api in the webview. My custome script does all the communication with the app. The app returns the geolocation and so the webview doesn't need to aks for permission.
Custome Script
export const getGeoLocationJS = () => {
const getCurrentPosition = `
navigator.geolocation.getCurrentPosition = (success, error, options) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'getCurrentPosition', options: options }));
window.addEventListener('message', (e) => {
let eventData = {}
try {
eventData = JSON.parse(e.data);
} catch (e) {}
if (eventData.event === 'currentPosition') {
success(eventData.data);
} else if (eventData.event === 'currentPositionError') {
error(eventData.data);
}
});
};
true;
`;
const watchPosition = `
navigator.geolocation.watchPosition = (success, error, options) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'watchPosition', options: options }));
window.addEventListener('message', (e) => {
let eventData = {}
try {
eventData = JSON.parse(e.data);
} catch (e) {}
if (eventData.event === 'watchPosition') {
success(eventData.data);
} else if (eventData.event === 'watchPositionError') {
error(eventData.data);
}
});
};
true;
`;
const clearWatch = `
navigator.geolocation.clearWatch = (watchID) => {
window.ReactNativeWebView.postMessage(JSON.stringify({ event: 'clearWatch', watchID: watchID }));
};
true;
`;
return `
(function() {
${getCurrentPosition}
${watchPosition}
${clearWatch}
})();
`;
};
Webview
import Geolocation from '#react-native-community/geolocation';
let webview = null;
<WebView
geolocationEnabled={ true }
injectedJavaScript={ getGeoLocationJS() }
javaScriptEnabled={ true }
onMessage={ event => {
let data = {}
try {
data = JSON.parse(event.nativeEvent.data);
} catch (e) {
console.log(e);
}
if (data?.event && data.event === 'getCurrentPosition') {
Geolocation.getCurrentPosition((position) => {
webview.postMessage(JSON.stringify({ event: 'currentPosition', data: position }));
}, (error) => {
webview.postMessage(JSON.stringify({ event: 'currentPositionError', data: error }));
}, data.options);
} else if (data?.event && data.event === 'watchPosition') {
Geolocation.watchPosition((position) => {
webview.postMessage(JSON.stringify({ event: 'watchPosition', data: position }));
}, (error) => {
webview.postMessage(JSON.stringify({ event: 'watchPositionError', data: error }));
}, data.options);
} else if (data?.event && data.event === 'clearWatch') {
Geolocation.clearWatch(data.watchID);
}
}}
ref={ ref => {
webview = ref;
if (onRef) {
onRef(webview)
}
}}
source={ url }
startInLoadingState={ true }
/>

Ionic 4 Popover, Alertsheet etc... on iOS scrolls back content and not the popover itself

I hope you all having a great day.
I am facing an issue on iOS with ActionSheets and Popovers, while a popover is appeared, the user can scroll when swiping inside the popover, and it scrolls the back content on which i open the popover from, if i click outside of the popover it closes the popover and it does not let me swipe at all but when i click inside the popover it scrolls the back content of the popover not the popover itself.
here is a video of it:
sorry had no option to upload video, here is a link:
Video of popover/actionsheet
here is code of opening the popover
async openCreateNewFolder(type, folder?) {
const popover = await this.popoverController.create({
component: NewFolderComponent,
componentProps: {
folderId: this.folderId,
parentId: this.parentId,
type,
folder
}
});
popover.onDidDismiss().then((dataReturned) => {
console.log(dataReturned);
if (dataReturned.data !== undefined) {
if (dataReturned.data === 'cancelClicked') {
} else if (dataReturned.data === 'confirmClickedFolder') {
this.deleteFolder(dataReturned.role);
} else if (dataReturned.data === 'confirmClickedFile') {
this.deleteFile(dataReturned.role);
} else if (dataReturned.data === 'newFolderCreated') {
this.getFolders();
}
// this.dataReturned = dataReturned.data;
//alert('Modal Sent Data :'+ dataReturned);
}
});
return await popover.present();
}
here is a code for the actionsheet:
async confirmChangeLanguageDialogue(selectedLanguage) {
let languageClass: any;
if (selectedLanguage === 'English') {
languageClass = 'alertControllerEnglishLanguageIcon';
} else if (selectedLanguage === 'Deutch') {
languageClass = 'alertControllerGermanLanguageIcon';
} else if (selectedLanguage === 'French') {
languageClass = 'alertControllerFrenchLanguageIcon';
} else if (selectedLanguage === 'Italian') {
languageClass = 'alertControllerItalianLanguageIcon';
}
const alert = await this.alertCtrl.create({
header: this.translate.instant('confirm'),
mode: 'ios',
message: this.translate.instant('change_language_confirm_message', {selected_language: selectedLanguage}),
buttons: [
{
text: this.translate.instant('cancel'),
role: 'cancel',
cssClass: 'secondary',
handler: (blah) => {
console.log('Confirm Cancel: blah');
}
}, {
text: this.translate.instant('okay'),
cssClass: languageClass,
handler: () => {
this.requestChangeLanguage(selectedLanguage);
}
}
]
});
await alert.present();
const result = await alert.onDidDismiss();
console.log(result);
}
Any help would be appriciated, thank you.
fix can be found here, check it out:
https://forum.ionicframework.com/t/popover-alertsheet-etc-on-ios-scrolls-back-content-and-not-the-popover-itself/180122

don't display notification in ios device when use react-native-fcm

i'm using from react-native-fcm for recieve pushNotification and do all config in this document(https://github.com/evollu/react-native-fcm)
in ios device only recieve notification and call notificationListener that checked by console.log but dont display notification message and alert even test FCM.presentLocalNotification for show local notification still dont show notification
async componentDidMount() {
if (Platform.OS === 'ios') {
try {
const result = await FCM.requestPermissions({ badge: false, sound: true, alert: true });
} catch (e) {
console.error(e);
}
FCM.getFCMToken().then(token => {
if (token !== undefined) {
// this.props.onChangeToken(token);
} else {
console.log('TOKEN (getFCMToken)', 'null');
}
});
// FCM.getAPNSToken().then(token => {
// this.props.onAPNToken(token);
// });
FCM.getInitialNotification().then(notif => {
console.log('INITIAL NOTIFICATION', notif);
});
this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
console.log(" >> notificationListener: ", notif)
if (notif.local_notification) return;
FCM.presentLocalNotification({
body: 'tdtydt',
priority: "high",
title: 'notif.fcm.title',
sound: "default",
show_in_foreground: true,
local_notification: true,
icon: "ic_launcher",
status: "400"
});
});
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, token => {
console.log('TOKEN (refreshUnsubscribe)', token);
this.props.onChangeToken(token);
});
FCM.enableDirectChannel();
this.channelConnectionListener = FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => {
console.log(`direct channel connected${data}`);
});
setTimeout(() => {
FCM.isDirectChannelEstablished().then(d => console.log('isDirectChannelEstablished', d));
}, 500);
}

ionic 3 - popToRoot not destroying page

I am using ionic 3 and when I popToRoot() the current page still executes and does not gets destoryed. I thought popToRoot() is suppose to destroy the current page as it goes to the root page? Here is the code:
ionViewDidLoad() {
this.menuCtrl.swipeEnable(false);
this.navCtrl.swipeBackEnabled = false;
this.loop();
}
loop() {
this.API.doGet(URL)
.then((data) => {
if (data == 'Yes') {
...
}
else {
// Wait on timer and call again - 10 seconds
this.Timeout = setTimeout(() => { this.onLoopTimeout() }, 10000);
}
})
.catch((err) => {
this.errorTimeout = setTimeout(() => { this.loop(); }, 3000);
})
}
onLoopTimeout() {
this.loop();
}
notInRepair() {
// Clear timers
clearTimeout(this.Timeout);
clearTimeout(this.errorTimeout);
// Go back to home page
this.navCtrl.setRoot(ServicesPage);
this.navCtrl.popToRoot();
}
Do you guys know what I am doing wrong here?

How to call downloadItem.pause() and downloadItem.pause() from outside of session.on(‘will-download’)?

I am trying to implement pause and resume functionality with electron libraries. how could i call item.pause out of session.on(‘will-download’) because it is function of item inside session.on(‘will-download’) or from any other way?
var url, path, filename;
ipcMain.on(‘item:file’, function(e, file) {
if(file.msg === ‘d’){
console.log(file);
url = file.url;
path = pathLib.join(__dirname, file.path);
filename = file.filename;
contents.downloadURL(url);
}
if(file.msg === ‘p’){
item.pause();
}
if(file.msg === ‘r’){
item.resume();
}
contents.session.on('will-download', (event, item, contents) => {
// Set the save path, making Electron not to prompt a save dialog.
path = `${path}${filename}`;
item.setSavePath(path);
item.on('updated', (event, state) => {
if (state === 'interrupted') {
console.log('Download is interrupted but can be resumed');
} else if (state === 'progressing') {
if (item.isPaused()) {
console.log('Download is paused');
} else {
console.log(`Received bytes: ${item.getReceivedBytes()} out of ${item.getTotalBytes()}`);
var percentage = (item.getReceivedBytes()/item.getTotalBytes())*100;
var progress = {'rBytes':item.getReceivedBytes(), 'tBytes':item.getTotalBytes(), 'p':percentage};
contents.send('item:progress', progress);
}
}
});
item.once('done', (event, state) => {
if (state === 'completed') {
console.log('Download successfully');
} else {
console.log(`Download failed: ${state}`);
}
});
});
});
https://github.com/sindresorhus/electron-dl/blob/master/index.js
Following, given above, link Worked for me!

Resources