Skip to content

Commit

Permalink
Create a default channel and display the notification in it
Browse files Browse the repository at this point in the history
As part of the move to API 26, create a default channel and display
the notification in it
e-mission/e-mission-docs#325 (comment)

Bonus fix:
- support pending intents that come from a resolution + pending intents that
  the activity knows how to handle directly
  • Loading branch information
shankari committed Mar 12, 2019
1 parent 539547b commit 6d364dc
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions src/android/NotificationHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.berkeley.eecs.emission.cordova.unifiedlogger;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand All @@ -16,11 +17,15 @@

import edu.berkeley.eecs.emission.MainActivity;
import edu.berkeley.eecs.emission.R;

import java.util.List;


public class NotificationHelper {
private static String TAG = "NotificationHelper";
private static String DEFAULT_CHANNEL_ID = "emissionPluginChannel";
private static String DEFAULT_CHANNEL_DESCRIPTION = "common channel used by all e-mission native plugins";
public static final String DISPLAY_RESOLUTION_ACTION = "DISPLAY_RESOLUTION";
public static final String RESOLUTION_PENDING_INTENT_KEY = "rpIntentKey";

public static void createNotification(Context context, int id, String message) {
Expand Down Expand Up @@ -48,25 +53,29 @@ public static void createNotification(Context context, int id, String message) {
nMgr.notify(id, builder.build());
}

/*
* Used to show a pending intent - e.g. to turn on location services
*/
public static void createNotification(Context context, int id, String message, PendingIntent intent) {
NotificationManager nMgr =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification.Builder builder = getNotificationBuilderForApp(context, nMgr, message);
builder.setContentIntent(intent);

Log.d(context, TAG, "Generating notify with id " + id + ", message " + message
+ " and pending intent " + intent);
nMgr.notify(id, builder.build());
}

/*
* This is a bit of magic voodoo. The tutorial on launching the activity actually uses a stackbuilder
* to create a fake stack for the new activity. However, it looks like the stackbuilder
* is only available in more recent versions of the API. So I use the version for a special activity PendingIntent
* (since our app currently has only one activity) which resolves that issue.
* This also appears to work, at least in the emulator.
*
* TODO: Decide what level API we want to support, and whether we want a more comprehensive activity.
* Used to show a resolution - e.g. to turn on location services
*/
public static void createResolveNotification(Context context, int id, String message, PendingIntent intent) {
NotificationManager nMgr =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification.Builder builder = getNotificationBuilderForApp(context, nMgr, message);

Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.setAction(DISPLAY_RESOLUTION_ACTION);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activityIntent.putExtra(NotificationHelper.RESOLUTION_PENDING_INTENT_KEY, intent);

Expand All @@ -93,13 +102,8 @@ public static Notification.Builder getNotificationBuilderForApp(Context context,
String message) {
Notification.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
List<NotificationChannel> channels = nMgr.getNotificationChannels();
String channelID;
if (channels.size() < 1) {
throw new AssertionError("Did not find any channels, no notifications will be generated");
}
channelID = channels.get(0).getId();
builder = new Notification.Builder(context, channelID);
createDefaultNotificationChannelIfNeeded(context);
builder = new Notification.Builder(context, DEFAULT_CHANNEL_ID);
} else {
builder = new Notification.Builder(context);
}
Expand All @@ -111,4 +115,28 @@ public static Notification.Builder getNotificationBuilderForApp(Context context,

return builder;
}

@TargetApi(26)
private static void createDefaultNotificationChannelIfNeeded(final Context ctxt) {
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) ctxt.getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannel> channels = notificationManager.getNotificationChannels();

for (int i = 0; i < channels.size(); i++) {
String id = channels.get(i).getId();
Log.d(ctxt, TAG, "Checking channel with id = "+id);
if (DEFAULT_CHANNEL_ID.equals(id)) {
Log.d(ctxt, TAG, "Default channel found, returning");
return;
}
}

Log.d(ctxt, TAG, "Default channel not found, creating a new one");
NotificationChannel dChannel = new NotificationChannel(DEFAULT_CHANNEL_ID,
DEFAULT_CHANNEL_DESCRIPTION, NotificationManager.IMPORTANCE_DEFAULT);
dChannel.enableVibration(true);
notificationManager.createNotificationChannel(dChannel);
}
}
}

0 comments on commit 6d364dc

Please sign in to comment.