With the release of version 8.3.23 of the Cordova plugin, we added the setShowPushnotificationAlert method. It is still available in the current plugin release (8.3.70).
The method provides developers with the flexibility to display push notifications when the app is running in the foreground. By setting this method to either true or false, you can control the visibility of notifications according to your app's needs.
By default, foreground notification display is set to false, meaning notifications will not be displayed while the app is in the foreground. On Android you can also set the default via the com.pushwoosh.foreground_push meta-data key in AndroidManifest.xml.
To implement foreground notifications in your Cordova/Capacitor app, follow the example below:
function initPushwoosh() {
var pushwoosh = cordova.require("pushwoosh-cordova-plugin.PushNotification");
// Should be called before pushwoosh.onDeviceReady
document.addEventListener('push-notification', function(event) {
var notification = event.notification;
// Handle push open here
console.log('Received push notification: ', notification);
});
// Initialize Pushwoosh. This will trigger all pending push notifications on start.
// "appid" is the only configuration key the plugin reads.
pushwoosh.onDeviceReady({
appid: "APP_CODE"
});
// Registering for push notifications
pushwoosh.registerDevice(
function(status) {
var pushToken = status.pushToken;
// Handle successful registration here
console.log('Push token received: ', pushToken);
},
function(status) {
// Handle registration error here
console.error('Push registration failed: ', status);
}
);
pushwoosh.setShowPushnotificationAlert(true);
}
Note: older versions of this example passed projectid (FCM Sender ID) and serviceName to onDeviceReady. Neither is used any more: the plugin reads only appid, Android FCM registration is driven by google-services.json, and serviceName only ever applied to the discontinued Windows Phone (MPNS) platform. Remove both keys from your init code.
Comments
0 comments
Please sign in to leave a comment.