2016-09-01 8 views
1

でFirebaseを使用して通知が届かない、私は、Eclipseを使用して、任意の警告なしFirebase建物を持っている(関連参照:Unable to find obfuscated Firebase class in EclipseEclipseの

私はしかし、私は何も届かない、テスト通知を送信しよう。私はライブラリプロジェクトも使用しています。これは私のコードです:

の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ファイルを使用する必要がありますか?

おかげ

+0

使用のAndroidメーカー –

+0

私は残念ながら、できません。 – zundi

答えて

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のプロジェクト設定で確認できます。メッセージングにのみ関心があるので、いくつかの値は必要ないかもしれません。安全であるためには、最初にすべてを定義してください。あなたは初期化があなたの主な活動のonCreate()方法でこのコードを置くことによって成功したことを確認することができます

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

:有効な結果が記録され

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が初期化された示し、メッセージを受信できるはずです。

+0

偉大なもの、私は今これを試しています。 JSONファイルはどのようにXMLに変換されますか?たとえば、{ "project_info":{ "project_number": "75794"、 "firebase_url": "https://this-is-a-test-project.firebaseio.com"などの値が表示されています – zundi

+0

@ zundi:あなたが探しているマッピングは、私が答えているGoogle Services Gradle Pluginのドキュメントにあると思います。 –

+0

@ zundi:これの最終結果はどうでしたか?それはあなたのために働いたのですか? –