Skip to content

Instantly share code, notes, and snippets.

@hieunhums
Created February 21, 2019 08:47
Show Gist options
  • Select an option

  • Save hieunhums/5c5fd307632fc2db0615d8315cbd5320 to your computer and use it in GitHub Desktop.

Select an option

Save hieunhums/5c5fd307632fc2db0615d8315cbd5320 to your computer and use it in GitHub Desktop.
app.components.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';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { Device } from '@ionic-native/device';
import { HomePage } from '../pages/home/home';
declare var WindowsAzure: any;
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,public push: Push, private device: Device ) {
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.styleDefault();
splashScreen.hide();
this.pushsetup();
});
}
private pushOptions: PushOptions;
pushsetup() {
// to check if we have permission
// this.push.hasPermission()
// .then((res: any) => {
// if (res.isEnabled) {
var client = new WindowsAzure.MobileServiceClient('https://hieumobilequickstart.azurewebsites.net');
console.log('We have permission to send push notifications');
// configuration of push notification
this.pushOptions = {
android: {
senderID: '922835198017',
},
ios: {
alert: 'true',
badge: true,
sound: 'false',
},
windows: {}
};
const pushObject : PushObject = this.push.init(this.pushOptions);
// pushObject.on('registration').subscribe((data: any) => {
// console.log('device token -> ' + data.registrationId);
// //TODO - send device token to server
// });
pushObject.on('registration').subscribe((registration: any) => {
// Fires when device finishes registering to the push service
console.log('Device registered', registration);
var platform = this.device.platform;
// Get the handle returned during registration.
var handle = registration.registrationId;
// Set the device-specific message template.
if (platform == 'android' || platform == 'Android') {
// Register for GCM notifications.
client.push.register('gcm', handle, {
mytemplate: { body: { data: { message: "{$(messageParam)}" } } }
});
} else if (this.device.platform === 'iOS') {
// Register for notifications.
client.push.register('apns', handle, {
mytemplate: { body: { aps: { alert: "{$(messageParam)}" } } }
});
} else if (this.device.platform === 'windows') {
// Register for WNS notifications.
client.push.register('wns', handle, {
myTemplate: {
body: '<toast><visual><binding template="ToastText01"><text id="1">$(messageParam)</text></binding></visual></toast>',
headers: { 'X-WNS-Type': 'wns/toast' } }
});
}
});
pushObject.on('notification').subscribe((notification: any) => {
alert('Received a notification' + notification)
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
// });
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment