Not getting notification using Firebase in Eclipse

2020-08-01 06:04发布

问题:

I've got Firebase building without any warnings using Eclipse (see related: Unable to find obfuscated Firebase class in Eclipse)

I try to send a test notification, however I'm not getting anything. I'm using a library project as well. This is my code:

AndroidManifest.xml

<application />
    ... 
    <!-- ==================================
               FIREBASE
    =================================== -->
    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <service
        android:name=".MyFirebaseInstanceIdService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>
....
</application>

These files are practically straight from the samples:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    public static final String PUSH_NOTIFICATION_TEXT = "pushnotificationtext";

    private static final String TAG = "Firebase";

    public static final int NOTIFICATION_ID = 1;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // Handle data payload of FCM messages.
        String messageId = remoteMessage.getMessageId();
        RemoteMessage.Notification notification = remoteMessage.getNotification();
        Map<String, String> data = remoteMessage.getData();

        Log.d(TAG, "FCM Message Id: " + messageId);
        Log.d(TAG, "FCM Notification Message: " + notification);
        Log.d(TAG, "FCM Data Message: " + data);

        sendNotification(this, notification.toString());

   }

    // Put the GCM message into a notification and post it.
    private void sendNotification(Context context, String message) {

        NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        final Intent intent = new Intent(context, Splash.class);
        //intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        //Save push notification message to show when app starts
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putString(PUSH_NOTIFICATION_TEXT, message).commit();

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setVibrate(new long[] { 0, 500, 200, 500, 200, 500 })
            .setContentIntent(contentIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message)
            .setWhen(System.currentTimeMillis()).setOngoing(false);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

}

Other class:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    private static final String TAG = "Firebase";
    private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage";

    /**
    * The Application's current Instance ID token is no longer valid 
    * and thus a new one must be requested.
    */
    @Override
    public void onTokenRefresh() {
        // If you need to handle the generation of a token, initially or
        // after a refresh this is where you should do that.
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "FCM Token: " + token);

        // Once a token is generated, we subscribe to topic.
        FirebaseMessaging.getInstance().subscribeToTopic(FRIENDLY_ENGAGE_TOPIC);
    }
}

I send a message via the Firebase console targeting the package name with no luck. I'm uncertain what I should do to get this to work. Do I need to use the google-services.json file in some way?

Thanks

回答1:

I don't use Eclipse and am not able to verify that these steps work. I hope they will get you started on the right path.

The Firebase framework is initialized by FirebaseInitProvider. You don't need to implement or subclass it. Just declare it in your manifest.

    <provider
        android:authorities="${applicationId}.firebaseinitprovider"
        android:name="com.google.firebase.provider.FirebaseInitProvider"
        android:exported="false"
        android:initOrder="100" />

FirebaseInitProvider expects to find string resources that contain the configuration values for the project. In an app built with Android Studio and the Google Services Gradle Plugin, the resource values are created from the google-services.json file. Because you are not using AS and the plugin, you will need to define these resources in your res folder. The values can be found in your Project Settings at the Firebase Console or in the google-service.json file. Because you are only interested in messaging, some values may not be needed. To be safe, define them all initially.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id" translatable="false">8888888888888-ooqodhln4cjj4qst7b4sadfiousdf7.apps.googleusercontent.com</string>
    <string name="firebase_database_url" translatable="false">https://project-888888888888888.firebaseio.com</string>
    <string name="gcm_defaultSenderId" translatable="false">888888888888</string>
    <string name="google_api_key" translatable="false">AIzaSyB0Bhr1sfsydfsdfnwelhkOYifak_Go2xU</string>
    <string name="google_app_id" translatable="false">1:888888888888:android:526f9740dfg987sdfg</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzaSyDkG-g8hH7T4TV7Rrsdfgiopudfmn234897</string>
    <string name="google_storage_bucket" translatable="false">project-8888888888888888888888.appspot.com</string>
</resources>

You can confirm that the initialization succeeded by putting this code in the onCreate() method of your main activity:

    FirebaseOptions opts = FirebaseApp.getInstance().getOptions();
    Log.i(TAG, "onStart: ID=" + opts.getApplicationId());
    Log.i(TAG, "onStart: SenderId=" + opts.getGcmSenderId());
    Log.i(TAG, "onStart: Key=" + opts.getApiKey());

If valid results are logged, it indicates the default FirebaseApp has been initialized, and you should be able to receive messages.