连接到FCM的应用程序没有从AWS SNS接收通知

9
我已经按照这篇指南将Android应用连接到了Google Firebase Cloud Messaging服务(FCM),并且我按照这个答案设置了FCM与AWS SNS之间的连接。

我能成功地从FCM控制台接收消息,但无法从AWS SNS控制台接收消息。

在AWS上记录的交付状态对于我发送的每个消息都显示为“SUCCESS”,但我的设备上没有通知显示。

是否有一种方法可以检查发生了什么?


同样的问题。而且没有地方可以找到消息何时以及如何丢失。Cloudwatch报告显示我发送的每条消息都成功,但是没有一条消息被发送到设备上。是否有人能回答这个问题。我怀疑是Firebase的问题。 - Joseph Bolade Caxton-Idowu
你解决了你的问题吗?因为我也遇到了同样的问题。 - Gaurav Pandey
5个回答

11
问题在于AWS SNS发送了Google称之为数据消息的内容。使用FCM可以发送两种类型的消息 - 通知和数据。通知会被FCM自动显示,而数据消息则不会。有关详细信息,请参见:https://firebase.google.com/docs/cloud-messaging/concept-options。来自SNS的数据消息仍然可以处理 - 即使您的应用程序在后台运行 - 通过扩展FirebaseMessagingService并覆盖其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工作真是一件轻而易举的事情,天哪)。


1
这对我很有帮助。 - Saurabh Ghewari
这对我没有用。问题在于Firebase手动消息传递可以工作,但从AWS SNS无法工作。 - Yuki Tanaka

11

我遇到了完全相同的问题,来自Firebase的设备令牌消息有效,但是从SNS发送到Firebase的消息却无法传递。

我也开发了iOS应用程序,在那时,只需发送“brabra”即可向iOS传递消息。然而,FCM仅接受特定的消息格式以从AWS SNS控制台对其进行测试。

以下是通过SNS和FCM成功向Android传递消息的示例格式。

{
 "GCM": "{\"notification\": { \"body\": \"Sample message for Android endpoints\", \"title\":\"Hello world\" } }"
}

重点是我们需要将“数据”更改为“通知”,并且通知中应包含正文和标题。


1
嗨,我无法发送IOS通知,我需要使用哪种特定格式的有效载荷才能通过SNS发送它。从Firebase可以工作,但从SNS不行。 - Curiousdev
1
我一直在努力让 iOS 消息从 SNS -> FCM 正常工作,这条评论拯救了我。谢谢! - brk
官方文档中有更多的参考内容:https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html - maddy

6
你可以使用这个视频教程https://youtu.be/iBTFLu30dSg,里面有英文字幕,讲解了如何逐步使用FCM与AWS SNS,并提供了如何从AWS控制台发送推送通知的示例。对我而言它很有效,我成功地从SNS控制台和我的移动设备上的代码中收到了推送通知。

3

只需要使用以下这个 JSON 格式:

{
  "GCM": "{ \"notification\": { \"body\": \"Sample message for Android endpoints\",\"title\": \"Sample message for Android endpoints\"}}"
}

0

要从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的消息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接