我正在尝试使用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()
}
答案 0 :(得分:2)
如果您使用apns的开发人员资料,则应编写APNS_SANDBOX而不是APNS
var payload = {
default: 'Test Message',
APNS_SANDBOX: {
aps: {
alert: 'Test Message on iPhone',
badge: 1,
sound: "default"
},
}
};