|
3 | 3 | package com.urbanairship.reactnative;
|
4 | 4 |
|
5 | 5 | import android.Manifest;
|
| 6 | +import android.app.NotificationManager; |
6 | 7 | import android.content.Context;
|
7 | 8 | import android.content.Intent;
|
8 | 9 | import android.content.SharedPreferences;
|
|
11 | 12 | import android.os.Build;
|
12 | 13 | import android.os.Bundle;
|
13 | 14 | import android.preference.PreferenceManager;
|
| 15 | +import android.service.notification.StatusBarNotification; |
14 | 16 | import android.support.annotation.NonNull;
|
| 17 | +import android.support.v4.app.NotificationManagerCompat; |
15 | 18 | import android.support.v4.content.ContextCompat;
|
16 | 19 | import android.support.v4.os.AsyncTaskCompat;
|
| 20 | +import android.util.Log; |
17 | 21 |
|
18 | 22 | import com.facebook.react.bridge.Arguments;
|
19 | 23 | import com.facebook.react.bridge.Dynamic;
|
|
34 | 38 | import com.urbanairship.actions.ActionRunRequest;
|
35 | 39 | import com.urbanairship.actions.LandingPageActivity;
|
36 | 40 | import com.urbanairship.analytics.AssociatedIdentifiers;
|
| 41 | +import com.urbanairship.push.PushMessage; |
37 | 42 | import com.urbanairship.push.TagGroupsEditor;
|
38 | 43 | import com.urbanairship.reactnative.events.NotificationOptInEvent;
|
| 44 | +import com.urbanairship.reactnative.events.PushReceivedEvent; |
39 | 45 | import com.urbanairship.richpush.RichPushInbox;
|
40 | 46 | import com.urbanairship.richpush.RichPushMessage;
|
| 47 | +import com.urbanairship.util.UAStringUtil; |
41 | 48 |
|
42 | 49 | import java.util.Calendar;
|
43 | 50 | import java.util.Date;
|
@@ -598,12 +605,84 @@ public void markInboxMessageRead(String messageId, Promise promise) {
|
598 | 605 | }
|
599 | 606 | }
|
600 | 607 |
|
| 608 | + @ReactMethod |
| 609 | + public void clearNotifications() { |
| 610 | + NotificationManagerCompat.from(UAirship.getApplicationContext()).cancelAll(); |
| 611 | + } |
| 612 | + |
| 613 | + @ReactMethod |
| 614 | + public void clearNotification(String identifier) { |
| 615 | + if (UAStringUtil.isEmpty(identifier)) { |
| 616 | + return; |
| 617 | + } |
| 618 | + |
| 619 | + String[] parts = identifier.split(":", 2); |
| 620 | + if (parts.length == 0) { |
| 621 | + Log.e(getName(), "Invalid identifier: " + identifier); |
| 622 | + return; |
| 623 | + } |
| 624 | + |
| 625 | + int id; |
| 626 | + String tag = null; |
| 627 | + try { |
| 628 | + id = Integer.valueOf(parts[0]); |
| 629 | + } catch (NumberFormatException e) { |
| 630 | + Log.e(getName(), "Invalid identifier: " + identifier); |
| 631 | + return; |
| 632 | + } |
| 633 | + |
| 634 | + if (parts.length == 2) { |
| 635 | + tag = parts[1]; |
| 636 | + } |
| 637 | + |
| 638 | + |
| 639 | + NotificationManagerCompat.from(UAirship.getApplicationContext()).cancel(tag, id); |
| 640 | + } |
| 641 | + |
| 642 | + |
| 643 | + |
601 | 644 | /**
|
602 |
| - * Forces the inbox to refresh. This is normally not needed as the inbox will automatically refresh on foreground or when a push arrives thats associated with a message. |
| 645 | + * Retrieves the current inbox messages. |
603 | 646 | *
|
604 | 647 | * @param promise The JS promise.
|
605 | 648 | */
|
606 | 649 | @ReactMethod
|
| 650 | + public void getActiveNotifications(Promise promise) { |
| 651 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 652 | + WritableArray notifications = Arguments.createArray(); |
| 653 | + |
| 654 | + NotificationManager notificationManager = (NotificationManager) UAirship.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); |
| 655 | + StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications(); |
| 656 | + |
| 657 | + for (StatusBarNotification statusBarNotification : statusBarNotifications) { |
| 658 | + int id = statusBarNotification.getId(); |
| 659 | + String tag = statusBarNotification.getTag(); |
| 660 | + |
| 661 | + PushMessage pushMessage; |
| 662 | + Bundle extras = statusBarNotification.getNotification().extras; |
| 663 | + if (extras != null && extras.containsKey("push_message")) { |
| 664 | + pushMessage = new PushMessage(extras.getBundle("push_message")); |
| 665 | + } else { |
| 666 | + pushMessage = new PushMessage(new Bundle()); |
| 667 | + } |
| 668 | + |
| 669 | + notifications.pushMap(new PushReceivedEvent(pushMessage, id, tag).getBody()); |
| 670 | + } |
| 671 | + |
| 672 | + promise.resolve(notifications); |
| 673 | + } else { |
| 674 | + promise.reject("UNSUPPORTED", "Getting active notifications is only supported on Marshmallow and newer devices."); |
| 675 | + } |
| 676 | + } |
| 677 | + |
| 678 | + |
| 679 | + |
| 680 | + /** |
| 681 | + * Forces the inbox to refresh. This is normally not needed as the inbox will automatically refresh on foreground or when a push arrives thats associated with a message. |
| 682 | + * |
| 683 | + * @param promise The JS promise. |
| 684 | + */ |
| 685 | + @ReactMethod |
607 | 686 | public void refreshInbox(final Promise promise) {
|
608 | 687 | UAirship.shared().getInbox().fetchMessages(new RichPushInbox.FetchMessagesCallback() {
|
609 | 688 | @Override
|
|
0 commit comments