Hi Guys
I am experiencing 2 issues with FCM on Ionic:
1.) I have developed a FCM application which is running on my android device,
I only receive FCM notifications (sent via postman) when my app is closed, and when I tap on the notification, nothing happens, however I have specified in my code to redirect to a specific page, this does not happen.
2.) Secondly, When the app is active and running and I send a push notification via postman, nothing happens at all, nothing is logged to the console, and this.fcm.onNotification().subscribe function is not even firing from my app.component.ts.
Below is the JSON that gets pushed via C#:
var data = new
{
to = device_Id,
notification = new
{
title = title,
body = message,
sound = “default”,
click_action = “FCM_PLUGIN_ACTIVITY”,
icon = “fcm_push_icon”
},
data = new
{
landing_page = “second”,
messageText = title.Replace(’~’, ‘-’) + “~” + message.Replace(’~’, ‘-’) + “~” + sFullName.Replace(’~’, ‘-’)
}
};
and below is the code in my app.component.ts
import { Component } from ‘@angular/core’;
import { Platform } from ‘@ionic/angular’;
import { SplashScreen } from ‘@ionic-native/splash-screen/ngx’;
import { StatusBar } from ‘@ionic-native/status-bar/ngx’;
import { FCM } from ‘@ionic-native/fcm/ngx’;
import { Router } from ‘@angular/router’;
import { Token } from ‘src/Token’;
@Component({
selector: ‘app-root’,
templateUrl: ‘app.component.html’,
styleUrls: [‘app.component.scss’]
})
export class AppComponent {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private fcm: FCM,
private router: Router,
private tokenKey: Token
) {
this.initFCM();
}
initFCM() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.fcm.subscribeToTopic(‘marketing’);
this.fcm.getToken().then(token => {
this.tokenKey.tokenId = token;
console.log("Token", token);
});
this.fcm.onNotification().subscribe(data => {
if (data.wasTapped) {
console.log("Received in background");
this.router.navigate([data.landing_page, data.messageText]);
} else {
console.log("Received in foreground");
this.router.navigate([data.landing_page, data.messageText]);
};
});
this.fcm.onTokenRefresh().subscribe(token => {
this.tokenKey.tokenId = token;
console.log("Token", token);
});
this.fcm.hasPermission().then(hasPermission => {
if (hasPermission) {
console.log("Has permission!");
}
})
this.fcm.clearAllNotifications();
this.fcm.unsubscribeFromTopic('marketing');
})
}
}
Could someone please assist me as I have scoured the net looking for answers and I cant seem to find an answer.
Thanks in advance guys!