我無法從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>
無記錄(無論什麼我嘗試在此窗口中選擇):
文件中的Android項目:
在磁盤上個
貴消息負載的樣子從你的服務器 – tyczj
嘗試尋找您的logcat中的任何錯誤。但首先,你之前是否已經關注了[this](https://firebase.google.com/docs/android/setup)設置教程? –
FCM消息成功來自服務器。如果我從抽屜或屏幕頂部單擊通知消息,我將打開MainActivity。我使用FCM UI從服務器發送3個鍵值對。 – asdf