Integración OneSignal y configuración HTTP nativo
- Configurar OneSignal con nuevo App ID - Agregar network_security_config.xml para permitir HTTP - Habilitar CapacitorHttp nativo en capacitor.config.ts - Actualizar SDK a Android 34 - Configurar API_URL fija (192.168.10.207:8080) - Agregar onesignal.service.ts Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import { LanguageService } from './services/language.service';
|
||||
import { AlertService } from './services/alert.service';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { OneSignalService } from './services/onesignal.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
@@ -29,9 +30,12 @@ export class AppComponent {
|
||||
private events: EventService,
|
||||
private alertService: AlertService,
|
||||
private loadingCtrl: LoadingController,
|
||||
private oneSignalService: OneSignalService,
|
||||
) {
|
||||
this.initializeApp();
|
||||
this.events.subscribe('set_role', role => {
|
||||
// Set OneSignal user tags when role is set (user logged in)
|
||||
this.setupOneSignalUser(role);
|
||||
if (role >= 2){
|
||||
this.appPages = [
|
||||
{
|
||||
@@ -104,23 +108,32 @@ export class AppComponent {
|
||||
await StatusBar.setStyle({ style: Style.Light });
|
||||
await StatusBar.setOverlaysWebView({ overlay: false });
|
||||
await StatusBar.setBackgroundColor({ color: '#0080ff' });
|
||||
// TODO: Implement OneSignal with Capacitor plugin
|
||||
// this.handlerNotifications();
|
||||
|
||||
// Initialize OneSignal push notifications
|
||||
await this.oneSignalService.init();
|
||||
}
|
||||
|
||||
this.languageService.getDefaultLanguage();
|
||||
this.authService.getToken();
|
||||
}
|
||||
|
||||
private async setupOneSignalUser(role: number) {
|
||||
if (this.authService.token && this.authService.token.user_id) {
|
||||
await this.oneSignalService.setUserId(this.authService.token.user_id);
|
||||
await this.oneSignalService.setUserRole(role);
|
||||
}
|
||||
}
|
||||
|
||||
// When Logout Button is pressed
|
||||
async logout() {
|
||||
this.loading = await this.loadingCtrl.create();
|
||||
await this.loading.present();
|
||||
|
||||
this.authService.logout().subscribe(
|
||||
data => {
|
||||
async data => {
|
||||
this.alertService.presentToast(this.translateService.instant('alerts.logout'));
|
||||
// TODO: Implement OneSignal tag clearing with Capacitor
|
||||
// Clear OneSignal user tags on logout
|
||||
await this.oneSignalService.logout();
|
||||
},
|
||||
error => {
|
||||
this.loading.dismiss();
|
||||
@@ -136,7 +149,4 @@ export class AppComponent {
|
||||
async openUrl(url: string) {
|
||||
await Browser.open({ url });
|
||||
}
|
||||
|
||||
// TODO: Re-implement push notifications with OneSignal Capacitor plugin
|
||||
// private handlerNotifications() { ... }
|
||||
}
|
||||
|
||||
@@ -4,13 +4,11 @@ import { Injectable } from '@angular/core';
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class EnvService {
|
||||
// Usa el mismo host pero puerto 8080 para el API
|
||||
API_URL = (typeof window !== 'undefined' && window.location.hostname !== 'localhost')
|
||||
? `http://${window.location.hostname}:8080/api/`
|
||||
: 'http://localhost:8080/api/';
|
||||
API_URL = 'http://192.168.10.207:8080/api/';
|
||||
SECRET = 'wBIIKuDbrxNKzQhAUGiZLoaoQ4MichAN3wP2AP7B';
|
||||
MERCHANT_ID = 'm9k4beuso5az0wjqztvt';
|
||||
PUBLIC_API_KEY = 'pk_9465179493384689a8d2da9adc825411';
|
||||
ONESIGNAL_APP_ID = 'c854ae89-7ff7-4216-a70e-5fdff0cd8e10';
|
||||
|
||||
constructor() { }
|
||||
}
|
||||
|
||||
132
src/app/services/onesignal.service.ts
Normal file
132
src/app/services/onesignal.service.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { EnvService } from './env.service';
|
||||
import { Router } from '@angular/router';
|
||||
|
||||
declare var OneSignalPlugin: any;
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class OneSignalService {
|
||||
private initialized = false;
|
||||
|
||||
constructor(
|
||||
private env: EnvService,
|
||||
private router: Router
|
||||
) { }
|
||||
|
||||
async init(): Promise<void> {
|
||||
if (this.initialized || !Capacitor.isNativePlatform()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Initialize OneSignal
|
||||
OneSignalPlugin.initialize(this.env.ONESIGNAL_APP_ID);
|
||||
|
||||
// Request notification permission
|
||||
OneSignalPlugin.Notifications.requestPermission(true).then((accepted: boolean) => {
|
||||
console.log('OneSignal notification permission:', accepted ? 'accepted' : 'denied');
|
||||
});
|
||||
|
||||
// Handle notification clicks
|
||||
OneSignalPlugin.Notifications.addEventListener('click', (event: any) => {
|
||||
console.log('OneSignal notification clicked:', event);
|
||||
this.handleNotificationClick(event);
|
||||
});
|
||||
|
||||
// Handle foreground notifications
|
||||
OneSignalPlugin.Notifications.addEventListener('foregroundWillDisplay', (event: any) => {
|
||||
console.log('OneSignal notification received in foreground:', event);
|
||||
// Display the notification
|
||||
event.getNotification().display();
|
||||
});
|
||||
|
||||
this.initialized = true;
|
||||
console.log('OneSignal initialized successfully');
|
||||
} catch (error) {
|
||||
console.error('Error initializing OneSignal:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async setUserId(userId: number | string): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Set user tag for targeting
|
||||
OneSignalPlugin.User.addTag('iChamba_ID', String(userId));
|
||||
console.log('OneSignal user tag set:', userId);
|
||||
} catch (error) {
|
||||
console.error('Error setting OneSignal user tag:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async setUserRole(roleId: number | string): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
OneSignalPlugin.User.addTag('iChamba_Role', String(roleId));
|
||||
console.log('OneSignal role tag set:', roleId);
|
||||
} catch (error) {
|
||||
console.error('Error setting OneSignal role tag:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async clearTags(): Promise<void> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
OneSignalPlugin.User.removeTags(['iChamba_ID', 'iChamba_Role']);
|
||||
console.log('OneSignal tags cleared');
|
||||
} catch (error) {
|
||||
console.error('Error clearing OneSignal tags:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async logout(): Promise<void> {
|
||||
await this.clearTags();
|
||||
}
|
||||
|
||||
private handleNotificationClick(event: any): void {
|
||||
const data = event?.notification?.additionalData;
|
||||
|
||||
if (data) {
|
||||
// Handle navigation based on notification data
|
||||
if (data.route) {
|
||||
this.router.navigate([data.route]);
|
||||
} else if (data.type) {
|
||||
switch (data.type) {
|
||||
case 'contract':
|
||||
this.router.navigate(['/contracts']);
|
||||
break;
|
||||
case 'postulation':
|
||||
this.router.navigate(['/postulations']);
|
||||
break;
|
||||
default:
|
||||
this.router.navigate(['/dashboard']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getPlayerId(): Promise<string | null> {
|
||||
if (!Capacitor.isNativePlatform()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const deviceState = await OneSignalPlugin.User.pushSubscription.getId();
|
||||
return deviceState || null;
|
||||
} catch (error) {
|
||||
console.error('Error getting OneSignal player ID:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user