amazon-aws:发送通知类型的推送

amazon-aws : send notification type of push

我在亚马逊 lambda 上编写和托管了一个 lambda 函数。下面是该 lambda 的代码:

const AWS = require('aws-sdk');

exports.handler = (event, context) => {

  console.log("Received event:", JSON.stringify(event, null, 2));

  const targetArn = event.TargetArn;
  const sns = new AWS.SNS();

  const payload = {
    default: "some default message",
    GCM: {
      notification: {
        title: "Sample title",
        body: "Sample Body"
      },
      data: {
        title: "Sample title",
        body: "Sample Body"
      }
    }
  };

  const params = {
   Subject: "some default subject",
   Message: JSON.stringify(payload),
   MessageStructure: "json",
   TargetArn: targetArn
  };

  console.log('PUBLISHING', JSON.stringify(params, null, 2));

  sns.publish(params, function(err, data) {

    console.log('PUBLISHED!');

    if (err) {
      console.log(err, err.stack);

      return {
        statusCode: 500,
        body: JSON.stringify({error: err})
      };
    } else {
      console.log('SUCCESS!', data);

      return {
          statusCode: 200,
          body: JSON.stringify(data)
      };
    }

  });

};

现在,当我测试 lambda 以测试我是否在 Android 上收到推送时,我没有看到控制台上打印出完整的消息。下面是我用于登录 Android:

的代码
public class MyFirebaseMessagingService extends FirebaseMessagingService implements LifecycleObserver {

    public static final String ACTION_USER_FEEDBACK = "ACTION_UserFeedback";
    public static final String ARG_TITLE = "Title";
    public static final String ARG_BODY = "Body";
    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "onMessageReceived() called with: remoteMessage = [" + remoteMessage + "]");
        super.onMessageReceived(remoteMessage);

        if (remoteMessage.getData() != null) {
            for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                Log.d(TAG, "Data: key, " + key + " value " + value);
            }
            String title = remoteMessage.getData().get("title");
            String body = remoteMessage.getData().get("body");
            notifyActivity(title, body);
        }

        if (remoteMessage.getNotification() != null) {
            for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                Log.d(TAG, "Notification: key, " + key + " value " + value);
            }
        }
    }

    private void notifyActivity(String title, String body) {
        Intent intent = new Intent(ACTION_USER_FEEDBACK);
        intent.putExtra(ARG_TITLE, title);
        intent.putExtra(ARG_BODY, body);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public void onNewToken(String token) {
        sendRegistrationToServer(token);
    }

    private void sendRegistrationToServer(String token) {
        FCMTokenPreference.storeFCMDeviceToken(this, token);
        AWSRegistrationIntentService.start(this);
    }
}

下面是我在测试 lambda 时在控制台上得到的:

D/MyFirebaseMessagingService: onMessageReceived() called with: remoteMessage = [com.google.firebase.messaging.RemoteMessage@d9558fe] D/MyFirebaseMessagingService: Data: key, default value some default message

目的是发送类型为 Notification 而非 Data

的推送通知

谁能帮我解决这个问题?

非常愚蠢的问题,但无论如何亚马逊都应该处理它。

您必须在有效载荷对象中对 GCM 对象进行字符串化

  const payload = { 
    "default": "User Feedback Request",
    "GCM":"{\"notification\":{\"title\":\"Sample title\",\"body\":\"Sample body\"},\"data\":{\"title\":\"Sample title\",\"body\":\"Sample body\"}}"
  };

而且有效!妈的!