Push notifications - App crash when app is in foreground


#1

Hi! I’m receiving notifications correctly when the app is on background or closed. But when the app its in foreground, and I send a push notification by firebase, it crashes without no error. Even I receive the object with the notification, I can see it in the console.

This is the code in app.component.ts

 constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private push: Push) {
    platform.ready().then(() => {
      this.push.hasPermission().then((res: any) => {

        if (res.isEnabled) {
          console.log('We have permission to send push notifications');

        } else {
          console.log('We dont have permission to send push notifications');
        }
      });

        const options: PushOptions = {
          android: {
            senderID: '123456789'
          },
          ios: {
        alert: 'true',
        badge: true,
        sound: 'true'
      },
      windows: {}
    };

    const push: PushObject = this.push.init(options);

    push.on('notification').subscribe((notification: any) =>
      console.log('Received a notification', notification)
    );

    push.on('registration').subscribe((registration: any) =>
      console.log('Device registered', registration.registrationId)
    );

    push.on('error').subscribe(error =>
      console.error('Error with Push plugin', error));

});

Hope someone can help. Thank you!


#2

So this is actually successfully executed but then the app crashes, correct?
Anything else in the console that shouldn’t be there?

iOS or Android?
Emulator or real device?


#3

Correct, i receive the console.log correctly with the message, etc. But then it crashes.

At the moment I’m using Android.

The console is not showing any errors, or anything, thats why I dont know why the app is crashing.

This code should be in the app.component.ts? Or anywhere else? I wrote the code there, just to have it when the app launches.


#4

Aren’t there crash log generated when an app crashes? I know HockeyApp offers (native) code to upload them to their server where they are collected and displayed - probably you can do that locally too. I would invest into understanding the crash.


#5

Sorry, crash.log? Where should i search for that log? Never seen one.

The app works correctly if its in background, I dont get it :expressionless:


#6

If the app is in background, the notifications are handled by the OS, not the app. Only when you open the app it gets informed about the notifications that came in (or not even that).


#7

So… the plugin is crashing the app…

But I don’t know how to solve it, because I can’t see any errors, etc… :frowning:


#8

Ok, so I tried deleting the code, and the app crashes anyway when its in foreground and receive a push notification.

Now I’m totally lost…

Even with the addons removed from the app, its still crashing receiving a notification…


#9

I am having the same issue here.
Same code as stated above. When a notification is sent from Firebase and the App is in the foreground,
the Application just crashes without any log. When the App is in the background, the notification is received two times (it appears two times in the top bar on my phone) and they can be clicked and open the application without crashing.


#10

You just described perfectly the issue I’m having, couldn’t say it better.

Actually, if you remove the code, you will get only 1 notification when the app its in the background, but the crash in foreground is still there.


#11

For me it seems, that the code from the plugin can be removed entirely, since the firebase plugin is doing the work for us. Are you using firebase too?
There is also a firebase.messaging() reference, maybe firebase and the push-plugin are somehow interfering each other. Is that a possibility? Could also explain, why this happens to us, but not to everyone.

So there is possibly a way to either override the firebase behaviour, or disable it completly so only the push-plugin is used.
That’s just my theory.


#12

Yes, I also have installed firebase plugin, but I’m not sure if i need it. I can’t try it out without firebase plugin now, but I tried only having firebase plugin, having push plugin removed, and I don’t receive any push notifiactions, so push plugin is required.
Maybe only with push plugin, and removing firebase will solve our issue. If you can try it out will be great!


#13

Well I need my Firebase access for sure, and I tried installing the fcm-plugin, but this raised even more compatibility issues. I am pretty sure by now tho, that this can entirely be solved by the firebase.message() feature, since installing fcm and push results in a lot of problems. I will look into this in more detail tomorrow. For now I suppose I plan on working without the push plugin.

Here I found some related problems to mine. But I will for sure tell you, when I got anything to work :slight_smile:


#14

if you take a look at the guide in the end of that post, he doesnt install firebase, just push notifications from ionic native, so that will be the problem.


#15

Hi everybody, I’ve found the solution:

You must handle it in the app itself

window.FirebasePlugin.onNotificationOpen(function(notification) {
    console.log(notification);
}, function(error) {
    console.error(error);
});

https://www.npmjs.com/package/cordova-plugin-firebase -> source


#16

I am getting following message:

Default FirebaseApp is not initialized in this process.

Any idea how to fix this?
I places the google-services.json file inside the root of my project and am calling the firebase-functions inside > app.component.ts:

platform.ready().then(() => {

this.firebasePlugin.getToken().then(…)
});


#17

The thing is I’m not using firebase plugin to register the token, or handle notifications, just push plugin.


#18

Well I got rid of the Push Plugin now, and installed the native Firebase Plugin (https://ionicframework.com/docs/native/firebase/) too. This is working for me and I can now receive Notifications.


#19

do you mind to share your solution? how do you implement push notification with firebase plugin ?


#20

I use the Push Plugin again, since when I got the Firebase Plugin to work, I was able to replace it with the basic Push Plugin. The important part is, that only one of them is present.