【问题标题】:App connected to FCM not receiving notification from AWS SNS连接到 FCM 的应用程序未收到来自 AWS SNS 的通知
【发布时间】:2017-04-16 22:48:22
【问题描述】:

我已按照this guide 将 Android 应用连接到 Google Firebase 云消息传递服务 (FCM),

我已经按照this answer 设置了 FCM 和AWS SNS 之间的连接。

我可以成功接收到来自FCM console 的消息,但不能接收来自AWS SNS console 的消息。

登录到 AWS 的消息 delivery status 显示我发送的每条消息都成功,而我的设备上没有显示任何通知。

有没有办法检查发生了什么?

【问题讨论】:

  • 这里也一样。并且没有地方可以找到消息下降的方式和时间。 Cloudwatch 报告显示我发送的每条消息都成功,但没有一条消息传递到设备。有没有人可以回答这个问题。我怀疑这里有 Firebase。
  • 您的问题解决了吗? Bcoz 我也面临同样的问题。

标签: android amazon-web-services amazon-sns


【解决方案1】:

这里的问题是 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 工作变得轻而易举,天哪)。

【讨论】:

  • 这对我有帮助。
  • 这对我不起作用。问题是 Firebase 手动消息传递有效,但不适用于 AWS SNS。
【解决方案2】:

要从 AWS SNS 控制台获取数据,请按照以下步骤操作:

1) 在 FCM 中添加项目并为 AWS SNS 使用旧服务器密钥。

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

【讨论】:

    【解决方案3】:

    您可以使用此视频教程https://youtu.be/iBTFLu30dSg,并附有英文字幕,逐步说明如何将 FCM 与 AWS SNS 结合使用,以及如何从 AWS 控制台发送推送通知的示例。它对我很有效,我成功地收到了来自 SNS 控制台和移动设备上的代码的推送通知

    【讨论】:

      【解决方案4】:

      我遇到了完全相同的问题,来自 Firebase 的带有设备令牌的消息有效,但不知何故,从 SNS 到 Firebase 的消息没有传递。

      我也开发了 iOS 应用程序,当时只是向 iOS 发送“brabra”传递的消息。但是,FCM 只接受特定的消息格式来从 AWS SNS 控制台对其进行测试。

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

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

      关键是我们要修改“通知”,而不是“数据”,并且通知中应该有正文和标题。

      【讨论】:

      • 您好,我无法发送 IOS 通知,是否有任何特定格式需要用于有效负载以通过 SNS 发送。它适用于 Firebase,但不适用于 SNS。
      • 我一直在努力让 iOS 消息从 SNS -> FCM 工作,这条评论是救命稻草。谢谢!
      • 更多官方文档参考:docs.aws.amazon.com/sns/latest/dg/…
      【解决方案5】:

      只需使用这种 JSON 格式:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-07
        • 2021-10-30
        • 1970-01-01
        • 2017-11-29
        相关资源
        最近更新 更多