2016-02-02 88 views
1

我正嘗試使用aws-sdk-js向iOS和Android設備發送推送通知。它可以發送通知消息,但它不是我想要的。如果我在aps字典中輸入badgesound,應用程序應該有徽章併發出聲音。但事實並非如此。無法使用aws-sdk-js發送aps到iOS使用aws-sdk-js

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() 

} 

回答

1

如果您使用的是開發者配置文件用於APNS你應該寫的,而不是APNS

var payload = { 
    default: 'Test Message', 
    APNS_SANDBOX: { 
     aps: { 
      alert: 'Test Message on iPhone', 
      badge: 1, 
      sound: "default" 
     }, 
    } 
}; 
APNS_SANDBOX