Skip to content
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

Show Android notifications above apps, and introduce notification channels #13694

Merged
merged 3 commits into from
Dec 20, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.expensify.chat.customairshipextender;

import static androidx.core.app.NotificationCompat.PRIORITY_MAX;

import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand All @@ -8,11 +13,13 @@
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuffXfermode;
import android.graphics.PorterDuff.Mode;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
Expand Down Expand Up @@ -48,6 +55,12 @@ public class CustomNotificationProvider extends ReactNotificationProvider {
// Logging
private static final String TAG = "NotificationProvider";

// Define notification channel
public static final String CHANNEL_MESSAGES_ID = "CHANNEL_MESSAGES";
public static final String CHANNEL_MESSAGES_NAME = "Message Notifications";
public static final String CHANNEL_GROUP_ID = "CHANNEL_GROUP_CHATS";
public static final String CHANNEL_GROUP_NAME = "Chats";

// Conversation JSON keys
private static final String PAYLOAD_KEY = "payload";
private static final String TYPE_KEY = "type";
Expand All @@ -58,6 +71,9 @@ public class CustomNotificationProvider extends ReactNotificationProvider {

public CustomNotificationProvider(@NonNull Context context, @NonNull AirshipConfigOptions configOptions) {
super(context, configOptions);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createAndRegisterNotificationChannel(context);
}
}

@NonNull
Expand All @@ -66,6 +82,13 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
super.onExtendBuilder(context, builder, arguments);
PushMessage message = arguments.getMessage();

// Configure the notification channel or priority to ensure it shows in foreground
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(CHANNEL_MESSAGES_ID);
} else {
builder.setPriority(PRIORITY_MAX);
}

if (message.containsKey(PAYLOAD_KEY)) {
try {
JsonMap payload = JsonValue.parseString(message.getExtra(PAYLOAD_KEY)).optMap();
Expand All @@ -82,6 +105,17 @@ protected NotificationCompat.Builder onExtendBuilder(@NonNull Context context, @
return builder;
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void createAndRegisterNotificationChannel(@NonNull Context context) {
NotificationChannelGroup channelGroup = new NotificationChannelGroup(CHANNEL_GROUP_ID, CHANNEL_GROUP_NAME);
NotificationChannel channel = new NotificationChannel(CHANNEL_MESSAGES_ID, CHANNEL_MESSAGES_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.setGroup(CHANNEL_GROUP_ID);

NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannelGroup(channelGroup);
notificationManager.createNotificationChannel(channel);
}

/**
* Creates a canvas to draw a circle and then draws the bitmap avatar within that circle
* to clip off the area of the bitmap outside the circular path and returns a circular
Expand Down