EddyVerbruggen / nativescript-plugin-firebase

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

[iOS] Cloud messaging notifications not working #1859

Open Wakko90 opened 2 years ago

Wakko90 commented 2 years ago

Hello, I'm working on a NS/Angular project using Firebase. Push notifications from Firebase Cloud Messaging are not received at all on iOS simulator (I cannot test on a real iOS device at the moment).

Here is my configuration:

"@nativescript/firebase": "^11.1.3", "@nativescript/angular": "~12.0.0", "@nativescript/core": "~8.0.0"

XCode version 13.0 Simulating an iPhone 13 with iOS 15.0

APN Auth Key is uploaded to my Firebase console

This is my firebase.nativescript.json file

{
    "using_ios": true,
    "using_android": true,
    "analytics": true,
    "firestore": true,
    "realtimedb": false,
    "authentication": true,
    "remote_config": false,
    "performance_monitoring": false,
    "external_push_client_only": false,
    "messaging": true,
    "in_app_messaging": true,
    "crashlytics": false,
    "storage": true,
    "functions": true,
    "facebook_auth": false,
    "google_auth": true,
    "admob": false,
    "dynamic_links": false,
    "ml_kit": false
}

Entitlements file is placed in App_Resources/iOS and this is its content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
</dict>
</plist>

the file App_Resources/iOS/Info.plist contains this value

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

I followed this guide to the letter as requested in the plugin documentation. Anyway i couldn't perform the tasks described in the sections "Initialize Firebase in your app" and "Register for remote notifications" but I guess this plugin should handle that.

my main.ts file


  console.log('initializing firebase');
  firebase.init({
    showNotifications: true,
    showNotificationsWhenInForeground: true,
  }).then(() => {
     console.log('Firebase initialized');
  }).catch(err => {
      console.error(err)
  });

runNativeScriptAngularApp({
  appModuleBootstrap: () => platformNativeScript().bootstrapModule(AppModule),
});

my app.component.ts ngOnInit method

try {
      console.log('setting notifications handlers');

      await firebase.addOnPushTokenReceivedCallback(token => {
        console.log('[Firebase] onPushTokenReceivedCallback:', { token });
            this.authService.saveDeviceToken(token);
      })

      await firebase.addOnMessageReceivedCallback(message => {
        console.log('[Firebase] onMessageReceivedCallback:', { message });
      })
    } catch (error) {
      console.error(error);
    }

When I run the application, I correctly receive the push token via the onPushTokenReceivedCallback

I use Postman to make POST calls to Firebase Messaging service, this is what I send:

{
     "notification": {
       "title": "My title",
       "text": "My text",
       "badge": 1,
       "sound": "default"
     },
     "priority": "High",
     "to": "XXXXXXXXXXXXXX"
   }

and this is the response

{
    "multicast_id": xxxxxxxxxxxxxxxx,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "xxxxxxxxxxxxxxxxxxx"
        }
    ]
}

But in the app the OnMessageReceivedCallback is never fired and no popup comes out on the simulator.

Can someone please help me figure out what I'm doing wrong?

Thanks

chanyb commented 2 years ago

now, i faced this issue too

tomups commented 2 years ago

Push notifications don't work in iOS simulator, only on real device:

https://stackoverflow.com/questions/1811900/can-we-check-push-notification-in-simulator

You can trigger them manually for testing i.e. via terminal commands. But real push notifications coming from Firebase will not work.