Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Callback onMessage is never called on Firebase Messaging for iOS #24

Closed
CatchABus opened this issue Dec 8, 2021 · 14 comments
Closed

Comments

@CatchABus
Copy link
Contributor

CatchABus commented Dec 8, 2021

Even though I receive notifications when app is in background, onMessage callback is never triggered, neither on foreground nor on background.
This works on android but not on iOS. I use iOS 15.

@DieOver
Copy link

DieOver commented Dec 8, 2021

Even though I receive notifications when app is in background, onMessage callback is never triggered, neither on foreground nor on background. This works on android but not on iOS. I use iOS 15.

I'm having exacly the same issue, i can receive push when background, but not when foreground throght onMessage method..

searching the plugins code, i see that a need to set showNotificationsWhenInForeground to true.. but not working anyway...

I'm using iOS 14 and 15

@williamjuan027
Copy link
Member

@triniwiz I also ran into this issue and did some more testing to try to figure out the root cause of the problem. Here are a few of my findings.

  1. If the app is in the background or closed, I get the notification. However, tapping on the notification to open the app doesn't trigger any callback (I think the previous firebase plugin calls the onMessage callback when this happens).
  2. When the app is in the foreground, the notification isn't displayed and onMessage is never called (both with firebase().messaging().showNotificationsWhenInForeground set to true and false)
  3. I tested with and without the Firebase Authentication plugin, both yielding the same result as above.

Further investigation, I found that only 3 methods from the Firebase Messaging plugin's AppDelegate is being called:

  • applicationDidRegisterForRemoteNotificationsWithDeviceToken
  • sharedInstance getter
  • observe

None of the methods from UNUserNotificationCenterDelegate is ever called.

I used the latest version that was released today (alpha36) to test these.

Hope that helps :)

@shirakaba
Copy link
Collaborator

shirakaba commented Jan 21, 2022

Just a quick uninformed thought, though it may be completely off the mark. Firebase relies on a lot of swizzling of the AppDelegate; does NativeScript do anything odd with its own handling of AppDelegate that might cause the swizzling to fail?

@triniwiz
Copy link
Member

I think might be a matter of when firebase().messaging() is called since (in this demo it’s called before the app launches) it sets up a couple things

@williamjuan027
Copy link
Member

@triniwiz It turns out to be the initialization issue. In an Angular project, initializing it in the main.ts resolves the issue. Below is the code I used to initialize the plugin.

// main.ts

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

const firebaseInit = firebase().initializeApp();
if (firebaseInit) {
  firebase().messaging();
}

@CatchABus
Copy link
Contributor Author

@triniwiz It turns out to be the initialization issue. In an Angular project, initializing it in the main.ts resolves the issue. Below is the code I used to initialize the plugin.

// main.ts

import { firebase } from '@nativescript/firebase-core';
import '@nativescript/firebase-messaging';

const firebaseInit = firebase().initializeApp();
if (firebaseInit) {
  firebase().messaging();
}

What about apps that need messaging only after user authentication?

@triniwiz
Copy link
Member

triniwiz commented Feb 4, 2022

Please update to the latest version

@felixkrautschuk
Copy link

@triniwiz I have a similar problem on iOS, even after installing the latest firebase-messaging plugin (1.0.0-alpha.41).

When testing the push notifications in Simulator using an apns file like this:

{
    "Simulator Target Bundle": "np.com.sagunrajlage.TestPushNotifications",
    "aps": {
        "alert": "Push Notifications Test",
        "sound": "default",
        "badge": 1
    }
}

(which is descriobed here https://betterprogramming.pub/how-to-send-push-notifications-to-the-ios-simulator-2988092ba931), then I see the notification in Simulator in all app states (foreground, background and closed).
BUT: the onMessage callback is never called and when tapping the notification, the onNotificationTap callback is not called as well.

When testing the push notifications on a real iOS device with our real server or sending a message via Firebase Console directly to the device, I do not even see a notification (no matter which state the app has) and no callback is called.
When doing the same stuff using the old nativescript firebase plugin, everything works as expected on iOS.

On Android the new firebase messaging plugin seems to work as expected.

@triniwiz
Copy link
Member

triniwiz commented Feb 4, 2022

@felixkrautschuk I didn't know about the sim notifications , I can see this being an issue since there are some checks in the code that may cause issues with the sim as for the issue with the real device does the app use any custom delegates by chance ?

@triniwiz
Copy link
Member

triniwiz commented Feb 4, 2022

Another thing please try calling firebase().initializeApp(); before the app launches

@felixkrautschuk
Copy link

@triniwiz thanks for your reply. I uncommented the custom delegate. And I moved the firebase().initializeApp(); to the beginning of app.js but unfortunately this does not help.

@triniwiz
Copy link
Member

triniwiz commented Feb 4, 2022

Do you have a sample app of how your app is setup ? The best I can do w/o an app is point you to the demo app in this project

@felixkrautschuk
Copy link

felixkrautschuk commented Feb 4, 2022

@triniwiz I believe it works now on my side, even with our custom app delegate and with initializing Firebase in the app launch event.

The only change: I added the following line after initializing firebase:

firebase().messaging().registerDeviceForRemoteMessages();

I did not find this method in the docs but my IDE suggested this method from the plugins API ;)
When adding this line, notifications are shown as expected and both callbacks are triggered.

@triniwiz
Copy link
Member

triniwiz commented Feb 4, 2022

You should call that register after requesting permission

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants