dpa99c / cordova-plugin-firebasex

Cordova plugin for Google Firebase
MIT License
570 stars 468 forks source link

No tap property on Background use case - iOS Ionic 4 app #96

Closed Sampath-Lokuge closed 5 years ago

Sampath-Lokuge commented 5 years ago

Hi,

There is no tap property on iOS device background use case. Hence it shows the notification 2 times. i.e. it always fires // Received in foreground section on fcmListeners() method. i.e. it shows system tray notification and custom notification where I have created for a foreground use case.

Note: Here I use firebase console dashboard to send the push. i.e. no API call

FCM dashboard custom property: https://imgur.com/cE9Yxwk

Note: No issues on the Killed use case. i.e. it doesn't fire the custom notification. only the system tray notification shows. This is the behavior where I need background use case too.

Note: No issues on Android device. i.e. it works fine on all 3 use cases.

service.ts

  async init() {
    if (this.platform.is('android')) {
      this.fcmToken = await this.firebase.getToken();
    } else if (this.platform.is('ios')) {
      const res = await this.firebase.hasPermission();
      if (!res.isEnabled) {
        await this.firebase.grantPermission();
        this.fcmToken = await this.firebase.getToken();
      }
    }
    this.fcmListeners();
    console.log('this.fcmToken', this.fcmToken);
  }

  fcmListeners() {
    this.firebase.onMessageReceived().subscribe(async (data: any) => {
      if (data.tap === 'background') {// when user tapped the background notification
        console.log('background notification', JSON.stringify(data, null, 4));
      } else { // Received in foreground
        console.log('Received in foreground', JSON.stringify(data, null, 4));
        this.showToastService.showFcmToast(data.title, data.message, data); // custom toast
      }
    });
  }

app.componet.ts

initializeApp() {
    this.platform.ready().then(() => {
      if (this.platform.is('cordova')) {
        this.firebaseCloudMessagingService.init(); // fcm
      }
    });
  }

package.json

"cordova-plugin-firebasex": "^6.0.7",

ionic info

Ionic:

   Ionic CLI                     : 5.2.4
   Ionic Framework               : @ionic/angular 4.7.4
   @angular-devkit/build-angular : 0.801.3
   @angular-devkit/schematics    : 8.1.3
   @angular/cli                  : 8.1.3
   @ionic/angular-toolkit        : 2.0.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : ios 5.0.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 8 other plugins)

Utility:

   cordova-res : not installed
   native-run  : not installed

System:

   ios-deploy : 1.9.4
   ios-sim    : 8.0.1
   NodeJS     : v10.14.2 (/usr/local/bin/node)
   npm        : 6.5.0
   OS         : macOS High Sierra
   Xcode      : Xcode 10.1 Build version 10B61

This is the payload comes when taps the system notification. i.e. it always goes to the // Received in foreground section on fcmListeners() method.

{
    "google.c.a.e": "1",
    "aps": {
        "alert": {
            "title": "Firebase Push",
            "body": "Test Firebase Push"
        }
    },
    "title": "Firebase Push",
    "gcm.n.e": "1",
    "google.c.a.c_id": "4193883769168521163",
    "message": "Test Firebase Push",
    "google.c.a.udt": "0",
    "gcm.message_id": "1566044177059408",
    "google.c.a.ts": "1566044177",
    "messageType": "notification"
}
brandPittCode commented 5 years ago

I have exactly the same problem with Ionic4; there is no tap property for iOS. I've also set notification_foreground: 'true', but it doesn't show the native notification. In Android it works well.

Config:

"cordova": "^9.0.0",
"cordova-android": "8.0.0",
"cordova-ios": "5.0.1",
"cordova-plugin-firebasex": "6.0.6",
papattes commented 5 years ago

Hi ! I had the same problem. Looking at the logs I noticed that in the didReceiveRemoteNotification () method the TAP variable was not instantiated.

This piece of code must be added to the AppDelegate+FirebasePlugin.m file at line 150:

NSString* tap;
if([self.applicationInBackground isEqual:[NSNumber numberWithBool:YES]]){
    tap = @"background";
}else{
    tap = @"foreground";

}
[mutableUserInfo setValue:tap forKey:@"tap"];
if([mutableUserInfo objectForKey:@"messageType"] == nil){
    [mutableUserInfo setValue:@"notification" forKey:@"messageType"];
}
Horst1102 commented 5 years ago

I can confirm the problem.

@papattes solution fixes the problem.

Sampath-Lokuge commented 5 years ago

@papattes Can't you create a PR for your solution?

papattes commented 5 years ago

@Sampath-Lokuge I created it, however, it added it on the arnesson/cordova-plugin-firebase repo. When I click that new PR, I am redirected automatically to this repository. How to do ?