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

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

问题描述

我已按照本指南

,我已经按照此答案设置了连接与FCM之间 AWS SNS .

我可以成功接收从 FCM控制台发送的消息,但不能从投放状态在AWS上,我发送的每条消息均显示SUCCESS,而在设备上未显示任何通知.

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

解决方案

此处的问题是AWS SNS发送的是Google通话数据消息.

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

通过扩展FirebaseMessagingService并覆盖它的onMessageReceived方法,即使来自后台的SNS仍然可以处理来自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正常工作是一件轻而易举的事,所以很努力).

I've connected an Android app to Google Firebase Cloud Messaging service (FCM) following this guide,

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

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

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?

解决方案

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

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

Data messages that come in from SNS still can be handled - even if your app is in 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

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

  • See them pop up when the app is in background
  • Have your text displayed in the notification
  • When the app activates you want all of the messages cleared out from the drawer

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

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

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());
        }
    }

}

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();
}

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天全站免登陆