I am developing an ionic4 app for both android & iOS, I am getting notifications but it does not call Firebase.onnotification()
,but when I tap on notification it will call function only app in background. But,app in foreground(running) state when I click on a notification nothing happens.
//this will get called when the app initialises
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleLightContent();
this.splashScreen.hide();
this.backButtonEvent();
if (this.platform.is('cordova')) {
// Initialize push notification feature
if (this.platform.is('android') === true) {
this.notificationSetup();
}
} else {
console.log('Push notifications are not enabled since this is not a real device');
}
}
this method does not get invoked at any time
private notificationSetup() {
debugger
console.log('notification setup');
this.notificationsService.getToken();
this.firebase.onNotification().subscribe(
(msg: NotificationData) => {
console.log('notification msg', JSON.stringify(msg));
if (msg.wasTapped) {
//this will work on background state
console.log('Received in background');
} else {
//this will never call
console.log('Received in foreground');
}
}, error => console.error('Error with Push plugin', error));
}
and notification service class
export class NotificationsService {
constructor(private firebase: FCM,
private platform: Platform) { }
async getToken() {
let token;
if (this.platform.is('android')) {
token = await this.firebase.getToken();
console.log('fcm token', token);
}
// this.saveToken(token);
}
topicSubscription(topic) {
this.firebase.subscribeToTopic(topic).then((res:any) => {
console.log('Subscribed to topic: ' + topic, res);
});
}
topicUnsubscription(topic) {
this.firebase.unsubscribeFromTopic(topic).then((res:any) => {
console.log('Unsubscribed from topic: ' + topic, res)
});
}
onNotifications() {
console.log("onnotification method")
debugger
return this.firebase.onNotification();
}
}
I am using this
import { FCM } from '@ionic-native/fcm/ngx';
package.json file -
{
"name": "Appname",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/common": "~8.1.2",
"@angular/core": "~8.1.2",
"@angular/forms": "~8.1.2",
"@angular/platform-browser": "~8.1.2",
"@angular/platform-browser-dynamic": "~8.1.2",
"@angular/router": "~8.1.2",
"@ionic-native/android-permissions": "^5.21.5",
"@ionic-native/calendar": "^5.0.0-beta.14",
"@ionic-native/clipboard": "^5.20.0",
"@ionic-native/core": "^5.17.1",
"@ionic-native/date-picker": "^5.19.1",
"@ionic-native/fcm": "^5.21.5",
"@ionic-native/file": "^5.21.5",
"@ionic-native/file-transfer": "^5.21.5",
"@ionic-native/firebase": "^5.21.4",
"@ionic-native/keyboard": "^5.17.1",
"@ionic-native/native-storage": "^5.17.1",
"@ionic-native/secure-storage": "^5.17.1",
"@ionic-native/splash-screen": "^5.17.1",
"@ionic-native/status-bar": "^5.17.1",
"@ionic-native/wheel-selector": "^5.19.1",
"@ionic/angular": "^4.11.5",
"@ionic/storage": "^2.2.0",
"@types/crypto-js": "^3.1.43",
"@types/hammerjs": "^2.0.36",
"bootstrap": "^4.4.1",
"cordova": "^9.0.0",
"cordova-android": "^8.1.0",
"cordova-browser": "^6.0.0",
"cordova-plugin-android-permissions": "^1.0.2",
"cordova-plugin-androidx": "^1.0.2",
"cordova-plugin-androidx-adapter": "^1.1.0",
"cordova-plugin-datepicker": "^0.9.3",
"cordova-plugin-fcm-with-dependecy-updated": "^4.1.1",
"cordova-plugin-file": "^6.0.2",
"cordova-plugin-file-transfer": "^1.7.1",
"cordova-plugin-firebase-lib": "^5.1.1",
"cordova-plugin-firebase-messaging": "4.0.1",
"cordova-plugin-firebasex": "^7.0.2",
"cordova-plugin-ionic-keyboard": "^2.2.0",
"cordova-plugin-nativestorage": "^2.3.2",
"cordova-plugin-splashscreen": "^5.0.3",
"cordova-support-android-plugin": "^1.0.1",
"cordova-support-google-services": "^1.3.2",
"cordova-wheel-selector-plugin": "^1.1.4",
"core-js": "^2.6.11",
"crypto-js": "^3.1.9-1",
"hammerjs": "^2.0.8",
"ionic": "^5.4.16",
"ionic-long-press": "^2.0.2",
"lodash": "^4.17.15",
"native-run": "^0.3.0",
"rxjs": "^6.5.4",
"rxjs-compat": "^6.5.4",
"tslib": "^1.9.0",
"web-animations-js": "^2.3.2",
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-devkit/architect": "~0.801.2",
"@angular-devkit/build-angular": "^0.803.24",
"@angular-devkit/core": "~8.1.2",
"@angular-devkit/schematics": "~8.1.2",
"@angular/cli": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/compiler-cli": "~8.1.2",
"@angular/language-service": "~8.1.2",
"@ionic/angular-toolkit": "~2.0.0",
"@ionic/lab": "^2.0.19",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/lodash": "^4.14.149",
"@types/node": "~8.9.4",
"codelyzer": "^5.2.1",
"cordova-plugin-device": "^2.0.3",
"cordova-plugin-ionic-webview": "^4.1.3",
"cordova-plugin-statusbar": "^2.4.3",
"cordova-plugin-whitelist": "^1.3.4",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.5.1",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.4.3"
},
"description": "An Ionic project",
"cordova": {
"plugins": {
"cordova-plugin-whitelist": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-device": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
},
"cordova-plugin-nativestorage": {},
"cordova-plugin-splashscreen": {},
"cordova-plugin-ionic-keyboard": {},
"cordova-plugin-datepicker": {},
"cordova-wheel-selector-plugin": {},
"cordova-plugin-firebasex": {
"FIREBASE_ANALYTICS_COLLECTION_ENABLED": "true",
"FIREBASE_PERFORMANCE_COLLECTION_ENABLED": "true",
"FIREBASE_CRASHLYTICS_COLLECTION_ENABLED": "true",
"ANDROID_ICON_ACCENT": "#FF00FFFF",
"ANDROID_PLAY_SERVICES_TAGMANAGER_VERSION": "17.0.0",
"ANDROID_FIREBASE_ANALYTICS_VERSION": "17.2.1",
"ANDROID_FIREBASE_MESSAGING_VERSION": "20.0.0",
"ANDROID_FIREBASE_CONFIG_VERSION": "19.0.3",
"ANDROID_FIREBASE_PERF_VERSION": "19.0.1",
"ANDROID_FIREBASE_AUTH_VERSION": "19.1.0",
"ANDROID_CRASHLYTICS_VERSION": "2.10.1",
"ANDROID_CRASHLYTICS_NDK_VERSION": "2.1.1"
},
"cordova-plugin-firebase-lib": {},
"cordova-plugin-firebase-messaging": {
"FIREBASE_MESSAGING_VERSION": "20.1.+",
"FIREBASE_ANALYTICS_VERSION": "17.2.+",
"ANDROIDX_CORE_VERSION": "1.0.+"
},
"cordova-plugin-file": {},
"cordova-plugin-file-transfer": {},
"cordova-plugin-android-permissions": {}
},
"platforms": [
"browser",
"android"
]
}
}
log for when app in background when user tap on notification from android studio-
FCMPlugin: ==> FCMPluginActivity onCreate
2020-02-19 11:17:12.441 7860-7860/ D/FCMPlugin: ==> USER TAPPED NOTFICATION
2020-02-19 11:17:12.441 7860-7860/ D/FCMPlugin: Key: google.delivered_priority Value: high
2020-02-19 11:17:12.442 7860-7860/D/FCMPlugin: Key: google.sent_time Value: 1582091222156
2020-02-19 11:17:12.442 7860-7860/D/FCMPlugin: Key: google.ttl Value: 2419200
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: google.original_priority Value: high
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: alarmId Value: 15
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: msg Value: alarm assgned to you
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: body Value: bbd started
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: from Value: /topics/all
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: title Value: dfbdfb CORDOVA
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: google.message_id Value: 0:15
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: Key: notificationType Value: user
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: Key: collapse_key Value: xxx
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: ==> FCMPlugin sendPushPayload
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: notificationCallBackReady: true
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: gWebView: org.apache.cordova.CordovaWebViewImpl@5fff06b
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: msg => alarm assgned to you
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: wasTapped => true
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.original_priority => high
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.sent_time => 1582091222156
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.delivered_priority => high
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: notificationType => user
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: body => bfdb started
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: title => fbdf CORDOVA
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.message_id => 0:1
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: collapse_key => xxx
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: alarmId => 15
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.ttl => 745
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: from => /topics/all
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: Sent PUSH to view: javascript:FCMPlugin.onNotificationReceived({"msg":"alarm assgned to you ","wasTapped":true,"google.original_priority":"high","google.sent_time":15,"google.delivered_priority":"high","notificationType":"user","body":"RORM started","title":"784 CORDOVA","google.message_id":"0:1","collapse_key":"xxx","alarmId":"1","google.ttl":2,"from":"\/topics\/all"})
log for when app in foreground when user tap on notification (from android studio)
2020-02-19 12:12:40.990 11337-11528/xxxx I/FirebasePlugin: Received message: notification
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: From: /topics/all
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Id: 0:5435325111132
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Title: gy8u CORDOVA
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Body: oyb started
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Sound: default
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Vibrate: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Light: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Color: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Icon: fcm_push_icon
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Channel Id: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Visibility: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Priority: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: sendMessage(): messageType=notification; showNotification=true; id=0:475%; title=ou CORDOVA; body=uin started; sound=default; vibrate=null; light=null; color=null; icon=fcm_push_icon; channel=null; data={alarmId=1, msg=alarm assgned to you , body=uybub started, title=uyb CORDOVA, notificationType=user}
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Channel ID: fcm_default_channel
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Small icon: application
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Large icon: application
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Color: default
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Visibility: 1
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Priority: 2
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: show notification: Notification(channel=fcm_default_channel pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0xff00ffff vis=PUBLIC)
2020-02-19 12:13:10.016 11337-11337/xxxx D/FirebasePlugin: OnNotificationOpenReceiver.onReceive(): Bundle[{alarmId=96, messageType=notification, id=0:iu90%, msg=alarm assgned to you , tap=foreground, ttl=2419200, body=uvyy started, from=/topics/all, icon=fcm_push_icon, sound=default, title=vui CORDOVA, sent_time=1582094560869, notificationType=user, collapse_key=xxxx, show_notification=true}]
2020-02-19 12:13:10.019 11337-11337/xxxx D/CordovaActivity: Paused the activity.
2020-02-19 12:13:10.020 11337-11337/xxxx D/CordovaActivity: Resumed the activity.
based on log while app in foreground mode it will not call FCMPluginActivity onCreate. but it works in background mode only. please give me the solution or suggestion for this
when app in foreground you can use this,add this in app initialisation method(app.component.ts) it will work-
(<any>window).FirebasePlugin.onMessageReceived((message) => {
try {
console.log(`Notification received - ${message}`);
}
catch (error) {
console.error('error notification receive', error);
}
});