ionic 3 get current location not working on android phone - ionic-framework

My ionic 3 application tries to get the currentlocation by using Geolocation from '#ionic-native/geolocation/ngx'.
When i rum my app in android phone,getLoc() is getting called but getCurrentPosition() method is not getting called.
getLoc() {
alert('getLoc');
this.geolocation.getCurrentPosition().then((resp) => {
resp.coords.latitude
resp.coords.longitude
alert('inside then');
}).catch((error) => {
console.log('Error getting location', error);
alert('catch');
});
}

The correct import for ionic 3 is :
import { Geolocation } from '#ionic-native/geolocation';
try to change the import, if it don't work , downgrade the geolocation plugin version.

Related

Initialize OneSignal SDK on Ionic with React.js

I'm trying to start with OneSignal SDK on a Ionic with React app, but I can't find any guide for React, all I find is for angular and I'm not pratice of typescript.
Where do I need to initialize OneSignal SDK?
I'm trying to do it in the App.componentDidMount such as:
componentDidMount() {
this.props.oneSignal.startInit(
"XXXXXX-XXX-XXX-X-XXXXX",
"YYYYYYYYYY"
);
this.props.oneSignal.inFocusDisplaying(
this.props.oneSignal.OSInFocusDisplayOption.InAppAlert
);
this.props.oneSignal.handleNotificationReceived().subscribe(() => {
// do something when notification is received
});
this.props.oneSignal.handleNotificationOpened().subscribe(() => {
// do something when a notification is opened
});
this.props.oneSignal.endInit();
}
but the IDE continues giving me errors about this.oneSignal does not exists in the type xxx.
My fault.
I was importing this way:
import { OneSignal } from '#ionic-native/onesignal/ngx';
This the correct way for React.js:
import { OneSignal } from '#ionic-native/onesignal';

I can't receiving notifications when the app is closed or swiped away on huawei device?

I'm using onsignal notifications with my flutter app,I've tested my app on three Samsunge devices and notifcations working perfectly in all these devices when the app on foreground, background, and also when I swiped it away.
after that I tested the app on huawei device which using EMUI 9.0.1 Os
the notifications only works if the app is active or on background
if I swiped it away I can't receiving any notifications.
Any help would be greatly appreciated, I've been struggling with this for a long time. I'll post my code for setting onesignal below
Future<void> initPlatformState() async {
if (!mounted) return;
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setRequiresUserPrivacyConsent(true);
OneSignal.shared.consentGranted(true);
var settings = {
OSiOSSettings.autoPrompt: false,
OSiOSSettings.promptBeforeOpeningPushUrl: true
};
OneSignal.shared.setNotificationReceivedHandler((notification) {
this.setState(() {
print('Notifiaction received');
});
});
OneSignal.shared
.setNotificationOpenedHandler((OSNotificationOpenedResult result) {
this.setState(() {
newUrl = result.notification.payload.additionalData['url'].toString();
});
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => WebNotification(newUrl)));
});
// NOTE: Replace with your own app ID from https://www.onesignal.com
await OneSignal.shared
.init("xxxx-xxxx-xxxx-xxxx-xxxx", iOSSettings: settings);
OneSignal.shared
.setInFocusDisplayType(OSNotificationDisplayType.notification);
OneSignal.shared.inFocusDisplayType();
}
You need to set up a service extension. Take a look at our docs on Background Notifications. Also, consider Notification Behavior when designing your implementation
make sure you sue latest version of onesignal
for huwaii use HMS if possible ( onesignal support HMS )
In your root build.gradle, under buildscript, add the following 2 new lines to your existing repositories and dependencies sections
buildscript {
repositories {
// ...
maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal
}
dependencies {
// ...
// OneSignal-Gradle-Plugin
classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.8, 0.99.99]'
}
}
Add the following to the top of your app/build.gradle
apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
you need to make user add your app to ignore battry optmztion in huwaii it is (protacted app )
in flutter you can make button and attach it to the app setting ( use these plugin https://pub.dev/packages/app_settings)

Having issue with Battery status in Ionic 4

I'm using Ionic4 and I'm trying to get the device battery level. But I get an error:
ERROR TypeError: Invalid event target
and for the battery level, I get undefined.
Has anyone run into the same problem
There is an open bug report at ionic: https://github.com/ionic-team/ionic-native/issues/2972
Unfortunately no solution yet, however there is a workaround to bypass the ionic typescript wrapper and just listen directly on the window events.
fromEvent(window, 'batterystatus').subscribe((status) => {
console.log(status)
});
This is how I managed to get the battery status in Ionic 4:
window.addEventListener('batterystatus', this.onBatteryStatus, false);
onBatteryStatus(status) {
console.log('Level: ' + status.level + ' isPlugged: ' + status.isPlugged);
}
An easy way could be like this:
$ ionic cordova plugin add cordova-plugin-battery-status
$ npm install --save #ionic-native/battery-status#4
Then in your code:
import { Platform } from 'ionic-angular';
import { BatteryStatus } from '#ionic-native/battery-status';
batterylevel = 0;
constructor(...
private batteryStatus: BatteryStatus,
private plt: Platform ) {}
ionViewDidEnter()
{
// Cordova check
if(this.plt.is('core') || this.plt.is('mobileweb'))
{
// NO. It's a browser.
// don't call the batery.
}
else
{
const batterysubscription = this.batteryStatus.onChange().subscribe(status => {
this.batterylevel = status.level;
});
}
}
yourfunction()
{
console.log(this.batterylevel);
}
You'll also need to add it to your provider list too.
Of course, this will only work in emulation or a device.

Tracking all screens using Google Analytics in Ionic 3 project

I'm in the middle of finalizing an Ionic 3 build and would like to add Google Analytics to it. I added it successfully and can see in GA (real time) that the app is being used however I would like to track all page/screen views. Does anyone know of way to do that?
I'm using the following plugin: https://github.com/danwilson/google-analytics-plugin
Here is the code I am using to initialize GA
initGoogleAnalytics() {
var trackingId = 'UA-114720506-2';
if (/(android)/i.test(navigator.userAgent)) { // for android
trackingId = 'UA-114720506-2';
} else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) { // for ios
trackingId = 'UA-114720506-2';
}
//platform is injected in the Constructor
this.platform.ready().then(() => {
this.ga.debugMode();
this.ga.startTrackerWithId(trackingId).then(()=> {
console.log("GoogleAnalytics Initialized with ****** : " + trackingId);
this.ga.trackView('schedule');
this.ga.trackView('speakerList');
this.ga.trackView('map');
this.ga.trackView('social');
this.ga.trackView('exhibitors');
this.ga.enableUncaughtExceptionReporting(true)
.then((_success) => {
console.log("GoogleAnalytics enableUncaughtExceptionReporting Enabled.");
}).catch((_error) => {
console.log("GoogleAnalytics Error enableUncaughtExceptionReporting : " + _error)
});
});
});
}
Essentially adding in the GA Module and initializing with ionViewDidEnter on in every page that I wanted to track did the trick.

How to integrate Anyline OCR SDK made for Cordova into Ionic2

I´m new to Ionic2, but experienced in web development. Just learning new platform at the moment.
So I have tried to integrate the Anyline OCR SDK
https://github.com/Anyline/anyline-ocr-cordova-module
but I am failing, it seems to me that the plugin is written in Javascript and not compatible with TS but I´m not sure...
Is there anyone out there that could help?
Thanks,
Ben
Not sure if you still need help with that, but for those out there who are looking for a working solution, here is mine:
1 - Add the Anyline Plugin to your project cordova plugin add io-anyline-cordova
2 - Create a new file ionic g provider anyline
3 - add this code to your anyline.ts file:
export class OCR {
constructor() {
if (anylineScan === undefined) {
var anylineScan = {};
}
}
anylineScan = {
onResult: function (result) {
console.log("MRZ result: " + JSON.stringify(result));
//do what you want here with the result
},
onError: function (error) {
console.log("scanning error");
},
scan: function () {
var licenseKey = "enterYourLicenceKeyHere";
try {
(<any>window).cordova.exec(this.onResult, this.onError, "AnylineSDK", "OCR", [licenseKey, {
"captureResolution":"1080p",
//your other config setting here
}]);
}
catch (e){
console.log("Cannot open scan view: ERROR occurred");
}
}
}
4 - Add the file references to your app.module.ts file
import { OCR } from '../yourFolderName/anyline';
...
providers: [Storage, OCR]
5 - In your page.ts file add the following call:
this.anyline.anylineScan.scan();
Important! It will not work in the browser, make sure you run ionic platform add ios (or android) and run the app on a device.
This should work!
Good luck and happy coding :-)