gpt4 book ai didi

ios - 使用Amazon AWS SNS服务IOS

转载 作者:行者123 更新时间:2023-12-01 17:25:15 25 4
gpt4 key购买 nike

我正在开发一个使用Amazon AWS服务的应用程序,它是一个使者。

我想使用IOS推送通知服务和Amazon SNS来实现2个用户之间的通信。通过将消息发布到称为终结点的目的地,我可以从SNS控制台发送消息。

但是,我无法通过IOS的Amazon SDK将消息从一个手机发送到另一手机。我可以通过Amazon API以这种方式进行操作吗?

我想将名为messageDict的NSDictionary发送到目标端点。我可以在不使用服务器的情况下实现这一目标吗?

NSDictionary *messageDict = [[NSDictionary alloc]init];

messageDict = @{ @"Name" : @"HelloWrold" ,@"Id" :@"GoodBye",};

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:messageDict options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];


NSLog(@"Jsonstring %@",jsonString);

AWSSNS *publishCall = [AWSSNS new];

AWSSNSPublishInput *message = [AWSSNSPublishInput new];

message.subject = @"My First Message";


//This is the ending point
message.topicArn = @"arn:aws:sns:us-east-1:012345678912:endpoint/APNS_SANDBOX/appTesting/201sjad-XXXX-XXXX-XXXX-c34sdfdsf1d9c";

message.messageAttributes = messageDict;

message.messageStructure = jsonString;




[[publishCall publish:message]continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task){

if (task.error != nil) {
NSLog(@"Error %@",task.error);

}


else{



NSLog(@"Successful");

}

return nil;

}];

最佳答案

您可以将推送通知发送到特定设备(端点)或主题(多个订户列表)

两者的API调用略有不同。如此处API文档所述,您可以使用message.topicArnmessage.targetArn http://docs.aws.amazon.com/sns/latest/api/API_Publish.html

(Objective-C类文档在这里:http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSSNSPublishInput.html)

您的ARN是端点ARN,您的代码将其分配给message.topicArn我将其更改为

  message.targetArn = @"arn:aws:sns:us-east-1:123456789012:endpoint/APNS_SANDBOX/appTesting/201sjad-XXXX-XXXXXX-XXXX-c34sdfdsf1d9c";

(已对ARN进行了修改,以模糊您的帐户ID)

另外,请阅读 http://mobile.awsblog.com/post/Tx223MJB0XKV9RU/Mobile-token-management-with-Amazon-SNS并应用最佳实践以获取和管理您的设备 token 。

这是Javascript中的代码示例,可以轻松地适应Objective-C。
var DEFAULT_SNS_REGION  = 'eu-west-1';
var SNS_ENDPOINT_ARN = 'arn:aws:sns:eu-west-1:0123456789012:endpoint/APNS_SANDBOX/AmazonSNSPushDemo/95084b8f-XXXX-XXXX-XXXX-b3429d0fa528';
var SNS_TOPIC_ARN = 'arn:aws:sns:eu-west-1:012345678912:PushNotifications';

function sendNotification(msg, topicARN, endPointARN) {
var sns = new aws.SNS({
apiVersion: '2010-03-31',
region: DEFAULT_SNS_REGION
});

var params = {}
if (topicARN != '') {
params = {
Message: msg,
//MessageStructure: 'json',
TopicArn: topicARN
};
} else {
params = {
Message: msg,
//MessageStructure: 'json',
TargetArn: endPointARN
};
}

console.log(params);

var deferred = Q.defer();
sns.publish(params, function(err,data) {
if (err) {
console.log(err, err.stack); // an error occurred
deferred.reject(err);
} else {
console.log(data); // successful response
deferred.resolve(data);
}
});

return deferred.promise; }

关于ios - 使用Amazon AWS SNS服务IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27618374/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com