Adding additional parameter to the payload in iOS - foreground/background state
Is it possible to add additional parameter to the payload sent to browser in iOS, except the 'wasTapped'? For example, to determine the foreground/background state of the app during the push?
In Android, added a flag which is changed by the onResume/onPause callbacks of the Activity, and I'm sending it on the payload as below:
data.put("wasTapped", true); data.put("foreground", !FCMPlugin.isBackground());
I don't know how to add it on iOS.
I'll describe the scenario I'm using it: Our app handles the push notifications in different ways. For example:
- If the app is on foreground and a push is being received, we show a popup with the push content, asking the user if to perform specific action relevant to the push. (in this case, wasTapped = false)
- If the app is on background, or closed/killed, when clicking it we open the app and handle the push and we perform the action relevant to the push without showing a popup as in foreground. (in this case, wasTapped = true)
The issue is that if the app is on background, but not closed, and push is being received, I'm getting first both notification on the OS, and also the onNotification is being called before clicking the notification. In this case, the 'wasTapped' is false, so I usually handle it as described in scenario #1 above, as foreground push and it shows the popup which I don't want in this case. Later I click the push, and then another callback of onNotification is being called, now with 'wasTapped' = true which is fine.
So, in order to filter the first callback, i added a logic (in Android currently):
if (window.cordova && window.cordova.platformId === "android") { if (!notificationData.wasTapped && !notificationData.foreground){ return; } }
So I can't just consider 'wasTapped = true' as background, and if it's false as foreground, which doesn't good in my case. I hope it's clear.
Maybe I'm missing something? :) Thanks!
I believe I understood.
On JS, you could try something like:
var isBackground = true
document.addEventListener("pause", () => isBackground = false, false);
document.addEventListener("resume", () => isBackground = true, false);
// ... and in the `onNotification`:
if (!notificationData.wasTapped && isBackground){
return;
}
Let me know if it doesn't work.
I believe I understood.
On JS, you could try something like:
var isBackground = true document.addEventListener("pause", () => isBackground = false, false); document.addEventListener("resume", () => isBackground = true, false); // ... and in the `onNotification`: if (!notificationData.wasTapped && isBackground){ return; }Let me know if it doesn't work.
Thanks for the suggestion. I actually tried that, but it's not good enough to use the foreground state according to the HTML event, since these HTML events callbacks are being received before the FCM plugin callbacks and it's always considered as foreground. So I did this workaround which seems to work meantime... What do you think? :)
Listening to HTML foreground/background events:
document.addEventListener("pause", function () {
isForegroundHTML = false;
foregroundTime = undefined;
});
document.addEventListener("resume", function () {
isForegroundHTML = true;
foregroundTime = Date.now();
});
As I saw, getInitialPushPayload contains value only when the app was killed/closed, so I consider that as foreground always and add the below params:
FCM.getInitialPushPayload().then(function (payload) {
if (payload){
payload.wasTapped = true;
payload.isForegroundNative= false;
handleNotification(payload); // Method below
}
});
FCM.eventTarget.addEventListener(
"notification",
function (data) {
handleNotification(data.detail); // Method below
},
false
);
If the foreground event of HTML occurred 2 sec ago, I consider it as the push arrived on foreground.
handleNotification(notificationData){
if (window.cordova.platformId === "ios" && notificationData.isForegroundNative == undefined) {
if (isForegroundHTML && foregroundTime && (Date.now() - foregroundTime) < 2000) {
notificationData.isForegroundNative = false;
} else {
notificationData.isForegroundNative = true;
}
}
...
...
...
}
Thanks.
Using time to infer its state is not ideal. Well, you convinced me that this deserves to be a feature.
Keep the solution you got working, and I'll let you know when the feature is implemented.
@andrehtissot I want to thank you for your great support and help with all questions and issues!