9

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?

Community
  • 1
  • 1
bluenowhere
  • 2,683
  • 5
  • 24
  • 37
  • Same here. And there is no where to find how and when the message is dropping. Cloudwatch reports show success for every messages I have been sending, but none is delivered to the device. Is there anyone out there with an answer to this one. I suspect Firebase here. – Joseph Bolade Caxton-Idowu Jul 31 '17 at 23:22
  • Did you resolve your issue? Bcoz I am also facing the same issue. – Gaurav Pandey Sep 22 '18 at 12:16

5 Answers5

11

The problem here is that AWS SNS sends what Google calls 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 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

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

  • See them pop up when the app is in the 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 visible 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).

Arghavan
  • 1,125
  • 1
  • 11
  • 17
Ivan Karpan
  • 1,554
  • 15
  • 20
11

I faced the exactly the same problem, message from Firebase with device token works but somehow message from SNS to Firebase is not delivered.

I did develop iOS app as well, and at that moment, just sending "brabra" delivered message to iOS. However, FCM only accepts particular message format to test it from AWS SNS console.

Here is the example format of successful delivery of message to Android through SNS and FCM.

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

The point is that we have to amend to "notification", not "data", and should have body and title in the notification.

Yuki Tanaka
  • 215
  • 2
  • 9
  • 1
    Hi I'm not able to Send IOS notification is there any specific format i need to use for payload to send it through SNS. It's working from Firebase but not from SNS. – Curiousdev Jul 27 '20 at 16:35
  • 1
    I was struggling to get iOS messages working from SNS -> FCM and this comment was a lifesaver. Thank you! – brk Oct 09 '20 at 16:32
  • a lot more reference on official docs: https://docs.aws.amazon.com/sns/latest/dg/sns-send-custom-platform-specific-payloads-mobile-devices.html – maddy Aug 12 '21 at 12:10
6

You can use this video tutorial https://youtu.be/iBTFLu30dSg with English subtitles of how to use FCM with AWS SNS step by step and example of how to send push notifications from AWS console. It works well for me and I successfully received push notification from SNS console and from my code on the mobile device

Arbron
  • 151
  • 2
  • 5
3

Simply using this JSON format:

{
  "GCM": "{ \"notification\": { \"body\": \"Sample message for Android endpoints\",\"title\": \"Sample message for Android endpoints\"}}"
}
fcdt
  • 2,371
  • 5
  • 14
  • 26
0

To get the Data from AWS SNS Console follow the below step:

1) Add the Project in FCM and Use Legacy server key for AWS SNS.

2) Get the Device token by using the below code:

String deviceToken = FirebaseInstanceId.getInstance().getToken();

3) Implement the below code in your application

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) Override onMessageReceived() its invoke when notification is received:

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

    }
}

}

When We get the Notification from AWS SNS Service then we use remoteMessage.getData().get("default") for reading the message from AWS.

Sumant Singh
  • 904
  • 1
  • 14
  • 16