Cannot resolve method 'setPushNotificationsReg

2019-06-09 14:37发布

问题:

I am implementing push notifications in Android with Firebase Cloud Messaging, and I am implementing Facebook Push Notifications and Facebook In-App Notifications too.

I am following the instructions at https://developers.facebook.com/docs/push-notifications/android to set up Push Campaigns for Android. I already completed the instructions at https://firebase.google.com/docs/cloud-messaging/android/client for setting up a Firebase Cloud Messaging Client App on Android.

This is the content of my MyFirebaseMessagingService.java file:

/**
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.[myapp];
import android.util.Log;

import com.facebook.notifications.internal.appevents.AppEventsLogger;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG = "MyFirebaseIIDService";
    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. This call is initiated by the
     * InstanceID provider. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        try {
            // Get updated InstanceID token.
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            AppEventsLogger.setPushNotificationsRegistrationId(refreshedToken);
            Log.d(TAG, "Refreshed token: " + refreshedToken);
            // If you want to send messages to this application instance or
            // manage this apps subscriptions on the server side, send the
            // Instance ID token to your app server.
            sendRegistrationToServer(refreshedToken);
            // TODO: Implement this method to send any registration to your app's servers.
            System.out.println("Registration.onTokenRefresh TOKEN: " + refreshedToken );
        } catch (Exception e) {
            Log.e("test", "Failed to complete token refresh", e);
        }
    }
    // [END refresh_token]
    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }
}

This is the line that is generating an error for me:

AppEventsLogger.setPushNotificationsRegistrationId(refreshedToken);

The error is: Cannot resolve method 'setPushNotificationsRegistrationId(java.lang.String)' and you can see it in the following image:

I thought the Facebook libraries should already be taking care of importing that method. I am using compile 'com.facebook.android:facebook-android-sdk:3.23.1' in my dependencies and also compile 'com.facebook.android:notifications:1.+'. Do you have any ideas about what may be causing this error? Maybe I need a newer version of the Facebook SDK? I will appreciate any hints and ideas to fix this error. Thank you.

EDIT 1: I already tried using compile 'com.facebook.android:facebook-android-sdk:4.20.0', but I still see the same error.

EDIT 2: I found the following information at https://developers.facebook.com/docs/android/change-log-4x:

4.11.0 - April 12, 2016 Facebook SDK Added AppEventsLogger.setPushNotificationsRegistrationId and AppEventsLogger.logPush*.

That means that AppEventsLogger.setPushNotificationsRegistrationId did not exist before Facebook SDK 4.11.0, and I am using Facebook SDK 3.23.1. It makes sense that the method setPushNotificationsRegistrationId() is not recognized. But why is it that even when I use compile 'com.facebook.android:facebook-android-sdk:4.11.0' in my dependencies and try to compile the app, I still see the same error?

EDIT 3: I am examining the documentation at https://developers.facebook.com/docs/reference/android/current/class/AppEventsLogger/ to learn more about the AppEventsLogger class, and I can see that the setPushNotificationsRegistrationId(String) method is listed with the following description: Sets a registration id to register the current app installation for push notifications. What I am using to have access to this class and method is: import com.facebook.notifications.internal.appevents.AppEventsLogger;. I do not see at https://developers.facebook.com/docs/reference/android/current/class/AppEventsLogger/ any restrictions or documentation about a minimum Facebook SDK or additional requirements to be able to use the setPushNotificationsRegistrationId() method that I was expecting to have available from the class AppEventsLogger that I am using.

EDIT 4: In order to use AppEventsLogger.setPushNotificationsRegistrationId(refreshedToken);, do I need to import import com.facebook.notifications.internal.appevents.AppEventsLogger; or import com.facebook.AppEventsLogger;? In other portions of my code, I have used for example AppEventsLogger logger = AppEventsLogger.newLogger(getApplication()); with import com.facebook.AppEventsLogger;. What would be the difference between using import com.facebook.notifications.internal.appevents.AppEventsLogger; versus import com.facebook.AppEventsLogger;? Thank you.

回答1:

The setPushNotificationsRegistrationId() method did not exist in Facebook SDK 3.23.1: https://developers.facebook.com/docs/reference/android/3.23.1/class/AppEventsLogger/. It was introduced from Facebook SDK 4.11: https://developers.facebook.com/docs/reference/android/4.11/class/AppEventsLogger/ and https://developers.facebook.com/docs/android/change-log-4x.