hansemannn / titanium-firebase-cloud-messaging

Use the Firebase Cloud Messaging SDK in Axway Titanium 🚀 Edit
Other
43 stars 31 forks source link

Can't obtain Android FCM Token #146

Closed ikruze closed 1 year ago

ikruze commented 1 year ago

Hi, I'm trying to register for Push Notifications for Android (iOS works fine!) but there is no response from FCM, when I execute FirebaseCloudMessaging.registerForPushNotifications() I see this debug message:

FirebaseCloudMessaging: (main) [20297,22063] Empty notification in Intent

And there is no response back from the registration

I've tried with Titanium SDK 12.0.0.GA, 11.1.1.GA and now 12.1.0.v20230224211003, always the same. Also disabled Firebase Core module for Android and this are the versions of the other modules I'm using:

ti.playservices 18.2.0 firebase.cloudmessaging 3.3.1 firebase.analytics 5.1.0

Google Play Services is enabled and the versión listed in the debug is 12451000

In my Firebase console I've configured the Android project adding both SHA1 and SHA256 signatures for the APK that are generated (debug and production, even the signed by Google Play). Downloaded that google-services.json and placed in my project Resources folder and even inside the Resources/Android folder.

My google-services.json file looks like this:

{
  "project_info": {
    "project_number": "1030XXXXXXXXX",
    "project_id": "myapp-c5XXX",
    "storage_bucket": "myapp-c5XXX.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:1030XXXXXXXXX:android:f288YYYYYYYYYYYYYY",
        "android_client_info": {
          "package_name": "package.mine"
        }
      },
      "oauth_client": [
        {
          "client_id": "1030XXXXXXXXX-40a8XXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "package.mine",
            "certificate_hash": "47afXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          }
        },
        {
          "client_id": "1030XXXXXXXXX-98niXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "package.mine",
            "certificate_hash": "33b8fXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          }
        },
        {
          "client_id": "1030XXXXXXXXX-mtrrXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "package.mine",
            "certificate_hash": "1bfffXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          }
        },
        {
          "client_id": "1030XXXXXXXXX-v4dmXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
          "client_type": 1,
          "android_info": {
            "package_name": "package.mine",
            "certificate_hash": "cce3fXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          }
        },
        {
          "client_id": "1030XXXXXXXXX-opg9XXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXX"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "1030XXXXXXXXX-opg9XXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
              "client_type": 3
            },
            {
              "client_id": "1030XXXXXXXXX-8nb2XXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com",
              "client_type": 2,
              "ios_info": {
                "bundle_id": "package.mine"
              }
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

My production app SHA1 signature is the one starting with 47:AF and the debug one starts with CC:E3

Finally, my code runs at the first Window creation and it's based on your documentation:

const FirebaseCloudMessaging = require('firebase.cloudmessaging');
FirebaseCloudMessaging.addEventListener('didRefreshRegistrationToken', onToken);
FirebaseCloudMessaging.addEventListener('didReceiveMessage', function(e) {
Ti.API.info('Message', e.message);
});
FirebaseCloudMessaging.addEventListener("error", function(e) {
Ti.API.info("Error en FirebaseCloudMessaging register");
});

function onToken(e)
{
  Ti.API.info("RECIBI TOKEN "+e.fcmToken);
}

if (ANDROID_OS)
{
    const channel = Ti.Android.NotificationManager.createNotificationChannel({
        id: 'mypushID', // if you use a custom id you have to set the same to the `channelId` in you php send script!
        name: 'mypushName',
        importance: Ti.Android.IMPORTANCE_DEFAULT,
        enableLights: true,
        enableVibration: true,
        showBadge: true
    });
    FirebaseCloudMessaging.notificationChannel = channel;
    Ti.API.info(`Last data: ${FirebaseCloudMessaging.lastData}`);
    requestPushPermissions();
}
else {
    Ti.App.iOS.addEventListener('usernotificationsettings', function eventUserNotificationSettings() {
        // Remove the event again to prevent duplicate calls through the Firebase API
        Ti.App.iOS.removeEventListener('usernotificationsettings', eventUserNotificationSettings);
        requestPushPermissions();
    });

    // Register for the notification settings event
    Ti.App.iOS.registerUserNotificationSettings({
    types: [
        Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
        Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
        Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
    ]
    });
}

function requestPushPermissions() {
        // Register for push notifications
            Ti.Network.registerForPushNotifications({
                success: function(e) {
                    // Register the device with the FCM service.
                    if (ANDROID_OS) {
                        Ti.API.info("// register for a token");
                        FirebaseCloudMessaging.registerForPushNotifications();
                    } else {
                        // iOS
                        onToken(e);
                    }
                },
                error: function(e) {
                    Ti.API.error(e);
                },
                callback: function(e) {
                    // Fired for all kind of notifications (foreground, background & closed)
                    Ti.API.info(e.data);
                }
            });
    }

After FirebaseCloudMessaging.registerForPushNotifications(); I have no more response!, one year ago it worked fine with other apps but now I don't know what else to check out since I've looked everything, I will appreciate your help with it

m1ga commented 1 year ago

Please verify the json folder: https://github.com/hansemannn/titanium-firebase#installation

Download the google-services.json and copy it to [application_name]/platform/android/ (classic) or [application_name]/app/platform/android/ (Alloy)

m1ga commented 1 year ago

Quick note about the log:

https://github.com/hansemannn/titanium-firebase-cloud-messaging/blob/38939651a23c0b404081457052116d6a0358a2b3/android/src/firebase/cloudmessaging/CloudMessagingModule.java#L374

It just checks if there was an intent and since there isn't in this case it will show the debug log

ikruze commented 1 year ago

Man, thanks!!! this was the solution, you made my day!!!

[application_name]/platform/android/

m1ga commented 1 year ago

no problem! Since Titanium uses gradle (Ti 9) it will automatically find and use the file in those folders