IONIC 2云端推送不起作用 - 应用程序崩溃

时间:2017-04-13 07:21:28

标签: ionic-framework ionic2

我已经创建了IONIC 2 App,并且在最后阶段我正在尝试实施IONI云推送,但是每当我启用云推送寄存器功能时每次App崩溃都无法正常工作:

            this.push.register().then((t: PushToken) => {
          return this.push.saveToken(t);
        }).then((t: PushToken) => {
          console.log('Token saved:', t.token);
        });

我在我的真实设备中使用' 离子运行android' 运行构建

请帮助我,我做错了什么。已经检查了很多文章到处都说我已经实现了相同的过程: -

npm install @ionic/cloud-angular --save
cordova plugin add phonegap-plugin-push --variable SENDER_ID=12341234 --save
ionic io init

以及所有其他配置。

以下是我的app.component.ts

    import { Component, ViewChild } from '@angular/core';
import { Nav, Platform, ToastController } from 'ionic-angular';
import { Events } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Storage } from '@ionic/storage';
import { TabsComponent } from '../pages/tabs/tabs-component/tabs.component';
import { LoginComponent } from '../pages/login/login-component/login.component';

import { Push, PushToken } from '@ionic/cloud-angular';
//import { Push } from 'ionic-native';
//import { CloudSettings, CloudModule } from '@ionic/cloud-angular';
//import { Push, PushObject, PushOptions } from "@ionic-native";


@Component({
    templateUrl: './app.html',
})

export class BanglaliveApp {
    @ViewChild(Nav) nav: Nav;

    rootPage = TabsComponent;
    user: any;

    pages: Array<{title: string, component: any, category: any}>;

    constructor(
        public push: Push,
        public platform: Platform,
        private storage: Storage,
        private toastController: ToastController,
        public events: Events
        ) {

        this.initializeApp();

        events.subscribe('userloggedin', user => {  
          if(user){
            this.user = user;
          }
        });

        storage.get('wordpress.user').then((value) => {
            if (value) {
                this.user = value;
            } 
        });

        this.pages = [
          { title: '????? ????', component: TabsComponent, category: { class:'home' }},
          { title: '?????????', component: LoginComponent, category: {class: 'category'}}
        ];

    }

    initializeApp() {
        this.platform.ready().then(() => {
            // Enable RTL Support
            // this.platform.setDir('rtl', true);
            StatusBar.styleDefault();
            Splashscreen.hide();


// ############## P U S H ###############
            this.push.register().then((t: PushToken) => {
              return this.push.saveToken(t);
            }).then((t: PushToken) => {
              console.log('Token saved:', t.token);
            });


            // this.push.rx.notification()
            // .subscribe((msg) => {
            //  alert(msg.title + ': ' + msg.text);
            // });
// ############## P U S H ###############


        });
    }


    openPage(page, index) {

        //check if page or post has been called
            this.nav.setRoot(page.component);
        }
    }

    login(){
        this.nav.push(LoginComponent);      
    }

    logout() {
        this.user = undefined;
        this.storage.remove('wordpress.user');
        this.nav.setRoot(LoginComponent);
    }

    profile() {     
        this.nav.push(ProfilePage);
    }
}

这是我的app.module.ts: -

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { SharedModule } from './shared/shared.module'
import { TabsModule } from '../pages/tabs/tabs.module';
import { SignUpModule } from '../pages/signup/signup.module'; //SSSS
import { ProfileModule } from '../pages/profile/profile.module'; //SSSS
import { MyApp} from './app.component';


import { CloudSettings, CloudModule } from '@ionic/cloud-angular';

const cloudSettings: CloudSettings = {
  'core': {
    'app_id': 'XXXXXX',
  },
  'push': {
    'sender_id': 'XXXXXXXX',
    'pluginConfig': {
      'ios': {
        'badge': true,
        'sound': true
      },
      'android': {
        'iconColor': '#343434'
      }
    }
   }
};

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    IonicModule.forRoot(MyApp, {
      backButtonIcon: 'arrow-back'
    }),
    CloudModule.forRoot(cloudSettings),
    SharedModule,
    TabsModule,
    SignUpModule,
    ProfileModule,
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}

请帮助.. !!

由于

2 个答案:

答案 0 :(得分:0)

它在真实设备上对我来说非常好。

使用 Git Repo播放。

enter image description here

<强> app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Push, PushToken } from '@ionic/cloud-angular';

import { HomePage } from '../pages/home/home';
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage: any = HomePage;

  constructor(platform: Platform, public push: Push) {
    platform.ready().then(() => {

      this.push.register().then((t: PushToken) => {
        return this.push.saveToken(t);
      }).then((t: PushToken) => {
        console.log('Token saved:', t.token);
      });

      this.push.rx.notification()
        .subscribe((msg) => {
          alert(msg.title + ': ' + msg.text);
        });


    });
  }
}

<强> app.module.ts

import { CloudSettings, CloudModule } from '@ionic/cloud-angular';

const cloudSettings: CloudSettings = {
  'core': {
    'app_id': '525f53a4'
  },
  'push': {
    'sender_id': '837506444622',
    'pluginConfig': {
      'ios': {
        'badge': true,
        'sound': true
      },
      'android': {
        'iconColor': '#343434'
      }
    }
  }
};

@NgModule({
  declarations: [
    ],
  imports: [
    CloudModule.forRoot(cloudSettings)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ],
  providers: [
    ]
})
export class AppModule { }

答案 1 :(得分:0)

最后我发现过时的cordova s​​plash插件导致了这个错误并且重新安装没有升级,因为在config.xml中有旧版本的插件。

我刚刚删除了插件并从config.xml中删除了行并重新安装了它并且工作正常。

以防它也可能适用于其他人。