我正在使用Twilio Java SDK向我的移动应用发送推送通知。我正在尝试覆盖APN有效负载以设置“徽章”属性,以便在iOS应用程序的图标上显示徽章。使用下面的代码,当我注释掉对来自notificationCreator.setApn的调用时,我成功收到通知,但如果我调用setApn,则不会收到通知。我想我正在创建的APN有效载荷存在问题。 这是代码:
NotificationCreator notificationCreator =
Notification.creator(BuildConfig.TWILIO_NOTIFY_SID)
.setIdentity(notifyeeId)
.setBody(body);
// Aps is a POJO class that mirrors the format of a
// notification's APS dictionary.
Aps aps = ApsBuilder.simpleAlertBuilder(body)
.setBadge(getBadgeCount(notifyeeId))
.setMutableContent(mutable)
.build();
// buildApnOverride converts Aps to JSON and returns
// a map mapping "aps" to that JSON.
Map<String, Object> apnOverride = buildApnOverride(aps);
logger.debug(String.format("apnOverride=%s", SharedObjects.getGson().toJson(apnOverride).toString()));
notificationCreator.setApn(apnOverride);
// Some code that I've omitted calls notificationCreator.setData
// to add some custom data.
Notification notification = notificationCreator.create();
logger.debug(String.format("notification=%s", SharedObjects.getGson().toJson(notification).toString()));
这是日志的输出:
apnOverride= { "aps": "{\"alert\":{\"body\":\"A patient booked a visit with you\"},\"badge\":3,\"sound\":\"default\",\"mutable-content\":1}" } notification= { "sid": "XXX", "accountSid": "XXX", "serviceSid": "XXX", "dateCreated": { "iMillis": 1517854346000, "iChronology": { "iBase": { "iMinDaysInFirstWeek": 4 } } }, "identities": [ "XXX" ], "tags": [], "segments": [], "priority": "HIGH", "ttl": 2419200, "body": "A patient booked a visit with you", "data": { "com.doctheapp.notificationData": "{\"type\":\"APPOINTMENT_BOOKED\",\"attributes\":[\"REFRESH_ACTIVITY_FEED\",\"REFRESH_DOCTOR_SCHEDULE\"],\"additionalData\":\"{\\\"appointmentStartTime\\\":\\\"2018-02-05T19:45Z\\\",\\\"serviceName\\\":\\\"New Patient Visit\\\"}\"}" }, "apn": { "aps": "{\"alert\":{\"body\":\"A patient booked a visit with you\"},\"badge\":3,\"sound\":\"default\",\"mutable-content\":1}" } }
任何人都知道我做错了什么?
编辑:我尝试在这里复制粘贴Twilio的示例代码(https://www.twilio.com/docs/api/notify/rest/notifications?code-sample=code-send-a-detailed-notification&code-language=java&code-sdk-version=7.x),我遇到了同样的问题。只有在我注释掉对setApn的调用时才会收到通知。答案 0 :(得分:0)
Twilio开发者传道者在这里。
我看到你试图用这个覆盖APNs通知(为便于阅读而分解):
{
"aps": "{
\"alert\":{
\"body\":\"A patient booked a visit with you\"
},
\"badge\":3,
\"sound\":\"default\",
\"mutable-content\":1
}"
}
当您使用mutable-content
时,我查看了文档并在modifying and presenting notifications上找到了此页面。它说:
为远程通知创建有效负载时,服务器应执行以下操作:
- 包含值为1的mutable-content键。
- 包含一个警报字典,其中包含警报标题和正文的子项。
我注意到您的通知只包含一个正文。你也可以尝试设置标题吗?
让我知道这是否有帮助