2017-07-17 108 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發送消息(執行POST到以下URL:https://fcm.googleapis.com/fcm/send)時,方法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