So I have an Ionic 2 application, it’s fairly simple and I have been trying to get push notifications integrated.
I have gone other the documentation multiple times but even though there are no errors in the build, the push notifications won’t come through. I have looked at the code and it seems that the “this.push.register()” function is not calling as I have a console.log within it that never displays in the console. Here is my code:
app.module.ts:
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { BrowserModule } from '@angular/platform-browser';
import { Http, HttpModule} from "@angular/http";
import { Storage } from "@ionic/storage";
import { RouterModule } from "@angular/router";
import { QRCodeModule } from 'angular2-qrcode';
import { Angular2TokenService } from 'angular2-token';
import { CloudSettings, CloudModule } from '@ionic/cloud-angular';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { MyApp } from './app.component';
import { HomePage } from '../pages/login/login';
import { RegisterPage } from '../pages/register/register';
import { SettingsbasicPage } from '../pages/settingsbasic/settingsbasic';
import { LanguagePage } from '../pages/language/language';
import { OpendoorPage } from '../pages/open-door/open-door';
import { SettingsPage } from '../pages/settings/settings';
import { CreatepincodePage } from '../pages/create-pincode/create-pincode';
import { VerifypincodePage } from '../pages/verify-pincode/verify-pincode';
import { ActionsPage } from '../pages/actions/actions';
import { ChangeSociasecurityPage } from '../pages/change-social-security/change-social-security';
import { ChangeFirstnamePage } from '../pages/change-first-name/change-first-name';
import { ChangeLastnamePage } from '../pages/change-last-name/change-last-name';
import { ChangeEmailPage } from '../pages/change-email/change-email';
import { ChangePasswordPage } from '../pages/change-password/change-password';
import { EnterSocialsecurityPage } from '../pages/enter-social-security/enter-social-security';
import { BankIDPage } from '../pages/bank-id/bank-id';
const cloudSettings: CloudSettings = {
'core': {
'app_id': '7f6764b2'
},
'push': {
'sender_id': '660073317521',
'debug': true,
'pluginConfig': {
'ios': {
'badge': true,
'sound': true
},
'android': {
'iconColor': '#343434'
}
}
}
};
export function HttpLoaderFactory(http: Http) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
MyApp,
HomePage,
RegisterPage,
SettingsbasicPage,
LanguagePage,
OpendoorPage,
SettingsPage,
CreatepincodePage,
VerifypincodePage,
ActionsPage,
ChangeSociasecurityPage,
ChangeFirstnamePage,
ChangeLastnamePage,
ChangeEmailPage,
ChangePasswordPage,
EnterSocialsecurityPage,
BankIDPage
],
imports: [
IonicModule.forRoot(MyApp, {
backButtonText: 'Test'
}, {}),
BrowserModule,
HttpModule,
RouterModule,
QRCodeModule,
IonicModule.forRoot(MyApp),
CloudModule.forRoot(cloudSettings),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [Http]
}
})
],
bootstrap: [IonicApp, MyApp ],
entryComponents: [
MyApp,
HomePage,
RegisterPage,
SettingsbasicPage,
LanguagePage,
OpendoorPage,
SettingsPage,
CreatepincodePage,
VerifypincodePage,
ActionsPage,
ChangeSociasecurityPage,
ChangeFirstnamePage,
ChangeLastnamePage,
ChangeEmailPage,
ChangePasswordPage,
EnterSocialsecurityPage,
BankIDPage
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}, Angular2TokenService, Storage ]
})
export class AppModule {}
app.component.ts:
import { Component } from '@angular/core';
import { Push, PushToken } from '@ionic/cloud-angular';
import { Storage } from "@ionic/storage";
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { TranslateService } from '@ngx-translate/core';
import { HomePage } from '../pages/login/login';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = HomePage;
lang = '';
constructor(platform: Platform, translate: TranslateService, public push: Push, public storage: Storage) {
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.
StatusBar.backgroundColorByHexString('#343538');
StatusBar.styleLightContent();
Splashscreen.hide();
});
// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('en');
this.storage.get('myLang').then((data) => {
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.setDefaultLang(data);
});
// Push Notifications
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);
});
}
}
Those are the only two pages that have any code to do with push as that is how I understood it was to be done. As I said, I never see the “console.log(‘Token saved:’, t.token);” come through on the console but I am getting no build errors. Anyone have any idea why this is? Another question is do I need to implement Ionic Auth for push to work?