EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 446 forks source link

I do not receive push notifications in the background #1601

Closed calleja23 closed 4 years ago

calleja23 commented 4 years ago

Good afternoon everyone, First of all Thanks Eddy for your plugins and the rest for the maintenance. I can't get background notifications on IOS, on android it works perfectly. it is an angular application My platforms android 6.5.0, ios 6.5.0 My firebase plugin 10.5.2 My xcode 11.4.1 MY ios version 13.4.1 my firebase file config: sorry for formatting the code. I don't know how to put it right { "using_ios": true, "using_android": true, "analytics": false, "firestore": false, "realtimedb": false, "authentication": false, "remote_config": false, "performance_monitoring": false, "external_push_client_only": false, "messaging": true, "in_app_messaging": false, "crashlytics": false, "storage": false, "functions": false, "facebook_auth": false, "google_auth": false, "admob": false, "dynamic_links": false, "ml_kit": false }

My App.component.ts: ` import { Component,OnInit,NgZone } from "@angular/core"; const firebase = require("nativescript-plugin-firebase"); import { alert, confirm } from "tns-core-modules/ui/dialogs"; import as applicationSettings from "tns-core-modules/application-settings"; import { Message, messaging } from "nativescript-plugin-firebase/messaging"; import { Router,NavigationExtras} from '@angular/router'; import as dialogs from "tns-core-modules/ui/dialogs"; @Component({ selector: "ns-app", templateUrl: "./app.component.html" }) export class AppComponent implements OnInit { constructor( private router: Router,private ngZone: NgZone) {}

ngOnInit(): void {
    firebase.init({
        showNotifications: true,
        showNotificationsWhenInForeground: true,
        onPushTokenReceivedCallback: (token) => {
          console.log('[Firebase] onPushTokenReceivedCallback:', { token });
        },
        onMessageReceivedCallback: (message) => {
          console.log('[Firebase] onMessageReceivedCallback:', { message });
          const self=this;
          let contentUrl:string=null;
          let contentType:string=null;
          let route:string="";
          if (message.foreground){
            dialogs.confirm({
              title: message.title,
              message: message.body,
              okButtonText: "open ",
              neutralButtonText: "cancel"
            }).then(function (result) {
              // result argument is boolean
              if(result){
                if (message.data.contentUrl && message.data.contentType) {
                  contentUrl = message.data.contentUrl;
                  contentType = message.data.contentType;
                  if (contentType === 'firma') {
                    route = '/firma/';
                  }
                  console.log("contentUrl",contentUrl);
                  self.router.navigate([route], { queryParams: { url: contentUrl } });
                }
              }
              console.log("Dialog result: " + result);
            });
          }else{
            if (message.contentUrl && message.contentType){
              contentUrl = message.contentUrl ;
              contentType = message.contentType;
              if(contentType==='firma'){
                route ='/firma/';
              }
                let navigationExtras: NavigationExtras = {
                  queryParams: {
                      "url": contentUrl
                  }
              };
              this.ngZone.run(()=>{
                console.log("contentUrl",contentUrl);
                this.router.navigate([route], navigationExtras);
              });
            }
          }
        }
      })
        .then(() => {
          console.log('[Firebase] Initialized');
        })
        .catch(error => {
          console.log('[Firebase] Initialize', { error });
        });
}

} ` the first thing i have done is configure my firebase environment (google.plist, apns key and google.json). Then I have installed the plugin with the checks that I have indicated above. later I have enabled in the info.plist the notifications in back ground. I have added to google's appResources the google.plist and entitlements. finally tns run ios repository: https://github.com/calleja23/pushNotificationIsssue/tree/master/QDevices thanks guys!

bylmzio commented 4 years ago

Did you have the key content_available: true key in your notifcation? Did you set following in your info.plist?

<key>UIBackgroundModes</key>
<array>
  <string>remote-notification</string>
</array>

https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#allow-processing-when-a-background-push-is-received

calleja23 commented 4 years ago

@bunower Hi! yes, I have it added in the file and checked in the xcode. I just ran a test correcting this: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#what-if-ios-doesnt-showreceive-notifications-in-the-background and I already receive notifications in the background. I would have to adapt it to my application.

I have had to remove the component initialization from the app.component.ts file and bring it to the first component of my application. and keep the require firebase in the main.ts. Thank you and really sorry for the inconvenience @EddyVerbruggen continues like this !! @bunower thanks