В настоящее время я работаю над мобильным приложением, использующим ionic, но недавно я столкнулся с препятствием.
1: всякий раз, когда я запускаю приложение в браузере ionic serve --lab
, оно отлично работает.
2: Когда я запускаю приложение на реальном Android-устройстве, оно не может открыть приложение и остается с первым видом, и оно никогда не исчезает по умолчанию, но пока я отлаживаю приложение с помощью chrome://inspect
, там отображается страница входа,Я также предоставлю несколько снимков экрана.
Следующие коды принадлежат файлу app.component.ts.
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { Storage } from '@ionic/storage';
import { App } from 'ionic-angular';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { AboutPage } from '../pages/about/about';
import { CartPage } from '../pages/cart/cart';
import { timer } from "rxjs/observable/timer";
import { LoginPage } from '../pages/loginPages/loginMainPage/login';
import firebase from 'firebase';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = LoginPage;
showSplash = true;
pages: Array<{ title: string, component: any, icon }>;
sendGetRequest() {
// ****** Pack the all return in the new promise ******
return new Promise(async (resolve, reject) => {
await this.storage.length().then(async (data) => {
if (data >= 1) {
console.log('There is one data');
await this.storage.get('isLogined').then(value => {
console.log('value is', value);
// if (value) this.app.getActiveNav().setRoot(LoginPage);
}
);
this.statusBar.styleDefault();
this.splashScreen.hide();
}
}).catch((error) => {
console.log('error while accessing isLogined \n' + error);
reject(error);
})
});
}
constructor(private push: Push,
public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public storage: Storage,
public app: App) {
console.log('The fist line of constructor ');
this.platform.ready().then(async () => {
timer(3000).subscribe(async () => {
await this.sendGetRequest();
this.showSplash = false;
})
})
}
ngOnInit() {
console.log('ngOnInit');
this.pushSetup();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'خانه', component: HomePage, icon: 'home' },
{ title: 'پروفایل', component: ListPage, icon: 'person' },
{ title: 'سبد خرید', component: CartPage, icon: 'cart' },
{ title: 'درباره', component: AboutPage, icon: 'person' },
];
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.statusBar.styleDefault();
this.splashScreen.hide();
firebase.initializeApp({
apiKey: "AIzaSyAej7RIuFY2_SKhsgcv9-7U7e34ZV85Yf0",
authDomain: "raihan-mobile.firebaseapp.com",
databaseURL: "https://raihan-mobile.firebaseio.com",
projectId: "raihan-mobile",
storageBucket: "raihan-mobile.appspot.com",
messagingSenderId: "551951714209"
})
timer(3000).subscribe(() => this.showSplash = false)
});
}
pushSetup() {
// to check if we have permission
this.push.hasPermission().then((res: any) => {
if (res.isEnabled) { console.log('We have permission to send push notifications'); } else {
console.log('We do not have permission to send push notifications');
}
});
const options: PushOptions = {
android: { senderID: "551951714209", },
ios: { alert: 'true', badge: true, sound: 'false' },
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));
pushObject.on('registration').subscribe((registration: any) => console.log('Device registered', registration.registrationId));
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
}