fcm background ionic 3 - ionic-framework

i am using FCM in ionic 3
I am getting notification properly when the app is in foreground. When I receive a notification in background, on tapping of notification i am getting the data. But i want to get the data without tapping on the notification. For example see the below code.
this.fcm.onNotification().subscribe(data => {
if(data.wasTapped){
console.log("Received in background");
} else {
console.log("Received in foreground");
};
});
So, my problem is to get data in background without tapping the notification

Related

when app is in background setNotificationWillShowInForegroundHandler doesnot print the notification title in console notification is send in onesignal

OneSignal.shared.setNotificationWillShowInForegroundHandler((OSNotificationReceivedEvent event) {
event.complete(event.notification);
print('notification title ${event.notification.title}');
});``

Run action before Ionic Vue closes or app runs in background

How can i detect in Ionic Vue if the app closes or if the app is send to the background?
this.platformor platform isn't available or does not work?!
You can subscribe to the appStateChange event, like described here: https://capacitorjs.com/docs/apis/app
Example:
import { App } from '#capacitor/app'
export default {
created () {
App.addListener('appStateChange', state => {
if (!state.isActive) { // if isActive is true, App got sent to foreground, if it is false, it got sent to the background
...
}
})
}
}

Flutter save OneSignal notification message

I want to save the message from OneSignal push notification, so I can create a notification list.. but I have no idea to achieve this.. I open the Onesignal for flutter documentation but still no clue.. anyone can share some references to me?
In your main.dart make sure you have the listener setNotificationReceivedHandler activated. Then just save the notification.payload elements
OneSignal.shared.setNotificationReceivedHandler((notification) {
this.setState(() {
_debugLabelString =
"Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
newNotificationTitle = notification.payload.title;
newNotificationBody = notification.payload.body;
});
});

Flutter: How to check if app is running on background from firebase messaging plugins onBackgroundMessage

I'm using firebase_messaging plugin to register a callback handler with onBackgroundMessage for my data-only payload of firebase messaging.
If the app is in foreground or in background, the normal way of operation is using sockets to get the data from network and show notification from the app.
But when the app is in killed state, I would like to show the notification by fetching the data from network.
But these operations conflicts when the app is in background as onBackgroundMessage is getting called in background also.
If I'm not wrong, the handler is running on a separate isolate and it has no access to the main contents.
So how can I differentiate the killed and background state of the app from this isolated function?
You can use IsolateNameServer to register a ReceiverPort from the foreground when it is running and remove it when the foreground is not running. Then on the background isolate check if it exists and if so redirect the FCM message through the port to the foreground for handling on foreground.
Something along the lines of this:
const FOREGROUND_ISOLATE_PORT_NAME = 'foreground_port';
class NotificationManager {
ReceivePort? _foregroundReceivePort;
StreamSubscription<RemoteMessage>? _fcmMessageSubscription;
init() async {
FirebaseMessaging.onBackgroundMessage(_fcmMessageHandlerBackground);
_fcmMessageSubscription = FirebaseMessaging.onMessage.listen(_fcmMessageHandlerForeground);
_foregroundReceivePort = ReceivePort();
IsolateNameServer.registerPortWithName(
_foregroundReceivePort!.sendPort,
FOREGROUND_ISOLATE_PORT_NAME,
);
_foregroundReceivePort!.listen((message) {
if (message is RemoteMessage) {
log('got fcm message for handling in foreground');
_fcmMessageHandlerForeground(message);
}
});
}
shutdown() async {
_fcmMessageSubscription?.cancel();
IsolateNameServer.removePortNameMapping(FOREGROUND_ISOLATE_PORT_NAME);
_foregroundReceivePort!.close();
_foregroundReceivePort = null;
}
}
With these two top level functions:
Future<void> _fcmMessageHandlerForeground(RemoteMessage message) async {
// ... handle message in foreground ...
}
Future<void> _fcmMessageHandlerBackground(RemoteMessage message) async {
final foreground = IsolateNameServer.lookupPortByName(FOREGROUND_ISOLATE_PORT_NAME);
if (foreground != null) {
log("redirecting FCM message to foreground");
foreground.send(message);
} else {
// ... handle message in background ...
}
}

Ionic FCM push notifications observable does not emit

I am doing the following in an ionic application:
When a user is logged in, subscribe to their user ID topic
When a push notification arrives, console.log something.
this.authState.subscribe((state) => {
if (state) {
this.fcmPush.subscribeToTopic(state.uid);
this.fcmPush.onNotification().subscribe(notification => {
if (notification.wasTapped) {
console.log('Received in background', notification);
} else {
console.log('Received in foreground', notification);
}
});
}
});
Source: https://github.com/AmitMY/ionic-starter-firebase/blob/master/src/app/app.component.ts#L118
Sending a notification to my own topic arrives (after a few minutes), but I never see anything in console, both from outside the app, and inside the app.
What am I doing wrong?
Sending a notification from firebase messaging does not work.
However, using the sdk I must add:
click_action: "FCM_PLUGIN_ACTIVITY",
Which is in the usage guide but I missed