Skip to content

Instantly share code, notes, and snippets.

@nvieirarafael
Last active June 14, 2017 19:15
Show Gist options
  • Save nvieirarafael/8e39c4a592db168fee210c5a9f9984b5 to your computer and use it in GitHub Desktop.
Save nvieirarafael/8e39c4a592db168fee210c5a9f9984b5 to your computer and use it in GitHub Desktop.
Definindo módulo principal aplicação Angular 2 para receber push notifications e registrar token do device em serviço
// src/app/app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
// Importando os dois componentes necessários para receber 'push notifications'
import { Push, PushToken } from '@ionic/cloud-angular';
//
import { HomePage } from '../pages/home/home';
import { Http, HttpModule } from '@angular/http';
@Component({
templateUrl: 'app.html',
providers: [HttpModule]
})
export class MyApp {
rootPage:any = HomePage;
constructor(
platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
public push: Push, // injetando o componente Push
http: Http
) {
platform.ready().then(() => {
statusBar.styleDefault();
splashScreen.hide();
this.push.register().then((t: PushToken) => {
return this.push.saveToken(t);
}).then((t: PushToken) => {
// #1 // a partir daqui temos o token do device e
// podemos enviar uma requisição para nosso
// servidor registra-lo
http.post('http://localhost:3000/api/v1/device', { registration: t.token })
.subscribe(data => {
console.log(data['_body']);
}, error => {
console.log(error);
});
//
console.log('Token saved:', t.token);
});
this.push.rx.notification()
.subscribe((msg) => {
// #2 // esta callback executa sempre que o
// device receber notificações
console.log('I received awesome push: ' + msg);
//
});
});
}
}
// src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { HttpModule } from '@angular/http';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
// Configurações do ionic cloud
import { CloudSettings, CloudModule } from '@ionic/cloud-angular';
const cloudSettings: CloudSettings = {
'core': {
'app_id': '5efe0ae0'
},
'push': {
'sender_id': 'xxxxxxxxxxxx',
'pluginConfig': {
'ios': {
'badge': true,
'sound': true
},
'android': {
'iconColor': '#ff0000'
}
}
}
};
//
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
CloudModule.forRoot(cloudSettings), // registrando as configurações do ionic cloud no modulo
HttpModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment