Hi, i have a problem whit silent push notifications in ionic and firebase.
I have an application that connects to external devices and must be periodically requesting to detect the device in android, everything goes well, it does it in the background with a setinterval or failing that, we send a notification that executes the necessary tasks, but in ios a Once the app is put in the background, the setinterval is canceled and silent notifications do not trigger any event in ionic and default notifications do not run until touched.
Current operation:
-I listen to the event by console in xcode but it does not activate any function in ionic project…-
-When i get one push notification by default it is not activated either until i touch it.
Expected performance:
-When listen a event silent push notification in xcode or default push notification run task event in ionic without wouching or doing anything in the notification.
Usage
-I implement notifications in ionic whit capacitor-push-notification and firebase.
Capacitor-push-notification
-I implement silent push whit this documentation.
Send-silent-push-notifications
Codes
Listen notification in xcode
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Perform background operation
if let value = userInfo["some-key"] as? String {
print(value) // output: "some-value"
}
// Inform the system after the background operation is completed.
completionHandler(.newData)
}
Register token and listen notifications
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
PushNotifications.register();
} else {
console.log('¬¬¬¬¬¬¬¬¬¬error. granted',JSON.stringify(result))
}
});
// On success, we should be able to receive notifications
PushNotifications.addListener( 'registration',(token: Token) => {
console.log('My token: ' + JSON.stringify(token))
this.tokenDevice.getTokenUser(token.value);
console.log('My token > Push registration success, token: ' + token.value)
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error: any) => {
// alert('Error on registration: ' + JSON.stringify(error));
console.log('Error on registration: ' + JSON.stringify(error))
}
);
PushNotifications.addListener('pushNotificationReceived',async (notification: any) => {
console.log('----------------- pushNotificationReceived'+ JSON.stringify(notification))
if (notification.title == 'Medición Horaria') {
let ultimaFecha = localStorage.getItem('ultimaFecha')
// console.log('Sincronizando datos de '+new Date(Date.parse(ultimaFecha)).toString())
let descargandoDatos = (localStorage.getItem('descargandoDatos') == 'true')
if (!descargandoDatos) {
localStorage.setItem('descargandoDatos', 'true')
this.bluetoothLEService.syncData(Date.parse(ultimaFecha), true)
setTimeout(() => {
this.bluetoothLEService.compararDatos()
}, 10000)
const loading = await this.bluetoothLEService.loadingController.create({
message: 'Actualizando datos. Por favor no cierre ni suspenda la aplicación.',
duration: 10000,
});
loading.present()
}
}
else {
switch (notification.data['body']) {
case '1':
this.isConnectDeviceStatus()
break;
case '2':
this.singleRealtimeMeasureNotfyRecive(0X81)
break
case '3':
this.singleRealtimeMeasureNotfyRecive(0X11)
break
case '4':
this.singleRealtimeMeasureNotfyRecive(0X09)
case '5':
this.singleRealtimeMeasureNotfyRecive(0X21)
break;
default:
break;
}
}
this.zone.run(() => {
this.notifications.push(notification);
});
}
);
I have read in several forums and in the same capacitor documentation that silent notifications are not available in ios but I need a solution that can be executed in ios from ionic, that opens a channel for us for a certain time and periodically or that receives us the silent notification events.
Thanks