我正在尋找使用node.js與Amazon SNS和Apple APN推送通知的示例。我們使用亞馬遜作爲我們的託管服務,之前我使用過SNS,這非常簡單。但是他們用於推送通知的示例是針對java的,並且沒有針對Node的示例。和往常一樣,這很讓人困惑,我希望能夠縮短我的研究和時間。它不是那麼難。我也想知道他們如何處理錯誤,以及沙盒和生產之間的差異。蘋果公司在這兩種環境之間的反應有所不同,而不是像沙盒中那樣在沙盒中失敗。任何使用Node.js與Amazon SNS和Apple推送通知的人?
回答
結果不是那麼難,只是弄清楚文檔是不愉快的。您需要在控制檯中爲SNS主題創建主要終端,這是最簡單的方法,包括加載證書。然後,您使用createPlatformEnpoint爲每個設備ID創建一個端點。這將返回另一個SNS主題,即該設備的特定主題,然後用於發送該郵件。
因此,以下工作將單個消息發送給單個客戶端。如果你想發送一個集體,不知道你可以做到這一點。也不知道你如何處理蘋果的反饋,你應該檢查失敗的發送。
config = require("./config.js").config;
var token = "1234567898123456789";
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: config.AWSAccessKeyId, secretAccessKey: config.AWSSecretKey});
AWS.config.update({region: config.AWSRegion});
var sns = new AWS.SNS();
var params = {'PlatformApplicationArn':config["AWSTargetARN"],'Token':token};
var message = 'Test';
var subject = 'Stuff';
sns.createPlatformEndpoint(params,function(err,EndPointResult)
{
var client_arn = EndPointResult["EndpointArn"];
sns.publish({
TargetArn: client_arn,
Message: message,
Subject: subject},
function(err,data){
if (err)
{
console.log("Error sending a message "+err);
}
else
{
console.log("Sent message: "+data.MessageId);
}
});
});
CargoMeister指出這很簡單。
我寫得到它一篇博客文章中設置檢查出來這裏http://evanshortiss.com/development/mobile/2014/02/22/sns-push-notifications-using-nodejs.html
我也已經使Node.js的包裝模塊相比,更便於AWS SDK使用,因爲我周圍的工作文檔。它支持iOS和Android推送服務(這是我測試過的所有功能),管理字符串以外的消息格式並公開事件:https://npmjs.org/package/sns-mobile
我沒有使用主題來管理端點,不確定是問題雖然。您只需通過SNS控制檯創建PlatformEndpoints。
不錯。我會檢查一下。我們發現的其中一件事是,如果您嘗試將所有內容一次全部發送給新用戶,則必須創建終結點,並且需要永久。 – CargoMeister
是的,我想這更像是一個I/O問題,與Amazon允許創建發生的速度有關,還有創建每個新用戶需要新的HTTP請求的事實 – eshortie
您的博客未激活@eshortie現在可以真正使用它:) – CodePhobia
var AWS = require('aws-sdk');
var express = require('express');
var app = express();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'add IdentityPoolId'
});
AWS.config.region = 'add region';
var sns = new AWS.SNS();
sns.createPlatformEndpoint({
PlatformApplicationArn: 'add platform application arn',
Token: 'add device token'
}, function (err, data) {
if (err) {
console.log("errorMessage" + err.stack);
return;
}
var endpointArn = data.EndpointArn;
var payload = {
default: 'Hello World',
APNS: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
}
};
// first have to stringify the inner APNS object...
payload.APNS = JSON.stringify(payload.APNS);
// then have to stringify the entire message payload
payload = JSON.stringify(payload);
console.log('sending push');
sns.publish({
Message: payload,
MessageStructure: 'json',
TargetArn: endpointArn
}, function (err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('push sent');
console.log(data);
});
});
var server = app.listen(8081, function() {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
一個簡短的總結會幫助評論者(比如我)來判斷你答案的有效性;感興趣的用戶不必深入代碼來理解你的方法。只需一兩句話就沒問題,工作也不會太多。 –
這是我認爲最安全的實施。 適合我。謝謝! – rubberdont
- 1. Android推送通知:Google GCM與Amazon SNS?
- 2. iOS推送通知與SNS
- 3. SNS推送通知與圖像使用Node.js?
- 4. 使用Amazon SNS從iOS發送推送通知
- 5. 使用Amazon SNS服務發送推送通知
- 6. 使用amazon sns發送推送通知到android設備
- 7. Amazon EC2 PHP&Apple推送通知失敗
- 8. 使用Amazon SNS的推送通知 - 設備ID
- 9. 使用Amazon SNS在Ionic應用程序中推送通知
- 10. Amazon SNS Apple推送通知教程中的錯誤(空指針異常)
- 11. 如何使用Apple推送通知?
- 12. Qt Apple推送通知
- 13. Amazon SNS向單個設備發送直接推送通知
- 14. Amazon SNS無聲推送
- 15. AWS SNS推送通知沒有發送
- 16. node.js和推送通知
- 17. Apple推送通知和端口2195
- 18. 無法使用Amazon SNS在APNS Dev環境中發送推送通知
- 19. 過濾Apple推送通知
- 20. Apple推送通知配置
- 21. Apple推送通知構造
- 22. 將推送通知遷移到FCM或Amazon SNS?
- 23. Apple推送通知證書
- 24. Apple推送通知問題
- 25. iOS通過AWS SNS推送通知
- 26. Apple推送通知或Firebase推送通知?
- 27. 無Apple推送通知服務的推送通知?
- 28. 使用Amazon SQS和SNS設計推送隊列,如何?
- 29. 沉默的蘋果推送通知與sns主題和boto3
- 30. AWS SNS推送通知澄清
我們可以發送任何圖片以及消息@cargomeister –