我无法从FirebaseMessagingService实例中读取日志。我没有看到任何日志条目打印到LogCat。
断点也不起作用。 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项目中的文件:
答案 0 :(得分:1)
当消息通过Firebase控制台发送时,如果应用处于后台或已杀死状态,则不会调用方法onMessageReceived(...)
。
但是当消息通过API发送时(对以下网址执行POST: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/