无法使用 aws-sdk-js 将 ap 发送到 iOS
Cannot send aps to iOS using aws-sdk-js
我正在尝试使用 aws-sdk-js 向 iOS 和 Android 设备发送推送通知。它可以发送通知消息,但它不是我想要的。如果我将 badge
和 sound
放入 aps
字典中,应用程序应该有一个徽章并播放声音。但事实并非如此。
Xcode 控制台输出:
[aps: {
alert = "Test Message";
}]
javascript代码:
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: '<key>', secretAccessKey: '<secrect>'});
AWS.config.update({region: 'ap-southeast-2'});
var sns = new AWS.SNS();
var payload = {
default: 'Test Message',
APNS: {
aps: {
alert: 'Test Message on iPhone',
badge: 1,
sound: "default"
},
}
};
payload.APNS = JSON.stringify(payload.APNS);
payload = JSON.stringify(payload);
var params = {
MessageStructure: 'json',
Message: payload,
Subject: 'Test push',
TargetArn: '<arn of the endpoint>'
};
sns.publish(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
application:didfinishlaunch
、
中的代码
let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = "ACCEPT_IDENTIFIER"
acceptAction.title = NSLocalizedString("Accept", comment: "Accept")
acceptAction.activationMode = .Foreground
acceptAction.destructive = false
acceptAction.authenticationRequired = false
let deleteAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_IDENTIFIER"
deleteAction.title = NSLocalizedString("Delete", comment: "Delete")
deleteAction.activationMode = .Foreground
deleteAction.destructive = true
deleteAction.authenticationRequired = false
let ignoreAction = UIMutableUserNotificationAction()
ignoreAction.identifier = "IGNORE_IDENTIFIER"
ignoreAction.title = NSLocalizedString("Ignore", comment: "Ignore")
deleteAction.activationMode = .Foreground
deleteAction.destructive = false
deleteAction.authenticationRequired = false
let messageCategory = UIMutableUserNotificationCategory()
messageCategory.identifier = "MESSAGE_CATEGORY"
messageCategory.setActions([acceptAction, deleteAction], forContext: .Minimal)
messageCategory.setActions([acceptAction, deleteAction, ignoreAction], forContext: .Default)
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: (NSSet(array: [messageCategory])) as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerForRemoteNotifications()
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
并实施协议:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print(identifier)
completionHandler()
}
如果您使用的是 apns 的开发者配置文件,您应该写 APNS_SANDBOX 而不是 APNS
var payload = {
default: 'Test Message',
APNS_SANDBOX: {
aps: {
alert: 'Test Message on iPhone',
badge: 1,
sound: "default"
},
}
};
我正在尝试使用 aws-sdk-js 向 iOS 和 Android 设备发送推送通知。它可以发送通知消息,但它不是我想要的。如果我将 badge
和 sound
放入 aps
字典中,应用程序应该有一个徽章并播放声音。但事实并非如此。
Xcode 控制台输出:
[aps: {
alert = "Test Message";
}]
javascript代码:
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: '<key>', secretAccessKey: '<secrect>'});
AWS.config.update({region: 'ap-southeast-2'});
var sns = new AWS.SNS();
var payload = {
default: 'Test Message',
APNS: {
aps: {
alert: 'Test Message on iPhone',
badge: 1,
sound: "default"
},
}
};
payload.APNS = JSON.stringify(payload.APNS);
payload = JSON.stringify(payload);
var params = {
MessageStructure: 'json',
Message: payload,
Subject: 'Test push',
TargetArn: '<arn of the endpoint>'
};
sns.publish(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
application:didfinishlaunch
、
let acceptAction = UIMutableUserNotificationAction()
acceptAction.identifier = "ACCEPT_IDENTIFIER"
acceptAction.title = NSLocalizedString("Accept", comment: "Accept")
acceptAction.activationMode = .Foreground
acceptAction.destructive = false
acceptAction.authenticationRequired = false
let deleteAction = UIMutableUserNotificationAction()
deleteAction.identifier = "DELETE_IDENTIFIER"
deleteAction.title = NSLocalizedString("Delete", comment: "Delete")
deleteAction.activationMode = .Foreground
deleteAction.destructive = true
deleteAction.authenticationRequired = false
let ignoreAction = UIMutableUserNotificationAction()
ignoreAction.identifier = "IGNORE_IDENTIFIER"
ignoreAction.title = NSLocalizedString("Ignore", comment: "Ignore")
deleteAction.activationMode = .Foreground
deleteAction.destructive = false
deleteAction.authenticationRequired = false
let messageCategory = UIMutableUserNotificationCategory()
messageCategory.identifier = "MESSAGE_CATEGORY"
messageCategory.setActions([acceptAction, deleteAction], forContext: .Minimal)
messageCategory.setActions([acceptAction, deleteAction, ignoreAction], forContext: .Default)
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: (NSSet(array: [messageCategory])) as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerForRemoteNotifications()
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
并实施协议:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject], completionHandler: () -> Void) {
print(identifier)
completionHandler()
}
如果您使用的是 apns 的开发者配置文件,您应该写 APNS_SANDBOX 而不是 APNS
var payload = {
default: 'Test Message',
APNS_SANDBOX: {
aps: {
alert: 'Test Message on iPhone',
badge: 1,
sound: "default"
},
}
};