2017-07-17 5 views
0

FirebaseMessagingServiceインスタンスからログを読み取ることができません。 LogCatにログエントリが表示されません。FirebaseMessagingServiceからのログがありません

ブレークポイントも機能しません。 putExtraコールは発動しません - エクストラは常に私のMainActivityにnullです。

このような動作の理由は何でしょうか?

デフォルトのスーパークラスのように感じるFirebaseMessagingServiceインスタンスは何とか呼び出されています。私のカスタムコードは完全に無視されているようです。

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // [START_EXCLUDE] 
     // There are two types of messages data messages and notification messages. Data messages are handled 
     // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
     // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
     // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
     // When the user taps on the notification they are returned to the app. Messages containing both notification 
     // and data payloads are treated as notification messages. The Firebase console always sends notification 
     // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 
     // [END_EXCLUDE] 

     // TODO(developer): Handle FCM messages here. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

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

      if (/* Check if data needs to be processed by long running job */ true) { 
       // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher. 
       scheduleJob(); 
      } else { 
       // Handle message within 10 seconds 
       handleNow(); 
      } 

     } 

     // Check if message contains a notification payload. 
     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
     } 

     // Also if you intend on generating your own notifications as a result of a received FCM 
     // message, here is where that should be initiated. See sendNotification method below. 


     Log.i(TAG, "onNewIntent send notification"); 
     sendNotification(remoteMessage.getNotification().getBody()); 
    } 
    // [END receive_message] 

    /** 
    * Schedule a job using FirebaseJobDispatcher. 
    */ 
    private void scheduleJob() { 
     // [START dispatch_job] 
     FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this)); 
     Job myJob = dispatcher.newJobBuilder() 
       .setService(MyJobService.class) 
       .setTag("my-job-tag") 
       .build(); 
     dispatcher.schedule(myJob); 
     // [END dispatch_job] 
    } 

    /** 
    * Handle time allotted to BroadcastReceivers. 
    */ 
    private void handleNow() { 
     Log.d(TAG, "Short lived task is done."); 
    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Log.i(TAG, "onNewIntent"); 

     Intent intent = new Intent().setClass(getApplicationContext(), MainActivity.class); 
     intent.putExtra("fcm", "test"); 
     intent.putExtra("notification", true); 
     intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     String uniqueID = UUID.randomUUID().toString(); 
     intent.setAction(uniqueID); 

     PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
//    .setSmallIcon(R.drawable.ic_stat_ic_notification) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

     MainActivity mainActivity = AppDelegate.shared().mainActivity; 
     if (mainActivity != null) { 
      NotificationsViewmodel notificationsViewmodel = mainActivity.notificationsViewmodel; 
      if (notificationsViewmodel != null) { 
       notificationsViewmodel.actionGetNotifications(); 
      } 
     } 
    } 
} 

Androidのマニフェスト:

<!-- [START firebase_service] --> 
     <service 
      android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <!-- [END firebase_service] --> 
     <!-- [START firebase_iid_service] --> 
     <service 
      android:name=".MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service> 

ないログ(私は、このウィンドウで選択しようと何をすべきかに関係なく):

enter image description here

ファイルのAndroidプロジェクトで

enter image description here

ディスク上のの

と同じファイル: enter image description here

構成: enter image description here

データ: enter image description here

+0

何あなたのメッセージペイロードがサーバー – tyczj

+0

からのように見えるんことはあなたのLogCatに誤りを探してみてください。しかし、まず、あなたは[this](https://firebase.google.com/docs/android/setup)セットアップチュートリアルの前に従っていますか? –

+0

FCMメッセージはサーバーから正常に送信されます。私が引出しや画面上部から通知メッセージをクリックすると、MainActivityが開きます。 FCMのUIを使用して、サーバーから3つのキーと値のペアを送信します。 – asdf

答えて

1

方法onMessageReceived(...)が、時にアプリがバックグラウンドで動作している場合と呼ばれるか殺さ状態取得することはできませんメッセージはFirebaseコンソールから送信されます。

しかし、メッセージがAPI経由で送信された場合(https://fcm.googleapis.com/fcm/sendのPOSTを実行した場合)、方法onMessageReceived(…)が呼び出されます。

APIを使用してメッセージを送信するには、AdvancedREST Clientというツールを使用できます。これはChrome拡張機能です。詳細については

https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo

このチュートリアルhttp://androidbash.com/firebase-push-notification-android/

+0

アプリケーションがフォアグラウンドにいる間は呼び出されません。何か案は? – asdf

+0

あなたはonResume()の後のアクティビティとonPauseの前に喜んでいますか? –

+0

APIの残りの使用に関するアドバイスが役に立ちました。私は今正しく記録されているいくつかのnull ptrの問題があります。 – asdf

関連する問題