未在Eclipse中使用Firebase获取通知

时间:2016-09-01 20:57:33

标签: android eclipse firebase firebase-cloud-messaging

我使用Eclipse时没有任何警告就建立了Firebase(参见相关:Unable to find obfuscated Firebase class in Eclipse

我尝试发送测试通知,但是我没有得到任何东西。我也在使用图书馆项目。这是我的代码:

的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>

这些文件几乎是直接来自样本:

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());
    }

}

其他课程:

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);
    }
}

我通过Firebase控制台发送邮件,目标是包名,但没有运气。我不确定应该怎样做才能让它发挥作用。我是否需要以某种方式使用google-services.json文件?

由于

1 个答案:

答案 0 :(得分:0)

我不使用Eclipse,也无法验证这些步骤是否有效。我希望他们能让你开始走正确的道路。

Firebase框架由FirebaseInitProvider初始化。您不需要实现或子类化它。只需在你的清单中声明它。

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

FirebaseInitProvider期望找到包含项目配置值的字符串资源。在使用Android Studio和Google Services Gradle Plugin构建的应用中,资源值是从google-services.json文件创建的。由于您没有使用AS和插件,因此需要在res文件夹中定义这些资源。可以在Firebase控制台的“项目设置”或google-service.json file中找到这些值。因为您只对消息传递感兴趣,所以可能不需要某些值。为了安全起见,最初定义它们。

<?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>

您可以通过将此代码放在主要活动的onCreate()方法中来确认初始化是否成功:

    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());

如果记录了有效结果,则表示默认FirebaseApp已初始化,您应该能够接收消息。