Skip to content

Commit

Permalink
Merge pull request Expensify#13694 from Expensify/jules-androidNotifi…
Browse files Browse the repository at this point in the history
…cationPrioritisation

Show Android notifications above apps, and introduce notification channels
  • Loading branch information
arosiclair authored Dec 20, 2022
2 parents eec9bf6 + fb01fc4 commit 9d357e4
Showing 1 changed file with 34 additions and 0 deletions.
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

0 comments on commit 9d357e4

Please sign in to comment.