Issue
I am dealing with a problem with IONIC and FCM. It is the following, I am sending "data" not "notifications" from my server in Node JS to my app in IONIC, I see in the console that the data is sent, but I don't know how to access it, according to the documentation, it is in Intent extras, but I have not had success with it. I could use the "notifications" that also serve me, but the problem with that is that only if you click on the push notification do you get the data ... so if the client does not click on the notification and enters the app from the background will not be able to get that data ...
My server in Node:
const options = {
priority: "high",
timeToLive: 60 * 60 * 24,
};
const message_notification = {
/*
notification: {
title: "¡Tienes un nuevo pedido!",
body: "Tienes 30 segundos para aceptar el pedido, de lo contrario será rechazado.",
sound: "default",
vibrate: 1,
Badge: 1,
click_action: "FCM_PLUGIN_ACTIVITY"
},
*/
data: {
detalles: JSON.stringify(nuevopedido)
},
};
admin
.messaging()
.sendToDevice(primeroCola.socket_id, message_notification, options)
.then((response) => {
console.log("Notificación enviada", response);
})
.catch((error) => {
console.log(error);
});
My app in ionic:
this.fcm.onNotification().subscribe((data) => {
console.log(JSON.parse(data.detalles))
});
I tried with the cordova intent web plugin in Ionic, but I can't get the FCM intent, just my project in ionic, I don't really know much about Android.
WebIntent.getIntent().then((intent) => {
console.log('Action' + JSON.stringify(intent.action));
const intentExtras = intent.extras;
if (intentExtras == null) {
console.log("No hay extras")
} else {
console.log('Extras de intent: ' + JSON.stringify(intentExtras));
}
}, (error) => {
console.log('Error getting launch intent', error);
});
Solution
Add this before of FCMPlugin.sendPushPayload(data);
in onMessageReceived (file: MyFirebaseMessagingService):
FCMPlugin.setInitialPushPayload(data);
It happens that when you only send "data" without "notification" in FCM, it does not make a set of the payload (FCMPlugin.setInitialPushPayload(data)
), because there is nothing to tapped, there is no notification and it cannot listen to the "notification" event because the app is closed, so there is to get the data when you open the application again (when the app is closed) in some way, then setting that when using the getInitialPushPayload in your app, you will get the data:
App in ionic:
async created() {
const payload = await this.fcm.getInitialPushPayload()
console.log(payload)
},
Now, when you open your application that was closed, you will receive the data.
Answered By - user7371424
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.