Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

added-small-icon-options-and-group-summary #1253

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dieam.reactnativepushnotification.modules;


import android.app.AlarmManager;
import android.app.Application;
import android.app.Notification;
Expand All @@ -19,7 +18,9 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;

import android.util.Log;

import com.facebook.react.bridge.ReadableMap;
Expand Down Expand Up @@ -50,7 +51,8 @@ public class RNPushNotificationHelper {
public RNPushNotificationHelper(Application context) {
this.context = context;
this.config = new RNPushNotificationConfig(context);
this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY, Context.MODE_PRIVATE);
this.scheduledNotificationsPersistence = context.getSharedPreferences(RNPushNotificationHelper.PREFERENCES_KEY,
Context.MODE_PRIVATE);
}

public Class getMainActivityClass() {
Expand All @@ -76,7 +78,8 @@ private PendingIntent toScheduleNotificationIntent(Bundle bundle) {
notificationIntent.putExtra(RNPushNotificationPublisher.NOTIFICATION_ID, notificationID);
notificationIntent.putExtras(bundle);

return PendingIntent.getBroadcast(context, notificationID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return PendingIntent.getBroadcast(context, notificationID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

public void sendNotificationScheduled(Bundle bundle) {
Expand Down Expand Up @@ -126,8 +129,8 @@ public void sendNotificationScheduledCore(Bundle bundle) {
// notification to the user
PendingIntent pendingIntent = toScheduleNotificationIntent(bundle);

Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
bundle.getString("id"), Long.toString(fireDate)));
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s", bundle.getString("id"),
Long.toString(fireDate)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
} else {
Expand All @@ -144,7 +147,8 @@ public void sendToNotificationCentre(Bundle bundle) {
}

if (bundle.getString("message") == null) {
// this happens when a 'data' notification is received - we do not synthesize a local notification in this case
// this happens when a 'data' notification is received - we do not synthesize a
// local notification in this case
Log.d(LOG_TAG, "Cannot send to notification centre because there is no 'message' field in: " + bundle);
return;
}
Expand All @@ -168,7 +172,7 @@ public void sendToNotificationCentre(Bundle bundle) {
final String priorityString = bundle.getString("priority");

if (priorityString != null) {
switch(priorityString.toLowerCase()) {
switch (priorityString.toLowerCase()) {
case "max":
priority = NotificationCompat.PRIORITY_MAX;
break;
Expand All @@ -193,7 +197,7 @@ public void sendToNotificationCentre(Bundle bundle) {
final String visibilityString = bundle.getString("visibility");

if (visibilityString != null) {
switch(visibilityString.toLowerCase()) {
switch (visibilityString.toLowerCase()) {
case "private":
visibility = NotificationCompat.VISIBILITY_PRIVATE;
break;
Expand All @@ -209,11 +213,8 @@ public void sendToNotificationCentre(Bundle bundle) {
}

NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentTitle(title)
.setTicker(bundle.getString("ticker"))
.setVisibility(visibility)
.setPriority(priority)
.setAutoCancel(bundle.getBoolean("autoCancel", true));
.setContentTitle(title).setTicker(bundle.getString("ticker")).setVisibility(visibility)
.setPriority(priority).setAutoCancel(bundle.getBoolean("autoCancel", true));

String group = bundle.getString("group");
if (group != null) {
Expand All @@ -235,20 +236,27 @@ public void sendToNotificationCentre(Bundle bundle) {
notification.setNumber(Integer.parseInt(numberString));
}

int smallIconResId;
int largeIconResId;

String smallIcon = bundle.getString("smallIcon");
int smallIconResId = 0;
int largeIconResId = 0;

Object smallIcon = bundle.get("smallIcon");
if (smallIcon != null) {
smallIconResId = res.getIdentifier(smallIcon, "mipmap", packageName);
if (smallIcon instanceof String) {
smallIconResId = res.getIdentifier((String) smallIcon, "mipmap", packageName);
} else if (smallIcon instanceof Bundle) {
Bundle smallIconReadAbleMap = (Bundle) smallIcon;
smallIconResId = res.getIdentifier(
smallIconReadAbleMap.getString("iconName"),
smallIconReadAbleMap.getString("iconLocation"),
packageName
);
}
} else {
smallIconResId = res.getIdentifier("ic_notification", "mipmap", packageName);
}

if (smallIconResId == 0) {
smallIconResId = res.getIdentifier("ic_launcher", "mipmap", packageName);

if (smallIconResId == 0) {
smallIconResId = android.R.drawable.ic_dialog_info;
}
Expand Down Expand Up @@ -308,6 +316,10 @@ public void sendToNotificationCentre(Bundle bundle) {
notification.setOngoing(bundle.getBoolean("ongoing"));
}

if (bundle.containsKey("groupSummary") || bundle.getBoolean("groupSummary")) {
notification.setGroupSummary(bundle.getBoolean("groupSummary"));
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notification.setCategory(NotificationCompat.CATEGORY_CALL);

Expand All @@ -331,7 +343,8 @@ public void sendToNotificationCentre(Bundle bundle) {
notification.setContentIntent(pendingIntent);

if (!bundle.containsKey("vibrate") || bundle.getBoolean("vibrate")) {
long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration") : DEFAULT_VIBRATION;
long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration")
: DEFAULT_VIBRATION;
if (vibration == 0)
vibration = DEFAULT_VIBRATION;
notification.setVibrate(new long[]{0, vibration});
Expand Down Expand Up @@ -375,10 +388,13 @@ public void sendToNotificationCentre(Bundle bundle) {
// Remove the notification from the shared preferences once it has been shown
// to avoid showing the notification again when the phone is rebooted. If the
// notification is not removed, then every time the phone is rebooted, we will
// try to reschedule all the notifications stored in shared preferences and since
// try to reschedule all the notifications stored in shared preferences and
// since
// these notifications will be in the past time, they will be shown immediately
// to the user which we shouldn't do. So, remove the notification from the shared
// preferences once it has been shown to the user. If it is a repeating notification
// to the user which we shouldn't do. So, remove the notification from the
// shared
// preferences once it has been shown to the user. If it is a repeating
// notification
// it will be scheduled again.
if (scheduledNotificationsPersistence.getString(notificationIdString, null) != null) {
SharedPreferences.Editor editor = scheduledNotificationsPersistence.edit();
Expand Down Expand Up @@ -413,7 +429,8 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
if (repeatType != null) {
long fireDate = (long) bundle.getDouble("fireDate");

boolean validRepeatType = Arrays.asList("time", "month", "week", "day", "hour", "minute").contains(repeatType);
boolean validRepeatType = Arrays.asList("time", "month", "week", "day", "hour", "minute")
.contains(repeatType);

// Sanity checks
if (!validRepeatType) {
Expand All @@ -422,8 +439,7 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {
}

if ("time".equals(repeatType) && repeatTime <= 0) {
Log.w(LOG_TAG, "repeatType specified as time but no repeatTime " +
"has been mentioned");
Log.w(LOG_TAG, "repeatType specified as time but no repeatTime " + "has been mentioned");
return;
}

Expand Down Expand Up @@ -469,8 +485,8 @@ private void scheduleNextNotificationIfRepeating(Bundle bundle) {

// Sanity check, should never happen
if (newFireDate != 0) {
Log.d(LOG_TAG, String.format("Repeating notification with id %s at time %s",
bundle.getString("id"), Long.toString(newFireDate)));
Log.d(LOG_TAG, String.format("Repeating notification with id %s at time %s", bundle.getString("id"),
Long.toString(newFireDate)));
bundle.putDouble("fireDate", newFireDate);
this.sendNotificationScheduled(bundle);
}
Expand Down Expand Up @@ -551,6 +567,7 @@ private static void commit(SharedPreferences.Editor editor) {
}

private static boolean channelCreated = false;

private void checkOrCreateChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
Expand All @@ -565,7 +582,7 @@ private void checkOrCreateChannel(NotificationManager manager) {
final String importanceString = bundle.getString("importance");

if (importanceString != null) {
switch(importanceString.toLowerCase()) {
switch (importanceString.toLowerCase()) {
case "default":
importance = NotificationManager.IMPORTANCE_DEFAULT;
break;
Expand All @@ -592,7 +609,9 @@ private void checkOrCreateChannel(NotificationManager manager) {
}
}

NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, this.config.getChannelName() != null ? this.config.getChannelName() : "rn-push-notification-channel", importance);
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
this.config.getChannelName() != null ? this.config.getChannelName() : "rn-push-notification-channel",
importance);

channel.setDescription(this.config.getChannelDescription());
channel.enableLights(true);
Expand All @@ -601,4 +620,4 @@ private void checkOrCreateChannel(NotificationManager manager) {
manager.createNotificationChannel(channel);
channelCreated = true;
}
}
}
17 changes: 17 additions & 0 deletions example/android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>android</name>
<comment>Project android created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions example/android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=
eclipse.preferences.version=1