-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.component.ts
126 lines (115 loc) · 3.71 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { Component } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { StorageService } from './services/storage.service';
import { Device, DeviceId } from '@capacitor/device';
import { PermissionStatus, PushNotifications, Token } from '@capacitor/push-notifications';
import { NavController, ToastController } from '@ionic/angular';
import { SoundService } from './services/sound.service';
import { UserService } from './services/user.service';
import { register } from 'swiper/element/bundle';
register();
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent {
_langs = ["es", "en"];
constructor(
private translate: TranslateService,
private storage:StorageService,
private toastCtrl:ToastController,
private navCtrl:NavController,
private sound:SoundService,
private userService:UserService,
) {
const _self = this;
PushNotifications.addListener('registration',
(token: Token) => {
Device.getId().then(
(id:DeviceId) => {
_self.userService.registerFcm(token, id).then(
(result:any) => {
console.log(result.message);
},
(error) => {
console.log(error.error);
}
)
}
)
}
);
PushNotifications.addListener('registrationError',
(error:any) => {
console.log('PUSH NOTIFICATION ERROR', error.error);
}
);
PushNotifications.addListener('pushNotificationReceived', notification => {
if(notification.link) {
_self.toastCtrl.create({
header: notification.title,
message: notification.body,
color: 'sky',
swipeGesture: 'vertical',
position: 'top',
duration: 5000,
buttons: [
{
icon: 'open',
side: 'end',
handler: () => {
_self.navCtrl.navigateForward(notification.link!);
}
}
]
}).then(
(toast) => {
toast.present();
_self.sound.play('assets/sounds/notification.mp3');
}
)
} else {
_self.toastCtrl.create({
header: notification.title,
message: notification.body,
color: 'sky',
swipeGesture: 'vertical',
position: 'top',
duration: 5000,
}).then(
(toast) => {
toast.present();
_self.sound.play('assets/sounds/notification.mp3');
}
)
}
});
PushNotifications.addListener('pushNotificationActionPerformed', (notification) => {
const data = notification.notification.data;
if(data.url) {
_self.navCtrl.navigateRoot(`${data.url}`);
}
});
_self.initializeApp();
}
initializeApp() {
Device.getLanguageCode().then(
(deviceLanguage) => {
this.storage.get('DEFAULT_LANGUAGE').then(
(defaultLanguage) => {
if(defaultLanguage && defaultLanguage.length > 0 && this._langs.find(supportedLanguage => supportedLanguage === defaultLanguage)) {
this.translate.setDefaultLang(defaultLanguage);
} else if(deviceLanguage && deviceLanguage.value.length > 0 && this._langs.find(supportedLanguage => supportedLanguage === deviceLanguage.value)) {
this.translate.setDefaultLang(deviceLanguage.value);
} else {
this.translate.setDefaultLang('es');
}
}
)
}
)
}
async listenNotifications() {
}
}