2017-07-17 110 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