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

时间:2019-03-25 09:30:34

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

我有一个lambda函数编写并托管在Amazon 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:数据:键,默认值一些默认消息

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

的推送通知

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

非常愚蠢的问题,但是亚马逊应该已经解决了。

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

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

它有效!该死!