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

Update LocalNotifications.java #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions src/android/LocalNotifications.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import android.app.NotificationChannel;

/**
* This class exposes methods in Cordova that can be called from JavaScript.
Expand Down Expand Up @@ -69,14 +70,14 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
}

private void showNotification(JSONArray args) throws JSONException {
// Get args
// Get args
String title = args.getString(0);
String dir = args.getString(1);
String lang = args.getString(2);
String body = args.getString(3);
String tag = args.getString(4);
String icon = args.getString(5);

String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string is not used

Context context = cordova.getActivity();

Intent notificationIntent = new Intent(context, NotificationHandlerActivity.class);
Expand All @@ -86,10 +87,20 @@ private void showNotification(JSONArray args) throws JSONException {
int requestCode = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder mBuilder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The channel should be named differently. Maybe id="default" and name="Default"? Also, just use NotificationManager.IMPORTANCE_DEFAULT directly here rather than creating the importance variable.

mNotificationManager.createNotificationChannel(notificationChannel);
mBuilder = new NotificationCompat.Builder(context, notificationChannel.getId());
} else {
mBuilder = new NotificationCompat.Builder(context);
}

// Build notifications
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
mBuilder = mBuilder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the mBuilder = here

.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(body)
Expand All @@ -103,7 +114,7 @@ private void showNotification(JSONArray args) throws JSONException {
}

// Show notification
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(tag, 0, mBuilder.build());
}

Expand Down