【问题标题】:How can i add extra key in UrbanAirship API using java如何使用 java 在 UrbanAirship API 中添加额外的密钥
【发布时间】:2015-10-26 10:41:37
【问题描述】:

我正在使用 Java 使用 UrbanAirship API 发送推送通知。

这里是文档:http://docs.urbanairship.com/api/

我想发送带有自定义键/值的推送通知。例如,我想发送到以下 Android/iOS 设备

name: "Jack"

String appKey = "appKey";
String appSecret = "appSecret";

// Setup an authenticated APIClient with your application key and
// application master secret.
APIClient apiClient = APIClient.newBuilder()
        .setKey(appKey)
        .setSecret(appSecret)
        .build();

// Setup a push payload to send to the API with our handy builders
PushPayload payload = PushPayload.newBuilder()
        .setAudience(Selectors.all())
        .setNotification(Notifications.notification("UA Push"))
        .setDeviceTypes(DeviceTypeData.of(DeviceType.IOS))
        .build();
// Try and send, handle anything that comes up
try {
    APIClientResponse<APIPushResponse> response = apiClient.push(payload);
    logger.info("Sent a push message!");
}
// Non 200 responses throw an APIRequestException. Check the documentation
// to debug your request.
catch (APIRequestException ex){
    logger.error("Non 200 request, checking error details and taking action");
}
// An underlying error occurred, most likely outside of the scope of the
// UA library, do some HTTP debugging
catch (IOException e){
    logger.error("Broken pipe what?");
}

这里是android的代码参考-https://github.com/urbanairship/java-library/blob/master/src/test/java/com/urbanairship/api/push/model/notification/android/AndroidDevicePayloadTest.java

如何使用 AndroidDevicePayload 发送带有自定义键/值的推送通知?

【问题讨论】:

    标签: java android urbanairship.com


    【解决方案1】:

    您可以这样创建通知:

    public PushPayload createPushPayloadCustom(String namedUser, String message) {
        Notification notification = Notification.newBuilder()
                .addDeviceTypeOverride(DeviceType.IOS, IOSDevicePayload.newBuilder()
                        .setAlert(message)
                        .build())
                .addDeviceTypeOverride(DeviceType.ANDROID, AndroidDevicePayload.newBuilder()
                        .setAlert(message)
                        .build())
                .build();       
    
        return PushPayload.newBuilder()
                .setAudience(Selectors.namedUser(namedUser))
                .setNotification(notification)
                .setDeviceTypes(DeviceTypeData.of(DeviceType.ANDROID, DeviceType.IOS))
                .build();
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以向“附加”对象添加任何键/值:

              DeviceTypeData deviceTypeData = DeviceTypeData.of(DeviceType.IOS, DeviceType.ANDROID);
      
          IOSDevicePayload iosPayload = IOSDevicePayload.newBuilder()
                  .setAlert(message)
                  .addExtraEntry("custom_ios_key", "custom value for IOS")
                  .build();
      
          AndroidDevicePayload androidPayload = AndroidDevicePayload.newBuilder()
                  .setAlert(message)
                  .addExtraEntry("custom_android_key", "custom value for Android")
                  .build();
      
          PushPayload payload = PushPayload.newBuilder()
                  .setAudience(Selectors.namedUser(email))
                  .setNotification(Notifications.notification(iosPayload,androidPayload))
                  .setDeviceTypes(deviceTypeData)
                  .build();
      

      然后在收到的push中,会找到对象:

      更多详情请至urbanairship official documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        • 1970-01-01
        • 2016-06-14
        • 1970-01-01
        相关资源
        最近更新 更多