使用带有ColdFusion的Java Apple推送通知服务库(http://notnoop.github.io/java-apns/)测试推送通知时,我有奇怪/间歇的结果。
以下是有效的Payload字符串示例:
{"aps":{"alert":"dsfkjsdlfkjasdlfkjsdlfkjasdlkfjasldfkjasldkfjasdlkfjasldkfjasldkfjasdlkfjasldkfjasdlkfjasdlfkjasdlkfjasdlkfjasldkfjasldkfjasldkfjasldkfjasldkfjasldkfjasldkfjalsdkfjlasdkfjaslkdjf"},"ID":"431"}
当我发送包含其他自定义字段的通知时,"短"消息文本,它通过。样品:
{"ALERTTYPE":"Collection","ID":"427","ALERTVALUE":"50","aps":{"alert":"005 - Source: Eric Dev PC;"}}
然而,当我尝试用"更长的"发送通知时文字,通知不会通过。
例如,我发送了一个带有Link to Collection的通知,消息文本为“004 - Source:Eric Dev PC;”,我收到了它,但是,当我发送带有Link to Collection和消息的通知时文字“004 - 资料来源:Eric Dev PC;证书:ABC123;链接到收藏:收集123;“,它没有通过。在尝试推送它之前,我检查了“较长”的有效载荷,并且它不大 - 158字节(我相信最大值是2048字节)。我还使用isTooLong()
方法检查有效负载并使用shrinkBody()
将其缩短以确保有效负载不会太大。
{"ALERTTYPE":"Collection","ID":"427","ALERTVALUE":"50","aps":{"alert":"005 - Source: Eric Dev PC; Certificate: ABC123; Link to Collection: Collection 123;"}}
同样,此有效负载远低于阈值。
这是我的代码:
// Create the ApnsService object
LOCAL.ApnsService =
CreateObject("java", "com.notnoop.apns.APNS")
.newService()
.withCert(
VARIABLES.ApnsCertificate["filePath"],
VARIABLES.ApnsCertificate["password"]
)
.withProductionDestination()
.build();
// Start the Apns service
LOCAL.ApnsService.start();
// Set default value for Connection Test status
LOCAL.ConnectionTestSuccess = true;
/*
Test the connection - this will throw an exception if the Apple
servers aren't reachable or the service cannot send
notifications for now (attempt will be made at next scheduled
time)
*/
try {
LOCAL.ApnsService.testConnection();
}
catch (any error){
LOCAL.ConnectionTestSuccess = false;
}
// Connection Test worked, so attempt to send the Notification
if (LOCAL.ConnectionTestSuccess) {
/*
Before attempting to send the push notification, retrieve
and deactivate inactive device tokens to prevent issues
with Apple
*/
deleteInactiveDevices(apnsCert=VARIABLES.ApnsCertificate);
// Retrieve all User Device Tokens for this Push Notification
LOCAL.qUserDeviceTokens = getUserDeviceTokens();
LOCAL.aDeviceTokens =
ListToArray(ValueList(LOCAL.qUserDeviceTokens.DEVICETOKEN));
// Generate a Payload Builder instance
LOCAL.PayloadBuilder =
CreateObject("java", "com.notnoop.apns.APNS")
.newPayload();
// Add the alert body to the payload
LOCAL.PayloadBuilder
.alertBody(Trim(VARIABLES.MessageText));
// Build the Custom Fields object/structure
LOCAL.CustomFields.id = VARIABLES.Id;
// Link to News Article
if (Len(Trim(VARIABLES.NewsArticleId))) {
LOCAL.CustomFields.alertType = "News";
LOCAL.CustomFields.alertValue = Trim(VARIABLES.NewsArticleId);
}
// Link to Collection
else if (Len(Trim(VARIABLES.CollectionId))) {
LOCAL.CustomFields.alertType = "Collection";
LOCAL.CustomFields.alertValue = Trim(VARIABLES.CollectionId);
}
// Link to Search Results
else if (Len(Trim(VARIABLES.SearchText))) {
LOCAL.CustomFields.alertType = "Search";
LOCAL.CustomFields.alertValue = Trim(VARIABLES.SearchText);
}
// Add Custom Fields to the Payload
LOCAL.PayloadBuilder
.customFields(LOCAL.CustomFields);
/*
Shrink the alert body so that the entire payload
string will fit within Apple's byte limit
*/
if (LOCAL.PayloadBuilder.isTooLong()) {
LOCAL.PayloadBuilder
.shrinkBody("...");
}
LOCAL.PayloadJSONString =
LOCAL.PayloadBuilder
.build();
/*
Push the notification through Apple's APNS;
Note: push() accepts a Java Collection and a Java
String. ColdFusion Arrays are already typed as Java
Collections, so there is no need to recast it
*/
LOCAL.Notifications =
LOCAL.ApnsService
.push(
LOCAL.aDeviceTokens,
JavaCast("string", LOCAL.PayloadJSONString)
);
// Stop the Apns service
LOCAL.ApnsService.stop();
快速更新......我发送了一堆带有其他自定义字段的通知,并逐个减少了消息文本中的字符数。如果消息文本包含32个字符或更少(Payload字节长度= 107) - 它们似乎更多,并且没有收到通知。似乎是随意的......令人费解...