|
27 | 27 | import com.google.common.collect.ImmutableList; |
28 | 28 | import com.google.firebase.ErrorCode; |
29 | 29 | import com.google.firebase.testing.IntegrationTestUtils; |
| 30 | +import java.time.Duration; |
30 | 31 | import java.util.ArrayList; |
31 | 32 | import java.util.List; |
32 | 33 | import java.util.concurrent.ExecutionException; |
@@ -77,6 +78,55 @@ public void testSend() throws Exception { |
77 | 78 | assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
78 | 79 | } |
79 | 80 |
|
| 81 | + @Test |
| 82 | + public void testSendAndroidV2RemoteNotification() throws Exception { |
| 83 | + FirebaseMessaging messaging = FirebaseMessaging.getInstance(); |
| 84 | + String id = messaging.sendAsync(androidV2RemoteNotificationMessage(), true).get(); |
| 85 | + assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testSendAndroidV2MinimalRemoteNotification() throws Exception { |
| 90 | + FirebaseMessaging messaging = FirebaseMessaging.getInstance(); |
| 91 | + String id = messaging.sendAsync(androidV2MinimalRemoteNotificationMessage(), true).get(); |
| 92 | + assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testSendAndroidV2BackgroundSync() throws Exception { |
| 97 | + FirebaseMessaging messaging = FirebaseMessaging.getInstance(); |
| 98 | + String id = messaging.sendAsync(androidV2BackgroundSyncMessage(), true).get(); |
| 99 | + assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testSendAndroidV2MinimalBackgroundSync() throws Exception { |
| 104 | + FirebaseMessaging messaging = FirebaseMessaging.getInstance(); |
| 105 | + String id = messaging.sendAsync(androidV2MinimalBackgroundSyncMessage(), true).get(); |
| 106 | + assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSendEachAndroidV2() throws Exception { |
| 111 | + List<Message> messages = ImmutableList.of( |
| 112 | + androidV2RemoteNotificationMessage(), |
| 113 | + androidV2MinimalRemoteNotificationMessage(), |
| 114 | + androidV2BackgroundSyncMessage(), |
| 115 | + androidV2MinimalBackgroundSyncMessage()); |
| 116 | + |
| 117 | + BatchResponse response = FirebaseMessaging.getInstance().sendEach(messages, true); |
| 118 | + |
| 119 | + assertEquals(4, response.getSuccessCount()); |
| 120 | + assertEquals(0, response.getFailureCount()); |
| 121 | + assertEquals(4, response.getResponses().size()); |
| 122 | + for (SendResponse sendResponse : response.getResponses()) { |
| 123 | + assertTrue(sendResponse.isSuccessful()); |
| 124 | + String id = sendResponse.getMessageId(); |
| 125 | + assertTrue(id != null && id.matches("^projects/.*/messages/.*$")); |
| 126 | + assertNull(sendResponse.getException()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
80 | 130 | @Test |
81 | 131 | public void testSendError() throws InterruptedException { |
82 | 132 | FirebaseMessaging messaging = FirebaseMessaging.getInstance(); |
@@ -297,4 +347,104 @@ public void testUnsubscribe() throws Exception { |
297 | 347 | ImmutableList.of(TEST_REGISTRATION_TOKEN), "mock-topic").get(); |
298 | 348 | assertEquals(1, results.getSuccessCount() + results.getFailureCount()); |
299 | 349 | } |
| 350 | + |
| 351 | + private static AndroidNotificationV2 fullAndroidNotificationV2() { |
| 352 | + return AndroidNotificationV2.builder() |
| 353 | + .setTitle("test.title") |
| 354 | + .setBody("test.body") |
| 355 | + .setIcon("test.icon") |
| 356 | + .setColor("#AABBCC") |
| 357 | + .setSound("test.sound") |
| 358 | + .setTag("test.tag") |
| 359 | + .setClickAction("test.click.action") |
| 360 | + .setBodyLocalizationKey("test.body.loc.key") |
| 361 | + .addAllBodyLocalizationArgs(ImmutableList.of("body.arg1", "body.arg2")) |
| 362 | + .setTitleLocalizationKey("test.title.loc.key") |
| 363 | + .addAllTitleLocalizationArgs(ImmutableList.of("title.arg1", "title.arg2")) |
| 364 | + .setChannelId("test.channel.id") |
| 365 | + .setImage(TEST_IMAGE_URL) |
| 366 | + .setTicker("test.ticker") |
| 367 | + .setSticky(true) |
| 368 | + .setEventTimeInMillis(System.currentTimeMillis()) |
| 369 | + .setLocalOnly(true) |
| 370 | + .setNotificationPriority(AndroidNotificationV2.NotificationPriority.HIGH) |
| 371 | + .setVibrateTimingsInMillis(new long[]{100L, 50L, 250L}) |
| 372 | + .setDefaultVibrateTimings(false) |
| 373 | + .setDefaultSound(true) |
| 374 | + .setLightSettings(LightSettings.builder() |
| 375 | + .setColorFromString("#AABBCC") |
| 376 | + .setLightOnDurationInMillis(200) |
| 377 | + .setLightOffDurationInMillis(300) |
| 378 | + .build()) |
| 379 | + .setDefaultLightSettings(false) |
| 380 | + .setVisibility(AndroidNotificationV2.Visibility.PRIVATE) |
| 381 | + .setNotificationCount(1) |
| 382 | + .setId(42) |
| 383 | + .build(); |
| 384 | + } |
| 385 | + |
| 386 | + private static AndroidConfigV2.Builder fullAndroidConfigV2Base() { |
| 387 | + return AndroidConfigV2.builder() |
| 388 | + .setCollapseKey("test-key") |
| 389 | + .setTtl(Duration.ofSeconds(5)) |
| 390 | + .setRestrictedPackageName("com.google.firebase.testing") |
| 391 | + .putData("androidFoo", "androidBar") |
| 392 | + .setFcmOptions(AndroidFcmOptions.withAnalyticsLabel("test-analytics")) |
| 393 | + .setDirectBootOk(true) |
| 394 | + .setBandwidthConstrainedOk(true) |
| 395 | + .setRestrictedSatelliteOk(true); |
| 396 | + } |
| 397 | + |
| 398 | + private static Message androidV2RemoteNotificationMessage() { |
| 399 | + AndroidConfigV2 config = fullAndroidConfigV2Base() |
| 400 | + .setRemoteNotification(AndroidRemoteNotification.builder() |
| 401 | + .setMutableContent(true) |
| 402 | + .setNotification(fullAndroidNotificationV2()) |
| 403 | + .setUseAsV1DataMessage(true) |
| 404 | + .build()) |
| 405 | + .build(); |
| 406 | + return Message.builder() |
| 407 | + .setNotification(Notification.builder() |
| 408 | + .setTitle("Title") |
| 409 | + .setBody("Body") |
| 410 | + .build()) |
| 411 | + .setAndroidConfigV2(config) |
| 412 | + .setTopic("foo-bar") |
| 413 | + .build(); |
| 414 | + } |
| 415 | + |
| 416 | + private static Message androidV2MinimalRemoteNotificationMessage() { |
| 417 | + AndroidConfigV2 config = AndroidConfigV2.builder() |
| 418 | + .setRemoteNotification(AndroidRemoteNotification.builder() |
| 419 | + .setNotification(AndroidNotificationV2.builder() |
| 420 | + .setTitle("test.title") |
| 421 | + .setBody("test.body") |
| 422 | + .build()) |
| 423 | + .build()) |
| 424 | + .build(); |
| 425 | + return Message.builder() |
| 426 | + .setAndroidConfigV2(config) |
| 427 | + .setTopic("foo-bar") |
| 428 | + .build(); |
| 429 | + } |
| 430 | + |
| 431 | + private static Message androidV2BackgroundSyncMessage() { |
| 432 | + AndroidConfigV2 config = fullAndroidConfigV2Base() |
| 433 | + .setBackgroundSync(AndroidBackgroundSyncMessage.builder().build()) |
| 434 | + .build(); |
| 435 | + return Message.builder() |
| 436 | + .setAndroidConfigV2(config) |
| 437 | + .setTopic("foo-bar") |
| 438 | + .build(); |
| 439 | + } |
| 440 | + |
| 441 | + private static Message androidV2MinimalBackgroundSyncMessage() { |
| 442 | + AndroidConfigV2 config = AndroidConfigV2.builder() |
| 443 | + .setBackgroundSync(AndroidBackgroundSyncMessage.builder().build()) |
| 444 | + .build(); |
| 445 | + return Message.builder() |
| 446 | + .setAndroidConfigV2(config) |
| 447 | + .setTopic("foo-bar") |
| 448 | + .build(); |
| 449 | + } |
300 | 450 | } |
0 commit comments