2016-08-16 10 views
2

Log、Toastを作成し、onMessageReceivedからサービスにブロードキャストを送信するために、以下のコードを使用しました。FireBase Colud Messaging ServiceのonMessageReceivedにトーストが表示されない

ログにFirebaseコンソールを使用して送信されたデータを確認することができます。しかし、トーストは表示されず、ブロードキャストも送信されません。

以下のコード:

class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Toast.makeText(getApplicationContext(),"From: " + remoteMessage.getFrom(),Toast.LENGTH_SHORT).show(); 

     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 

     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
      Toast.makeText(getApplicationContext(),"Toast shown",Toast.LENGTH_LONG).show(); 
      Intent i = new Intent(); 
      i.setAction("firebase.message.combinedthings"); 
      sendBroadcast(i); 
     } 
    } 
} 

答えて

0

私は長い間、この問題を経験していたし、いくつかの注目すべき提案があります まずアプリのマニフェストファイルにサービスを追加していることを確認してください:

<service android:name=".MyFirebaseMessagingService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
    </intent-filter> 
</service> 

第二に、あなたが追加したことを確認してください:

classpath 'com.google.gms:google-services:3.0.0' 

あなたのプロジェクトレベルのbuildscript>依存関係の下でbuild.gradleなどへ:

apply plugin: 'com.google.gms.google-services' 

アプリレベルのbuild.gradleの底に。 最後に誰もこれを勧めていませんでしたが、最後の手段として試してみましたが、私のFirebaseコンパイルのすべてがv9.4.0であることを確認しました。私は(あなたのアプリケーションレベルのbuild.gradle内にもいることを確認してください、それは私のために働いたことを変更したら、これは私が希望このことができます

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 

    compile 'com.android.support:appcompat-v7:24.1.1' 
    compile 'com.android.support:support-v4:24.1.1' 
    compile 'com.firebase:firebase-client-android:2.4.0' 
    compile 'com.google.firebase:firebase-crash:9.4.0' 
    compile 'com.google.firebase:firebase-core:9.4.0' 
    compile 'com.google.firebase:firebase-messaging:9.4.0' 
    compile 'com.google.firebase:firebase-auth:9.4.0' 
    compile 'com.android.support:recyclerview-v7:24.1.1' 
} 

:)のように見えたものです!

+0

答えをありがとう。 Toastは現在エラーを出していますが、私はToastのコードを削除しました。 –

3

onMessageReceived()のコードは、メインスレッド上で実行されていないので、トーストが表示されていない...あなたはUIスレッド上でその呼び出しを行う必要がありトーストを表示するには:

Handler handler = new Handler(Looper.getMainLooper()); 
    handler.post(new Runnable() { 
     public void run() { 
     Toast.makeText(getApplicationContext(), "Toast on UI thread", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

私がいました同じ問題を抱えていると、助けてくれることを願う

関連する問題