【问题标题】:amazon-aws : send notification type of pushamazon-aws:发送推送通知类型
【发布时间】:2019-08-15 12:21:14
【问题描述】:

我有一个在亚马逊 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() 调用: remoteMessage = [com.google.firebase.messaging.RemoteMessage@d9558fe] D/MyFirebaseMessagingService: Data: key, default value some default message

目标是发送类型为 Notification 而不是 Data

的推送通知

谁能帮我解决这个问题?

【问题讨论】:

    标签: android amazon-web-services aws-lambda aws-sdk amazon-sns


    【解决方案1】:

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

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

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

    而且它有效!该死!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多