expo-notifications 1.0.0-canary-20240927-ab8a962 → 1.0.0-canary-20241018-bf4b2f7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/java/expo/modules/notifications/notifications/NotificationManager.java +8 -0
  4. package/android/src/main/java/expo/modules/notifications/notifications/NotificationSerializer.java +7 -49
  5. package/android/src/main/java/expo/modules/notifications/notifications/background/BackgroundRemoteNotificationTaskConsumer.java +2 -1
  6. package/android/src/main/java/expo/modules/notifications/notifications/channels/managers/AndroidXNotificationsChannelManager.java +4 -1
  7. package/android/src/main/java/expo/modules/notifications/notifications/debug/DebugLogging.kt +2 -2
  8. package/android/src/main/java/expo/modules/notifications/notifications/handling/NotificationsHandler.kt +3 -1
  9. package/android/src/main/java/expo/modules/notifications/notifications/handling/SingleNotificationHandlerTask.java +2 -4
  10. package/android/src/main/java/expo/modules/notifications/notifications/interfaces/INotificationContent.kt +2 -2
  11. package/android/src/main/java/expo/modules/notifications/notifications/interfaces/NotificationBuilder.kt +1 -9
  12. package/android/src/main/java/expo/modules/notifications/notifications/interfaces/NotificationListener.java +1 -1
  13. package/android/src/main/java/expo/modules/notifications/notifications/interfaces/NotificationTrigger.kt +19 -0
  14. package/android/src/main/java/expo/modules/notifications/notifications/model/NotificationAction.java +3 -0
  15. package/android/src/main/java/expo/modules/notifications/notifications/model/NotificationCategory.java +2 -0
  16. package/android/src/main/java/expo/modules/notifications/notifications/model/NotificationContent.java +1 -1
  17. package/android/src/main/java/expo/modules/notifications/notifications/model/RemoteNotificationContent.kt +1 -1
  18. package/android/src/main/java/expo/modules/notifications/notifications/model/triggers/FirebaseNotificationTrigger.kt +11 -3
  19. package/android/src/main/java/expo/modules/notifications/notifications/presentation/ExpoNotificationPresentationModule.kt +16 -2
  20. package/android/src/main/java/expo/modules/notifications/notifications/presentation/builders/BaseNotificationBuilder.kt +148 -0
  21. package/android/src/main/java/expo/modules/notifications/notifications/presentation/builders/ExpoNotificationBuilder.kt +98 -29
  22. package/android/src/main/java/expo/modules/notifications/notifications/scheduling/NotificationScheduler.kt +26 -8
  23. package/android/src/main/java/expo/modules/notifications/notifications/triggers/NotificationTriggers.kt +195 -0
  24. package/android/src/main/java/expo/modules/notifications/service/NotificationsService.kt +11 -6
  25. package/android/src/main/java/expo/modules/notifications/service/delegates/ExpoHandlingDelegate.kt +11 -0
  26. package/android/src/main/java/expo/modules/notifications/service/delegates/ExpoPresentationDelegate.kt +7 -6
  27. package/android/src/main/java/expo/modules/notifications/service/delegates/ExpoSchedulingDelegate.kt +5 -0
  28. package/android/src/main/java/expo/modules/notifications/service/delegates/FirebaseMessagingDelegate.kt +13 -2
  29. package/build/NotificationPermissions.d.ts +3 -13
  30. package/build/NotificationPermissions.d.ts.map +1 -1
  31. package/build/NotificationPermissions.js +1 -15
  32. package/build/NotificationPermissions.js.map +1 -1
  33. package/build/NotificationScheduler.types.d.ts +8 -1
  34. package/build/NotificationScheduler.types.d.ts.map +1 -1
  35. package/build/NotificationScheduler.types.js.map +1 -1
  36. package/build/Notifications.types.d.ts +36 -4
  37. package/build/Notifications.types.d.ts.map +1 -1
  38. package/build/Notifications.types.js +2 -0
  39. package/build/Notifications.types.js.map +1 -1
  40. package/build/NotificationsEmitter.d.ts.map +1 -1
  41. package/build/NotificationsEmitter.js +8 -3
  42. package/build/NotificationsEmitter.js.map +1 -1
  43. package/build/scheduleNotificationAsync.d.ts.map +1 -1
  44. package/build/scheduleNotificationAsync.js +25 -1
  45. package/build/scheduleNotificationAsync.js.map +1 -1
  46. package/ios/EXNotifications/Notifications/Emitter/EXNotificationsEmitter.h +1 -0
  47. package/ios/EXNotifications/Notifications/Emitter/EXNotificationsEmitter.m +1 -1
  48. package/ios/EXNotifications/Notifications/Scheduling/EXNotificationSchedulerModule.m +16 -0
  49. package/ios/EXNotifications/Permissions/EXUserFacingNotificationsPermissionsRequester.m +1 -1
  50. package/package.json +7 -7
  51. package/plugin/build/withNotificationsIOS.js +3 -1
  52. package/plugin/src/withNotificationsIOS.ts +3 -1
  53. package/src/NotificationPermissions.ts +2 -23
  54. package/src/NotificationScheduler.types.ts +9 -0
  55. package/src/Notifications.types.ts +34 -13
  56. package/src/NotificationsEmitter.ts +9 -3
  57. package/src/scheduleNotificationAsync.ts +32 -1
  58. package/android/src/main/java/expo/modules/notifications/notifications/interfaces/NotificationTrigger.java +0 -16
  59. package/android/src/main/java/expo/modules/notifications/notifications/presentation/builders/BaseNotificationBuilder.java +0 -50
  60. package/android/src/main/java/expo/modules/notifications/notifications/presentation/builders/CategoryAwareNotificationBuilder.java +0 -80
  61. package/android/src/main/java/expo/modules/notifications/notifications/presentation/builders/ChannelAwareNotificationBuilder.java +0 -134
  62. package/android/src/main/java/expo/modules/notifications/notifications/triggers/ChannelAwareTrigger.java +0 -49
  63. package/android/src/main/java/expo/modules/notifications/notifications/triggers/DailyTrigger.java +0 -76
  64. package/android/src/main/java/expo/modules/notifications/notifications/triggers/DateTrigger.java +0 -64
  65. package/android/src/main/java/expo/modules/notifications/notifications/triggers/TimeIntervalTrigger.java +0 -87
  66. package/android/src/main/java/expo/modules/notifications/notifications/triggers/WeeklyTrigger.java +0 -85
  67. package/android/src/main/java/expo/modules/notifications/notifications/triggers/YearlyTrigger.java +0 -94
package/CHANGELOG.md CHANGED
@@ -10,15 +10,27 @@
10
10
  ### 🎉 New features
11
11
 
12
12
  - Add clearLastNotificationResponseAsync to API. ([#31607](https://github.com/expo/expo/pull/31607) by [@douglowder](https://github.com/douglowder))
13
+ - New monthly trigger type for scheduled notifications. ([#31823](https://github.com/expo/expo/pull/31823) by [@douglowder](https://github.com/douglowder))
13
14
 
14
15
  ### 🐛 Bug fixes
15
16
 
17
+ - [ios] fix crash if expo-update reload happens while Notifications.requestPermissionsAsync() is showing native dialog ([#32096](https://github.com/expo/expo/pull/32096) by [@mfazekas](https://github.com/mfazekas))
18
+ - [android] `createNotificationChannel` could return incorrect channel information ([#32000](https://github.com/expo/expo/pull/32000) by [@vonovak](https://github.com/vonovak))
19
+ - [android] fix notifications with `ChannelAwareTrigger` not being presented ([#31999](https://github.com/expo/expo/pull/31999) by [@vonovak](https://github.com/vonovak))
20
+ - export `PermissionStatus` as value, not as type ([#31968](https://github.com/expo/expo/pull/31968) by [@vonovak](https://github.com/vonovak))
21
+ - throw improved error on invalid subscription in removeNotificationSubscription ([#31842](https://github.com/expo/expo/pull/31842) by [@vonovak](https://github.com/vonovak))
22
+ - [android] fix notifications actions not being presented ([#31795](https://github.com/expo/expo/pull/31795) by [@vonovak](https://github.com/vonovak))
16
23
  - Add missing `react` and `react-native` peer dependencies for isolated modules. ([#30478](https://github.com/expo/expo/pull/30478) by [@byCedric](https://github.com/byCedric))
24
+ - [iOS] do not overwrite existing aps entitlement. ([#31892](https://github.com/expo/expo/pull/31892) by [@douglowder](https://github.com/douglowder))
17
25
 
18
26
  ### 💡 Others
19
27
 
28
+ - [android] refactor trigger serialization ([#32032](https://github.com/expo/expo/pull/32032) by [@vonovak](https://github.com/vonovak))
29
+ - [android] simplify DateTrigger ([#32002](https://github.com/expo/expo/pull/32002) by [@vonovak](https://github.com/vonovak))
30
+ - [android] refactor ExpoNotificationBuilder ([#31838](https://github.com/expo/expo/pull/31838) by [@vonovak](https://github.com/vonovak))
20
31
  - Warn about limited support in Expo Go ([#31573](https://github.com/expo/expo/pull/31573) by [@vonovak](https://github.com/vonovak))
21
32
  - Keep using the legacy event emitter as the module is not fully migrated to Expo Modules API. ([#28946](https://github.com/expo/expo/pull/28946) by [@tsapeta](https://github.com/tsapeta))
33
+ - [Android] Convert trigger Java classes to Kotlin. ([#31856](https://github.com/expo/expo/pull/31856) by [@douglowder](https://github.com/douglowder))
22
34
 
23
35
  ## 0.28.17 - 2024-09-17
24
36
 
@@ -281,6 +293,7 @@ _This version does not introduce any user-facing changes._
281
293
 
282
294
  ### 🛠 Breaking changes
283
295
 
296
+ - remove `usePermissions` hook ([#31905](https://github.com/expo/expo/pull/31905) by [@vonovak](https://github.com/vonovak))
284
297
  - [android] Set the "notification number" (sometimes used to increment badge count on some launchers) from the notification payload `badge` field. ([#17171](https://github.com/expo/expo/pull/17171) by [@danstepanov](https://github.com/danstepanov))
285
298
 
286
299
  ### 🐛 Bug fixes
@@ -1,4 +1,5 @@
1
1
  apply plugin: 'com.android.library'
2
+ apply plugin: 'kotlin-parcelize'
2
3
 
3
4
  group = 'host.exp.exponent'
4
5
  version = '0.28.3'
@@ -30,6 +30,10 @@ public class NotificationManager implements SingletonModule, expo.modules.notifi
30
30
  public NotificationManager() {
31
31
  mListenerReferenceMap = new WeakHashMap<>();
32
32
 
33
+ // TODO @vonovak there's a chain of listeners:
34
+ // ExpoHandlingDelegate -> NotificationManager -> NotificationsEmitter
35
+ // -> NotificationsHandler
36
+ // it seems it could be shorter?
33
37
  // Registers this singleton instance in static ExpoHandlingDelegate listeners collection.
34
38
  // Since it doesn't hold strong reference to the object this should be safe.
35
39
  ExpoHandlingDelegate.Companion.addListener(this);
@@ -81,6 +85,10 @@ public class NotificationManager implements SingletonModule, expo.modules.notifi
81
85
  * Calls {@link NotificationListener#onNotificationReceived(Notification)} on all values
82
86
  * of {@link NotificationManager#mListenerReferenceMap}.
83
87
  *
88
+ * In practice, that means calling {@link NotificationsEmitter} (just emits an event to JS) and
89
+ * {@link NotificationsHandler} which calls `handleNotification` in JS to determine the behavior.
90
+ * Then `SingleNotificationHandlerTask.processNotificationWithBehavior` may present it.
91
+ *
84
92
  * @param notification Notification received
85
93
  */
86
94
  public void onNotificationReceived(Notification notification) {
@@ -32,6 +32,7 @@ import expo.modules.notifications.notifications.model.triggers.FirebaseNotificat
32
32
 
33
33
  import expo.modules.notifications.notifications.triggers.DailyTrigger;
34
34
  import expo.modules.notifications.notifications.triggers.DateTrigger;
35
+ import expo.modules.notifications.notifications.triggers.MonthlyTrigger;
35
36
  import expo.modules.notifications.notifications.triggers.TimeIntervalTrigger;
36
37
  import expo.modules.notifications.notifications.triggers.WeeklyTrigger;
37
38
  import expo.modules.notifications.notifications.triggers.YearlyTrigger;
@@ -57,11 +58,12 @@ public class NotificationSerializer {
57
58
  public static Bundle toBundle(NotificationRequest request) {
58
59
  Bundle serializedRequest = new Bundle();
59
60
  serializedRequest.putString("identifier", request.getIdentifier());
60
- serializedRequest.putBundle("trigger", toBundle(request.getTrigger()));
61
+ NotificationTrigger requestTrigger = request.getTrigger();
62
+ serializedRequest.putBundle("trigger", requestTrigger == null ? null : requestTrigger.toBundle());
61
63
  Bundle content = toBundle(request.getContent());
62
64
  Bundle existingContentData = content.getBundle("data");
63
65
  if (existingContentData == null) {
64
- if(request.getTrigger() instanceof FirebaseNotificationTrigger trigger) {
66
+ if(requestTrigger instanceof FirebaseNotificationTrigger trigger) {
65
67
  RemoteMessage message = trigger.getRemoteMessage();
66
68
  RemoteMessage.Notification notification = message.getNotification();
67
69
  Map<String, String> data = message.getData();
@@ -76,8 +78,8 @@ public class NotificationSerializer {
76
78
  content.putBundle("data", toBundle(data));
77
79
  }
78
80
  } else if(
79
- request.getTrigger() instanceof SchedulableNotificationTrigger ||
80
- request.getTrigger() == null
81
+ requestTrigger instanceof SchedulableNotificationTrigger ||
82
+ requestTrigger == null
81
83
  ) {
82
84
  JSONObject body = request.getContent().getBody();
83
85
  if (body != null) {
@@ -101,7 +103,7 @@ public class NotificationSerializer {
101
103
  public static Bundle toBundle(INotificationContent content) {
102
104
  Bundle serializedContent = new Bundle();
103
105
  serializedContent.putString("title", content.getTitle());
104
- serializedContent.putString("subtitle", content.getSubtitle());
106
+ serializedContent.putString("subtitle", content.getSubText());
105
107
  serializedContent.putString("body", content.getText());
106
108
  if (content.getColor() != null) {
107
109
  serializedContent.putString("color", String.format("#%08X", content.getColor().intValue()));
@@ -186,50 +188,6 @@ public class NotificationSerializer {
186
188
  return result;
187
189
  }
188
190
 
189
- private static Bundle toBundle(@Nullable NotificationTrigger trigger) {
190
- if (trigger == null) {
191
- return null;
192
- }
193
- Bundle bundle = new Bundle();
194
- if (trigger instanceof FirebaseNotificationTrigger) {
195
- bundle.putString("type", "push");
196
- bundle.putBundle("remoteMessage", RemoteMessageSerializer.toBundle(((FirebaseNotificationTrigger) trigger).getRemoteMessage()));
197
- } else if (trigger instanceof TimeIntervalTrigger) {
198
- bundle.putString("type", "timeInterval");
199
- bundle.putBoolean("repeats", ((TimeIntervalTrigger) trigger).isRepeating());
200
- bundle.putLong("seconds", ((TimeIntervalTrigger) trigger).getTimeInterval());
201
- } else if (trigger instanceof DateTrigger) {
202
- bundle.putString("type", "date");
203
- bundle.putBoolean("repeats", false);
204
- bundle.putLong("value", ((DateTrigger) trigger).getTriggerDate().getTime());
205
- } else if (trigger instanceof DailyTrigger) {
206
- bundle.putString("type", "daily");
207
- bundle.putInt("hour", ((DailyTrigger) trigger).getHour());
208
- bundle.putInt("minute", ((DailyTrigger) trigger).getMinute());
209
- } else if (trigger instanceof WeeklyTrigger) {
210
- bundle.putString("type", "weekly");
211
- bundle.putInt("weekday", ((WeeklyTrigger) trigger).getWeekday());
212
- bundle.putInt("hour", ((WeeklyTrigger) trigger).getHour());
213
- bundle.putInt("minute", ((WeeklyTrigger) trigger).getMinute());
214
- } else if (trigger instanceof YearlyTrigger) {
215
- bundle.putString("type", "yearly");
216
- bundle.putInt("day", ((YearlyTrigger) trigger).getDay());
217
- bundle.putInt("month", ((YearlyTrigger) trigger).getMonth());
218
- bundle.putInt("hour", ((YearlyTrigger) trigger).getHour());
219
- bundle.putInt("minute", ((YearlyTrigger) trigger).getMinute());
220
- } else {
221
- bundle.putString("type", "unknown");
222
- }
223
- bundle.putString("channelId", getChannelId(trigger));
224
-
225
- return bundle;
226
- }
227
-
228
- @Nullable
229
- private static String getChannelId(NotificationTrigger trigger) {
230
- return trigger.getNotificationChannel();
231
- }
232
-
233
191
  @NotNull
234
192
  public static Bundle toResponseBundleFromExtras(Bundle extras) {
235
193
  Bundle serializedContent = new Bundle();
@@ -25,9 +25,10 @@ import expo.modules.interfaces.taskManager.TaskManagerUtilsInterface;
25
25
  /**
26
26
  * Represents a task to be run when the app is receives a remote push
27
27
  * notification. Map of current tasks is maintained in {@link FirebaseMessagingDelegate}.
28
+ *
29
+ * Instances are instantiated by expo task manager, after being registered in ExpoBackgroundNotificationTasksModule
28
30
  */
29
31
  public class BackgroundRemoteNotificationTaskConsumer extends TaskConsumer implements TaskConsumerInterface {
30
- private static final String TAG = BackgroundRemoteNotificationTaskConsumer.class.getSimpleName();
31
32
  private static final String NOTIFICATION_KEY = "notification";
32
33
 
33
34
  private TaskInterface mTask;
@@ -80,7 +80,10 @@ public class AndroidXNotificationsChannelManager implements NotificationsChannel
80
80
  NotificationChannel channel = new NotificationChannel(channelId, name, importance);
81
81
  configureChannelWithOptions(channel, channelOptions);
82
82
  mNotificationManager.createNotificationChannel(channel);
83
- return channel;
83
+ // We return the channel given by mNotificationManager, not the one we created.
84
+ // see "Note that the created channel may differ from this value." ("this value" = the one we provided)
85
+ // https://developer.android.com/reference/android/app/NotificationManager#createNotificationChannel(android.app.NotificationChannel)
86
+ return mNotificationManager.getNotificationChannel(channelId);
84
87
  }
85
88
 
86
89
  // Processing options
@@ -70,10 +70,10 @@ object DebugLogging {
70
70
  """
71
71
  $caller:
72
72
  notification.notificationRequest.content.title: ${notification.notificationRequest.content.title}
73
- notification.notificationRequest.content.subtitle: ${notification.notificationRequest.content.subtitle}
73
+ notification.notificationRequest.content.subText: ${notification.notificationRequest.content.subText}
74
74
  notification.notificationRequest.content.text: ${notification.notificationRequest.content.text}
75
75
  notification.notificationRequest.content.sound: ${notification.notificationRequest.content.soundName}
76
- notification.notificationRequest.content.channelID: ${notification.notificationRequest.trigger.notificationChannel}
76
+ notification.notificationRequest.content.channelID: ${notification.notificationRequest.trigger.getNotificationChannel()}
77
77
  notification.notificationRequest.content.body: ${notification.notificationRequest.content.body}
78
78
  notification.notificationRequest.content.color: ${notification.notificationRequest.content.color}
79
79
  notification.notificationRequest.content.vibrationPattern: ${notification.notificationRequest.content.vibrationPattern?.contentToString()}
@@ -99,7 +99,7 @@ open class NotificationsHandler : Module(), NotificationListener {
99
99
  ?: throw NotificationWasAlreadyHandledException(identifier)
100
100
 
101
101
  with(behavior) {
102
- task.handleResponse(
102
+ task.processNotificationWithBehavior(
103
103
  NotificationBehavior(shouldShowAlert, shouldPlaySound, shouldSetBadge, priority),
104
104
  promise
105
105
  )
@@ -110,6 +110,8 @@ open class NotificationsHandler : Module(), NotificationListener {
110
110
  * Callback called by [NotificationManager] to inform its listeners of new messages.
111
111
  * Starts up a new [SingleNotificationHandlerTask] which will take it on from here.
112
112
  *
113
+ * SingleNotificationHandlerTask.processNotificationWithBehavior can then present it
114
+ *
113
115
  * @param notification Notification received
114
116
  */
115
117
  override fun onNotificationReceived(notification: Notification) {
@@ -34,7 +34,6 @@ public class SingleNotificationHandlerTask {
34
34
  private Handler mHandler;
35
35
  private EventEmitter mEventEmitter;
36
36
  private Notification mNotification;
37
- private NotificationBehavior mBehavior;
38
37
  private Context mContext;
39
38
  private NotificationsHandler mDelegate;
40
39
 
@@ -88,12 +87,11 @@ public class SingleNotificationHandlerTask {
88
87
  * @param behavior Behavior requested by the app
89
88
  * @param promise Promise to fulfill once the behavior is applied to the notification.
90
89
  */
91
- /* package */ void handleResponse(NotificationBehavior behavior, final Promise promise) {
92
- mBehavior = behavior;
90
+ /* package */ void processNotificationWithBehavior(final NotificationBehavior behavior, final Promise promise) {
93
91
  mHandler.post(new Runnable() {
94
92
  @Override
95
93
  public void run() {
96
- NotificationsService.Companion.present(mContext, mNotification, mBehavior, new ResultReceiver(mHandler) {
94
+ NotificationsService.Companion.present(mContext, mNotification, behavior, new ResultReceiver(mHandler) {
97
95
  @Override
98
96
  protected void onReceiveResult(int resultCode, Bundle resultData) {
99
97
  super.onReceiveResult(resultCode, resultData);
@@ -17,7 +17,7 @@ import org.json.JSONObject
17
17
  interface INotificationContent : Parcelable {
18
18
  val title: String?
19
19
  val text: String?
20
- val subtitle: String?
20
+ val subText: String?
21
21
  val badgeCount: Number?
22
22
  val shouldPlayDefaultSound: Boolean
23
23
 
@@ -27,7 +27,7 @@ interface INotificationContent : Parcelable {
27
27
  val shouldUseDefaultVibrationPattern: Boolean
28
28
  val vibrationPattern: LongArray?
29
29
  val body: JSONObject?
30
- val priority: NotificationPriority
30
+ val priority: NotificationPriority?
31
31
  val color: Number?
32
32
  val isAutoDismiss: Boolean
33
33
  val categoryId: String?
@@ -8,14 +8,6 @@ import expo.modules.notifications.notifications.model.NotificationBehavior
8
8
  * on a [NotificationContent] spec.
9
9
  */
10
10
  interface NotificationBuilder {
11
- /**
12
- * Pass in a [Notification] based on which the Android notification should be based.
13
- *
14
- * @param notification [Notification] on which the notification should be based.
15
- * @return The same instance of [NotificationBuilder] updated with the notification.
16
- */
17
- fun setNotification(notification: Notification?): NotificationBuilder?
18
-
19
11
  /**
20
12
  * Pass in a [NotificationBehavior] if you want to override the behavior
21
13
  * of the notification, i.e. whether it should show a heads-up alert, set badge, etc.
@@ -23,7 +15,7 @@ interface NotificationBuilder {
23
15
  * @param behavior [NotificationBehavior] to which the presentation effect should conform.
24
16
  * @return The same instance of [NotificationBuilder] updated with the remote message.
25
17
  */
26
- fun setAllowedBehavior(behavior: NotificationBehavior?): NotificationBuilder?
18
+ fun setAllowedBehavior(behavior: NotificationBehavior?): NotificationBuilder
27
19
 
28
20
  /**
29
21
  * Builds the Android notification based on passed in data.
@@ -13,7 +13,7 @@ import expo.modules.notifications.notifications.model.NotificationResponse;
13
13
  */
14
14
  public interface NotificationListener {
15
15
  /**
16
- * Callback called when new notification is received.
16
+ * Callback called when new notification is received while the app is in foreground.
17
17
  *
18
18
  * @param notification Notification received
19
19
  */
@@ -0,0 +1,19 @@
1
+ package expo.modules.notifications.notifications.interfaces
2
+
3
+ import android.os.Bundle
4
+ import android.os.Parcelable
5
+
6
+ /**
7
+ * An interface specifying source of the notification, to be implemented
8
+ * by concrete classes.
9
+ */
10
+ interface NotificationTrigger : Parcelable {
11
+ // these are functions so that we're absolutely sure @Parcelize doesn't try to parcelize them
12
+ // (as opposed to if they were properties)
13
+
14
+ fun getNotificationChannel(): String? {
15
+ return null
16
+ }
17
+
18
+ fun toBundle(): Bundle
19
+ }
@@ -7,6 +7,9 @@ import java.io.Serializable;
7
7
 
8
8
  /**
9
9
  * A class representing a single notification action button.
10
+ *
11
+ * TODO vonovak: no need to implement serializable, parcelable is enough for storing
12
+ *
10
13
  */
11
14
  public class NotificationAction implements Parcelable, Serializable {
12
15
  private final String mIdentifier;
@@ -9,6 +9,8 @@ import java.util.List;
9
9
 
10
10
  /**
11
11
  * A class representing a collection of notification actions.
12
+ *
13
+ * TODO vonovak: no need to implement serializable, parcelable is enough for storing
12
14
  */
13
15
  public class NotificationCategory implements Parcelable, Serializable {
14
16
  private final String mIdentifier;
@@ -75,7 +75,7 @@ public class NotificationContent implements Parcelable, Serializable, INotificat
75
75
  }
76
76
 
77
77
  @Nullable
78
- public String getSubtitle() {
78
+ public String getSubText() {
79
79
  return mSubtitle;
80
80
  }
81
81
 
@@ -75,7 +75,7 @@ class RemoteNotificationContent(private val remoteMessage: RemoteMessage) : INot
75
75
  override val isSticky: Boolean
76
76
  get() = remoteMessage.data["sticky"]?.toBoolean() ?: false
77
77
 
78
- override val subtitle: String?
78
+ override val subText: String?
79
79
  get() = remoteMessage.data["subtitle"]
80
80
 
81
81
  override val badgeCount: Number?
@@ -1,30 +1,38 @@
1
1
  package expo.modules.notifications.notifications.model.triggers
2
2
 
3
3
  import android.os.Build
4
+ import android.os.Bundle
4
5
  import android.os.Parcel
5
6
  import android.os.Parcelable
6
7
  import androidx.annotation.RequiresApi
8
+ import androidx.core.os.bundleOf
7
9
  import com.google.firebase.messaging.RemoteMessage
10
+ import expo.modules.notifications.notifications.RemoteMessageSerializer
8
11
  import expo.modules.notifications.notifications.interfaces.NotificationTrigger
9
12
 
10
13
  /**
11
14
  * A trigger representing an incoming remote Firebase notification.
12
15
  */
13
- class FirebaseNotificationTrigger(private val remoteMessage: RemoteMessage) : NotificationTrigger {
16
+ class FirebaseNotificationTrigger(val remoteMessage: RemoteMessage) : NotificationTrigger {
14
17
 
15
18
  private constructor(parcel: Parcel) : this(
16
19
  parcel.readParcelable(FirebaseNotificationTrigger::class.java.classLoader)
17
20
  ?: throw IllegalArgumentException("RemoteMessage from readParcelable must not be null")
18
21
  )
19
22
 
20
- fun getRemoteMessage(): RemoteMessage = remoteMessage
21
-
22
23
  @RequiresApi(api = Build.VERSION_CODES.O)
23
24
  override fun getNotificationChannel(): String? {
24
25
  val channelId = remoteMessage.notification?.channelId ?: remoteMessage.data["channelId"]
25
26
  return channelId ?: super.getNotificationChannel()
26
27
  }
27
28
 
29
+ override fun toBundle(): Bundle {
30
+ return bundleOf(
31
+ "type" to "push",
32
+ "remoteMessage" to RemoteMessageSerializer.toBundle(remoteMessage)
33
+ )
34
+ }
35
+
28
36
  override fun describeContents(): Int {
29
37
  return 0
30
38
  }
@@ -11,6 +11,8 @@ import expo.modules.notifications.ResultReceiverBody
11
11
  import expo.modules.notifications.createDefaultResultReceiver
12
12
  import expo.modules.notifications.notifications.ArgumentsNotificationContentBuilder
13
13
  import expo.modules.notifications.notifications.NotificationSerializer
14
+ import expo.modules.notifications.notifications.interfaces.INotificationContent
15
+ import expo.modules.notifications.notifications.interfaces.NotificationTrigger
14
16
  import expo.modules.notifications.notifications.model.Notification
15
17
  import expo.modules.notifications.notifications.model.NotificationRequest
16
18
  import expo.modules.notifications.service.NotificationsService
@@ -31,7 +33,7 @@ open class ExpoNotificationPresentationModule : Module() {
31
33
 
32
34
  AsyncFunction("presentNotificationAsync") { identifier: String, payload: ReadableArguments, promise: Promise ->
33
35
  val content = ArgumentsNotificationContentBuilder(context).setPayload(payload).build()
34
- val request = NotificationRequest(identifier, content, null)
36
+ val request = createNotificationRequest(identifier, content, null)
35
37
  val notification = Notification(request)
36
38
  present(
37
39
  context,
@@ -54,7 +56,7 @@ open class ExpoNotificationPresentationModule : Module() {
54
56
  createResultReceiver { resultCode: Int, resultData: Bundle? ->
55
57
  val notifications = resultData?.getParcelableArrayList<Notification>(NotificationsService.NOTIFICATIONS_KEY)
56
58
  if (resultCode == NotificationsService.SUCCESS_CODE && notifications != null) {
57
- promise.resolve(notifications.map(NotificationSerializer::toBundle))
59
+ promise.resolve(serializeNotifications(notifications))
58
60
  } else {
59
61
  val e = resultData?.getSerializable(NotificationsService.EXCEPTION_KEY) as? Exception
60
62
  promise.reject("ERR_NOTIFICATIONS_FETCH_FAILED", "A list of displayed notifications could not be fetched.", e)
@@ -96,4 +98,16 @@ open class ExpoNotificationPresentationModule : Module() {
96
98
  }
97
99
  )
98
100
  }
101
+
102
+ protected open fun createNotificationRequest(
103
+ identifier: String,
104
+ content: INotificationContent,
105
+ trigger: NotificationTrigger?
106
+ ): NotificationRequest {
107
+ return NotificationRequest(identifier, content, null)
108
+ }
109
+
110
+ protected open fun serializeNotifications(notifications: Collection<Notification>): List<Bundle> {
111
+ return notifications.map(NotificationSerializer::toBundle)
112
+ }
99
113
  }
@@ -0,0 +1,148 @@
1
+ package expo.modules.notifications.notifications.presentation.builders
2
+
3
+ import android.app.NotificationChannel
4
+ import android.app.NotificationManager
5
+ import android.content.Context
6
+ import android.os.Build
7
+ import android.util.Log
8
+ import androidx.annotation.RequiresApi
9
+ import androidx.core.app.NotificationCompat
10
+ import expo.modules.notifications.R
11
+ import expo.modules.notifications.notifications.channels.managers.AndroidXNotificationsChannelGroupManager
12
+ import expo.modules.notifications.notifications.channels.managers.AndroidXNotificationsChannelManager
13
+ import expo.modules.notifications.notifications.channels.managers.NotificationsChannelManager
14
+ import expo.modules.notifications.notifications.interfaces.INotificationContent
15
+ import expo.modules.notifications.notifications.interfaces.NotificationBuilder
16
+ import expo.modules.notifications.notifications.model.Notification
17
+ import expo.modules.notifications.notifications.model.NotificationBehavior
18
+
19
+ /**
20
+ * A foundation class for [NotificationBuilder] implementations. Takes care
21
+ * of accepting [.mNotification] and [.mNotificationBehavior].
22
+ */
23
+ abstract class BaseNotificationBuilder protected constructor(protected val context: Context, protected val notification: Notification) :
24
+ NotificationBuilder {
25
+
26
+ protected var notificationBehavior: NotificationBehavior? = null
27
+ private set
28
+
29
+ override fun setAllowedBehavior(behavior: NotificationBehavior?): NotificationBuilder {
30
+ notificationBehavior = behavior
31
+ return this
32
+ }
33
+
34
+ fun createBuilder(): NotificationCompat.Builder {
35
+ val builder = channelId?.let { NotificationCompat.Builder(context, it) } ?: NotificationCompat.Builder(context)
36
+ return builder
37
+ }
38
+
39
+ protected val channelId: String?
40
+ /**
41
+ * @return A [NotificationChannel]'s identifier to use for the notification.
42
+ */
43
+ get() {
44
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
45
+ // Returning null on incompatible platforms won't be an error.
46
+ return null
47
+ }
48
+
49
+ val trigger = notification.notificationRequest?.trigger
50
+ if (trigger == null) {
51
+ Log.e(
52
+ "notifications",
53
+ String.format(
54
+ "Couldn't get channel for the notifications - trigger is 'null'. Fallback to '%s' channel",
55
+ FALLBACK_CHANNEL_ID
56
+ )
57
+ )
58
+ return fallbackNotificationChannel!!.id
59
+ }
60
+
61
+ val requestedChannelId = trigger.getNotificationChannel()
62
+ ?: return fallbackNotificationChannel!!.id
63
+
64
+ val channelForRequestedId =
65
+ notificationsChannelManager.getNotificationChannel(requestedChannelId)
66
+ if (channelForRequestedId == null) {
67
+ Log.e(
68
+ "notifications",
69
+ String.format(
70
+ "Channel '%s' doesn't exists. Fallback to '%s' channel",
71
+ requestedChannelId,
72
+ FALLBACK_CHANNEL_ID
73
+ )
74
+ )
75
+ return fallbackNotificationChannel!!.id
76
+ }
77
+
78
+ return channelForRequestedId.id
79
+ }
80
+
81
+ open val notificationsChannelManager: NotificationsChannelManager
82
+ get() = AndroidXNotificationsChannelManager(
83
+ context,
84
+ AndroidXNotificationsChannelGroupManager(context)
85
+ )
86
+
87
+ private val fallbackNotificationChannel: NotificationChannel?
88
+ /**
89
+ * Fetches the fallback notification channel, and if it doesn't exist yet - creates it.
90
+ *
91
+ *
92
+ * Returns null on [NotificationChannel]-incompatible platforms.
93
+ *
94
+ * @return Fallback [NotificationChannel] or null.
95
+ */
96
+ get() {
97
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
98
+ return null
99
+ }
100
+
101
+ val channel = notificationManager.getNotificationChannel(FALLBACK_CHANNEL_ID)
102
+ return channel ?: createFallbackChannel()
103
+ }
104
+
105
+ /**
106
+ * Creates a fallback channel of [.FALLBACK_CHANNEL_ID] ID, name fetched
107
+ * from Android resources ([.getFallbackChannelName]
108
+ * and importance set by [.FALLBACK_CHANNEL_IMPORTANCE].
109
+ *
110
+ * @return Newly created channel.
111
+ */
112
+ @RequiresApi(api = Build.VERSION_CODES.O)
113
+ protected fun createFallbackChannel(): NotificationChannel {
114
+ val channel = NotificationChannel(
115
+ FALLBACK_CHANNEL_ID,
116
+ fallbackChannelName,
117
+ FALLBACK_CHANNEL_IMPORTANCE
118
+ )
119
+ channel.setShowBadge(true)
120
+ channel.enableVibration(true)
121
+ notificationManager.createNotificationChannel(channel)
122
+ return channel
123
+ }
124
+
125
+ private val fallbackChannelName: String
126
+ /**
127
+ * Fetches fallback channel name from Android resources. Overridable by Android resources system
128
+ *
129
+ * @return Name of the fallback channel
130
+ */
131
+ get() = context.getString(R.string.expo_notifications_fallback_channel_name)
132
+
133
+ private val notificationManager: NotificationManager
134
+ get() = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
135
+
136
+ companion object {
137
+ private const val FALLBACK_CHANNEL_ID = "expo_notifications_fallback_notification_channel"
138
+
139
+ // Behaviors we will want to impose on received notifications include
140
+ // being displayed as a heads-up notification. For that we will need
141
+ // a channel of high importance.
142
+ @RequiresApi(api = Build.VERSION_CODES.N)
143
+ private val FALLBACK_CHANNEL_IMPORTANCE = NotificationManager.IMPORTANCE_HIGH
144
+ }
145
+
146
+ protected val notificationContent: INotificationContent
147
+ get() = notification.notificationRequest.content
148
+ }