onMessageReceived
方法。有关详细信息,请参见:https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService。我假设您希望AWS SNS消息模仿通知体验,即:
- 在应用程序后台时弹出它们。
- 显示通知中的文本。
- 当应用程序激活时,您希望从抽屉中清除所有消息。
要实现这一点,您需要做三件事情。
首先 - 您需要开始跟踪您的应用程序当前是否可见。如何可靠地检测这一点的详细信息可以在此处找到:https://dev59.com/sGMl5IYBdhLWcg3w5KXd#18469643
其次 - 当您的应用程序在后台运行时,您需要通过发布通知来处理来自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工作真是一件轻而易举的事情,天哪)。
我遇到了完全相同的问题,来自Firebase的设备令牌消息有效,但是从SNS发送到Firebase的消息却无法传递。
我也开发了iOS应用程序,在那时,只需发送“brabra”即可向iOS传递消息。然而,FCM仅接受特定的消息格式以从AWS SNS控制台对其进行测试。
以下是通过SNS和FCM成功向Android传递消息的示例格式。
{
"GCM": "{\"notification\": { \"body\": \"Sample message for Android endpoints\", \"title\":\"Hello world\" } }"
}
重点是我们需要将“数据”更改为“通知”,并且通知中应包含正文和标题。
只需要使用以下这个 JSON 格式:
{
"GCM": "{ \"notification\": { \"body\": \"Sample message for Android endpoints\",\"title\": \"Sample message for Android endpoints\"}}"
}
要从AWS SNS控制台获取数据,请按照以下步骤进行:
1)在FCM中添加项目,并使用AWS SNS的Legacy服务器密钥。
2)使用以下代码获取设备令牌:
String deviceToken = FirebaseInstanceId.getInstance().getToken();
3) 在您的应用程序中实现以下代码
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
sendRegistrationToServer(refreshedToken);
}
private void sendRegistrationToServer(String token) {
// TODO: Implement this method to send token to your app server.
}
}
4) 重写 onMessageReceived() 方法,当接收到通知时会被调用:
public class AppFirebaseMessagingService extends FirebaseMessagingService {
static protected int id = 0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//remoteMessage.getNotification().getBody()
if (remoteMessage.getData().get("default").length() > 0) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri ringNotificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("ApplicationName")
.setContentText(remoteMessage.getData().get("default"))
.setAutoCancel(true)
.setSound(ringNotificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id++, notifyBuilder.build());
}
}
}
当我们从AWS SNS服务收到通知时,我们使用remoteMessage.getData().get("default")
来读取来自AWS的消息。