这里的问题是 AWS SNS 发送 Google 所说的数据消息。
使用 FCM,您可以发送两种类型的消息 - 通知和数据。 FCM 会自动显示通知,而数据消息则不会。更多信息在这里:https://firebase.google.com/docs/cloud-messaging/concept-options
通过扩展 FirebaseMessagingService 并覆盖它的 onMessageReceived 方法,仍然可以处理来自 SNS 的数据消息 - 即使您的应用在后台。更多信息在这里:https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService
我假设您希望您的 AWS SNS 消息模仿通知体验,即:
- 当应用在后台时看到它们弹出
- 在通知中显示您的文本
- 当应用程序启动时,您希望清除所有消息
抽屉
要实现这一点,您需要做三件事。
首先 - 您需要开始跟踪您的应用当前是否可见。有关如何可靠地检测到这一点的详细信息,您可以在此处找到:https://stackoverflow.com/a/18469643/96911
其次 - 您需要通过发布通知来处理来自 AWS SNS 的数据消息,但前提是您的应用程序在后台:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
static protected int id = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (!MyApplication.isActivityVisible()) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(getString(R.string.app_name))
.setSmallIcon(R.drawable.notification_icon);
String message = remoteMessage.getData().get("default");
mBuilder.setContentText(message);
Intent resultIntent = new Intent(this, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(id ++, mBuilder.build());
}
}
}
最后 - 当用户点击其中一个通知时,您需要清除抽屉中的所有通知。结合我在响应通知的活动上方链接的可见性跟踪应该具有以下onResume 方法:
@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
}
你问这个问题已经很长时间了,但我还是很痛苦地想弄清楚这个问题,我还是决定回答一下。我希望这可以帮助你或其他人试图让这个东西正常工作(因为让 iOS 工作变得轻而易举,天哪)。