连接到 FCM 的应用程序未收到来自 AWS SNS 的通知 [英] App connected to FCM not receiving notification from AWS SNS

查看:30
本文介绍了连接到 FCM 的应用程序未收到来自 AWS SNS 的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照 本指南,

我已经按照这个答案设置连接FCM 与AWS SNS.

and I've followed this answer to setup the connection between FCM & AWS SNS.

我可以成功接收从 FCM 控制台 发送的消息,但不能从 AWS SNS 控制台.

I could successfully receive message sent from FCM console but not from AWS SNS console.

消息 delivery status 已登录AWS 为我发送的每条消息都显示了 SUCCESS,而我的设备上没有显示任何通知.

The message delivery status logged on AWS showed SUCCESS for every message I've sent while no notification was shown on my device.

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

Is there a way to check what's going on?

推荐答案

这里的问题是 AWS SNS 发送的是 Google 所谓的数据消息.

The problem here is that AWS SNS sends what Google calls data messages.

使用 FCM,您可以发送两种类型的消息 - 通知和数据.FCM 会自动显示通知,而数据消息则不会.更多相关信息:https://firebase.google.com/docs/cloud-消息/概念选项

With FCM you can send two types of messages - notifications and data. Notifications get displayed automatically by FCM while data messages do not. More on this here: https://firebase.google.com/docs/cloud-messaging/concept-options

仍然可以处理来自 SNS 的数据消息 - 即使您的应用程序在后台 - 通过扩展 FirebaseMessagingService 并覆盖它的 onMessageReceived 方法.更多相关信息:https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService

Data messages that come in from SNS still can be handled - even if your app is in the background - by extending FirebaseMessagingService and overriding it's onMessageReceived method. More on this here: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService

我假设您希望您的 AWS SNS 消息模仿通知体验,即:

I assume you would want your AWS SNS messages to mimic the notifications experience, namely:

  • 当应用程序在后台时看到它们弹出
  • 在通知中显示您的文字
  • 当应用程序激活时,您希望清除所有消息抽屉

要实现这一点,您需要做三件事.

To achieve this you'll want to do three things.

首先 - 您需要开始跟踪您的应用当前是否可见.有关如何可靠地检测此问题的详细信息,您可以在此处找到:https://stackoverflow.com/a/18469643/96911

Firstly - you'll want to start tracking if your app is currently visible or not. The details on how to reliably detect this you can find here: https://stackoverflow.com/a/18469643/96911

其次 - 您需要通过发布通知来处理来自 AWS SNS 的数据消息,但前提是您的应用程序在后台:

Secondly - you'll want to handle data messages from AWS SNS by posting a notification, but only when your app is in the background:

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 方法:

And lastly - you'll want to clear out all of the notifications from the drawer when the user clicks on one of them. Combined with the visibility tracking I linked just above the activity that responds to the notifications should have the following onResume method:

@Override
protected void onResume() {
    super.onResume();

    MyApplication.activityResumed();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mNotificationManager.cancelAll();
}

你问这个问题已经有很长时间了,但深入了解这个问题让我很痛苦,我还是决定回答.我希望这可以帮助你或其他人试图让这件事发挥作用(因为让 iOS 工作变得轻而易举,天哪).

It's been a long time since you asked this question but it was so painful for me to get to the bottom of this I decided to answer anyway. I hope this helps you or somebody tearing their hair out trying to make this thing work (cause making iOS work was a breeze, sheesh).

这篇关于连接到 FCM 的应用程序未收到来自 AWS SNS 的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆