Skip to content

feat(android): bubble action #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ repositories {
}

dependencies {
// For NotificationCompact.Builder.addPerson(person)
def core_version = "1.5.0-rc01"

// Java language implementation
implementation "androidx.core:core:$core_version"
implementation "androidx.core:app:$core_version"
api 'androidx.annotation:annotation:1.1.0' // https://developer.android.com/jetpack/androidx/releases/annotation
api "com.squareup.okhttp3:okhttp:3.12.12" // okhttp must stay on 3.12.x to support minSdkVersion < 21
api 'androidx.concurrent:concurrent-futures:1.1.0' // https://developer.android.com/jetpack/androidx/releases/concurrent
Expand Down
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

<application>
<!-- Receiver Service -->
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/app/notifee/core/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void post(Object event) {
getInstance().getDefault().post(event);
}

static void postSticky(Object event) {
public static void postSticky(Object event) {
getInstance().getDefault().postSticky(event);
}

Expand Down
173 changes: 137 additions & 36 deletions android/src/main/java/app/notifee/core/NotificationManager.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package app.notifee.core;

import static app.notifee.core.ReceiverService.ACTION_PRESS_INTENT;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.concurrent.futures.CallbackToFutureAdapter;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
import androidx.core.app.RemoteInput;
import androidx.core.graphics.drawable.IconCompat;
import androidx.work.Data;
Expand All @@ -23,21 +24,11 @@
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.WorkQuery;
import app.notifee.core.database.WorkDataEntity;
import app.notifee.core.database.WorkDataRepository;
import app.notifee.core.event.NotificationEvent;
import app.notifee.core.model.IntervalTriggerModel;
import app.notifee.core.model.NotificationAndroidActionModel;
import app.notifee.core.model.NotificationAndroidModel;
import app.notifee.core.model.NotificationAndroidStyleModel;
import app.notifee.core.model.NotificationModel;
import app.notifee.core.model.TimestampTriggerModel;
import app.notifee.core.utility.ObjectUtils;
import app.notifee.core.utility.ResourceUtils;
import app.notifee.core.utility.TextUtils;

import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -49,6 +40,27 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import app.notifee.core.database.WorkDataEntity;
import app.notifee.core.database.WorkDataRepository;
import app.notifee.core.event.MainComponentEvent;
import app.notifee.core.event.NotificationEvent;
import app.notifee.core.model.IntervalTriggerModel;
import app.notifee.core.model.NotificationAndroidActionModel;
import app.notifee.core.model.NotificationAndroidBubbleActionModel;
import app.notifee.core.model.NotificationAndroidModel;
import app.notifee.core.model.NotificationAndroidPersonModel;
import app.notifee.core.model.NotificationAndroidPressActionModel;
import app.notifee.core.model.NotificationAndroidStyleModel;
import app.notifee.core.model.NotificationModel;
import app.notifee.core.model.TimestampTriggerModel;
import app.notifee.core.utility.IntentUtils;
import app.notifee.core.utility.ObjectUtils;
import app.notifee.core.utility.ResourceUtils;
import app.notifee.core.utility.TextUtils;

import static app.notifee.core.ContextHolder.getApplicationContext;
import static app.notifee.core.ReceiverService.ACTION_PRESS_INTENT;

class NotificationManager {
private static final String TAG = "NotificationManager";
private static final ExecutorService CACHED_THREAD_POOL = Executors.newCachedThreadPool();
Expand All @@ -67,8 +79,7 @@ private static Task<NotificationCompat.Builder> notificationBundleToBuilder(
() -> {
Boolean hasCustomSound = false;
NotificationCompat.Builder builder =
new NotificationCompat.Builder(
ContextHolder.getApplicationContext(), androidModel.getChannelId());
new NotificationCompat.Builder(getApplicationContext(), androidModel.getChannelId());

// must always keep at top
builder.setExtras(notificationModel.getData());
Expand Down Expand Up @@ -243,6 +254,42 @@ private static Task<NotificationCompat.Builder> notificationBundleToBuilder(
return builder;
};

/*
* A task continuation for full-screen action, if specified.
*/
Continuation<NotificationCompat.Builder, NotificationCompat.Builder>
fullScreenActionContinuation =
task -> {
NotificationCompat.Builder builder = task.getResult();
if (androidModel.hasFullScreenAction()) {
NotificationAndroidPressActionModel fullScreenActionBundle =
androidModel.getFullScreenAction();

String launchActivity = fullScreenActionBundle.getLaunchActivity();
Class launchActivityClass = IntentUtils.getLaunchActivity(launchActivity);
Intent launchIntent = new Intent(getApplicationContext(), launchActivityClass);
if (fullScreenActionBundle.getLaunchActivityFlags() != -1) {
launchIntent.addFlags(fullScreenActionBundle.getLaunchActivityFlags());
}

if (fullScreenActionBundle.getMainComponent() != null) {
launchIntent.putExtra("mainComponent", fullScreenActionBundle.getMainComponent());
EventBus.postSticky(
new MainComponentEvent(fullScreenActionBundle.getMainComponent()));
}

PendingIntent fullScreenPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
notificationModel.getHashCode(),
launchIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setFullScreenIntent(fullScreenPendingIntent, true);
}

return builder;
};

/*
* A task continuation that builds all actions, if any. Additionally fetches
* icon bitmaps through Fresco.
Expand Down Expand Up @@ -303,6 +350,31 @@ private static Task<NotificationCompat.Builder> notificationBundleToBuilder(
return builder;
};

/*
* A task continuation that builds the notification bubble action, if any.
*/
Continuation<NotificationCompat.Builder, NotificationCompat.Builder> bubbleActionContinuation =
task -> {
NotificationCompat.Builder builder = task.getResult();
NotificationAndroidBubbleActionModel androidBubbleActionBundle = androidModel.getBubbleAction();
if (androidBubbleActionBundle == null) {
return builder;
}

Task<NotificationCompat.BubbleMetadata> bubbleActionTask =
androidBubbleActionBundle.getBubbleActionTask(CACHED_THREAD_POOL, notificationModel);
if (bubbleActionTask == null) {
return builder;
}

NotificationCompat.BubbleMetadata bubbleMetadata = Tasks.await(bubbleActionTask);
if (bubbleMetadata != null) {
builder.setBubbleMetadata(bubbleMetadata);
}

return builder;
};

/*
* A task continuation that builds the notification style, if any. Additionally
* fetches any image bitmaps (e.g. Person image, or BigPicture image) through
Expand Down Expand Up @@ -330,9 +402,44 @@ private static Task<NotificationCompat.Builder> notificationBundleToBuilder(
return builder;
};


/*
* A task continuation that builds the notification style, if any. Additionally
* fetches any image bitmaps (e.g. Person image, or BigPicture image) through
* Fresco.
*/
Continuation<NotificationCompat.Builder, NotificationCompat.Builder> personContinuation =
task -> {
NotificationCompat.Builder builder = task.getResult();
NotificationAndroidPersonModel androidPersonBundle = androidModel.getPerson();
if (androidPersonBundle == null) {
return builder;
}

Task<Person> personTask =
androidPersonBundle.buildPersonTask(CACHED_THREAD_POOL);
if (personTask == null) {
return builder;
}

Person person = Tasks.await(personTask);
if (person != null) {
// https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder?hl=zh-cn#addPerson(androidx.core.app.Person)
builder.addPerson(person);
}

return builder;
};

return Tasks.call(CACHED_THREAD_POOL, builderCallable)
// get a large image bitmap if largeIcon is set
.continueWith(CACHED_THREAD_POOL, largeIconContinuation)
// set a person, if person is set
.continueWith(CACHED_THREAD_POOL, personContinuation)
// set full screen action, if fullScreenAction is set
.continueWith(CACHED_THREAD_POOL, fullScreenActionContinuation)
// set bubble action, if bubbleAction is set
.continueWith(CACHED_THREAD_POOL, bubbleActionContinuation)
// build notification actions, tasks based to allow image fetching
.continueWith(CACHED_THREAD_POOL, actionsContinuation)
// build notification style, tasks based to allow image fetching
Expand All @@ -344,7 +451,7 @@ static Task<Void> cancelNotification(
return Tasks.call(
() -> {
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(ContextHolder.getApplicationContext());
NotificationManagerCompat.from(getApplicationContext());

if (notificationType == NOTIFICATION_TYPE_DISPLAYED
|| notificationType == NOTIFICATION_TYPE_ALL) {
Expand All @@ -353,13 +460,12 @@ static Task<Void> cancelNotification(

if (notificationType == NOTIFICATION_TYPE_TRIGGER
|| notificationType == NOTIFICATION_TYPE_ALL) {
WorkManager.getInstance(ContextHolder.getApplicationContext())
WorkManager.getInstance(getApplicationContext())
.cancelUniqueWork("trigger:" + notificationId);
}

// delete notification entry from database
WorkDataRepository.getInstance(ContextHolder.getApplicationContext())
.deleteById(notificationId);
WorkDataRepository.getInstance(getApplicationContext()).deleteById(notificationId);
return null;
});
}
Expand All @@ -368,7 +474,7 @@ static Task<Void> cancelAllNotifications(@NonNull int notificationType) {
return Tasks.call(
() -> {
NotificationManagerCompat notificationManagerCompat =
NotificationManagerCompat.from(ContextHolder.getApplicationContext());
NotificationManagerCompat.from(getApplicationContext());

if (notificationType == NOTIFICATION_TYPE_DISPLAYED
|| notificationType == NOTIFICATION_TYPE_ALL) {
Expand All @@ -377,16 +483,15 @@ static Task<Void> cancelAllNotifications(@NonNull int notificationType) {

if (notificationType == NOTIFICATION_TYPE_TRIGGER
|| notificationType == NOTIFICATION_TYPE_ALL) {
WorkManager workManager =
WorkManager.getInstance(ContextHolder.getApplicationContext());
WorkManager workManager = WorkManager.getInstance(getApplicationContext());
workManager.cancelAllWorkByTag(Worker.WORK_TYPE_NOTIFICATION_TRIGGER);

// Remove all cancelled and finished work from its internal database
// states include SUCCEEDED, FAILED and CANCELLED
workManager.pruneWork();

// delete all from database
WorkDataRepository.getInstance(ContextHolder.getApplicationContext()).deleteAll();
WorkDataRepository.getInstance(getApplicationContext()).deleteAll();
}

return null;
Expand All @@ -406,7 +511,7 @@ static Task<Void> displayNotification(NotificationModel notificationModel) {
if (androidBundle.getAsForegroundService()) {
ForegroundService.start(hashCode, notification, notificationModel.toBundle());
} else {
NotificationManagerCompat.from(ContextHolder.getApplicationContext())
NotificationManagerCompat.from(getApplicationContext())
.notify(hashCode, notification);
}

Expand Down Expand Up @@ -444,15 +549,15 @@ static void createIntervalTriggerNotification(
NotificationModel notificationModel, Bundle triggerBundle) {
IntervalTriggerModel trigger = IntervalTriggerModel.fromBundle(triggerBundle);
String uniqueWorkName = "trigger:" + notificationModel.getId();
WorkManager workManager = WorkManager.getInstance(ContextHolder.getApplicationContext());
WorkManager workManager = WorkManager.getInstance(getApplicationContext());

Data.Builder workDataBuilder =
new Data.Builder()
.putString(Worker.KEY_WORK_TYPE, Worker.WORK_TYPE_NOTIFICATION_TRIGGER)
.putString(Worker.KEY_WORK_REQUEST, Worker.WORK_REQUEST_PERIODIC)
.putString("id", notificationModel.getId());

WorkDataRepository.getInstance(ContextHolder.getApplicationContext())
WorkDataRepository.getInstance(getApplicationContext())
.insertTriggerNotification(notificationModel, triggerBundle);

long interval = trigger.getInterval();
Expand All @@ -473,7 +578,7 @@ static void createTimestampTriggerNotification(
TimestampTriggerModel trigger = TimestampTriggerModel.fromBundle(triggerBundle);

String uniqueWorkName = "trigger:" + notificationModel.getId();
WorkManager workManager = WorkManager.getInstance(ContextHolder.getApplicationContext());
WorkManager workManager = WorkManager.getInstance(getApplicationContext());
long delay = trigger.getDelay();
int interval = trigger.getInterval();

Expand All @@ -482,7 +587,7 @@ static void createTimestampTriggerNotification(
.putString(Worker.KEY_WORK_TYPE, Worker.WORK_TYPE_NOTIFICATION_TRIGGER)
.putString("id", notificationModel.getId());

WorkDataRepository.getInstance(ContextHolder.getApplicationContext())
WorkDataRepository.getInstance(getApplicationContext())
.insertTriggerNotification(notificationModel, triggerBundle);

// One time trigger
Expand Down Expand Up @@ -520,9 +625,7 @@ static Task<List<String>> getTriggerNotificationIds() {
query.addStates(Arrays.asList(WorkInfo.State.ENQUEUED));

List<WorkInfo> workInfos =
WorkManager.getInstance(ContextHolder.getApplicationContext())
.getWorkInfos(query.build())
.get();
WorkManager.getInstance(getApplicationContext()).getWorkInfos(query.build()).get();

if (workInfos.size() == 0) {
return Collections.emptyList();
Expand Down Expand Up @@ -551,8 +654,7 @@ static void doScheduledWork(

String id = data.getString("id");

WorkDataRepository workDataRepository =
new WorkDataRepository(ContextHolder.getApplicationContext());
WorkDataRepository workDataRepository = new WorkDataRepository(getApplicationContext());

Continuation<WorkDataEntity, Task<Void>> workContinuation =
task -> {
Expand Down Expand Up @@ -598,8 +700,7 @@ static void doScheduledWork(
if (workerRequestType != null
&& workerRequestType.equals(Worker.WORK_REQUEST_ONE_TIME)) {
// delete database entry if work is a one-time request
WorkDataRepository.getInstance(ContextHolder.getApplicationContext())
.deleteById(id);
WorkDataRepository.getInstance(getApplicationContext()).deleteById(id);
}
}
});
Expand Down
Loading