Skip to content

Commit

Permalink
Add PendingIntent.FLAG_IMMUTABLE to observation notification
Browse files Browse the repository at this point in the history
* Required for Android sdk 23
  • Loading branch information
newmanw committed Apr 1, 2024
1 parent a76815c commit df08224
Showing 1 changed file with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
public class ObservationNotificationListener implements IObservationEventListener {

public static int OBSERVATION_NOTIFICATION_ID = 1415;
private Context mContext;
private SharedPreferences mPreferences;
private final Context context;
private final SharedPreferences preferences;

/**
* Constructor.
Expand All @@ -33,20 +33,20 @@ public class ObservationNotificationListener implements IObservationEventListene
* notifications.
*/
public ObservationNotificationListener(Context context) {
mContext = context;
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
this.context = context;
preferences = PreferenceManager.getDefaultSharedPreferences(this.context);
}

@Override
public void onObservationCreated(Collection<Observation> observations, Boolean sendNotifications) {

if(sendNotifications != null && sendNotifications) {
// are we configured to fire notifications?
Boolean notificationsEnabled = mPreferences.getBoolean(mContext.getString(R.string.notificationsEnabledKey), mContext.getResources().getBoolean(R.bool.notificationsEnabledDefaultValue));
boolean notificationsEnabled = preferences.getBoolean(context.getString(R.string.notificationsEnabledKey), context.getResources().getBoolean(R.bool.notificationsEnabledDefaultValue));

// are any of the observations remote? We don't want to fire on locally created
// observations.
Boolean remoteObservations = Boolean.FALSE;
boolean remoteObservations = Boolean.FALSE;
if (notificationsEnabled) {
for (Observation obs : observations) {
if (obs.getRemoteId() != null) {
Expand All @@ -57,16 +57,16 @@ public void onObservationCreated(Collection<Observation> observations, Boolean s
}

// Should a notification be presented to the user?
if (notificationsEnabled && remoteObservations && (observations.size()) > 0) {
if (notificationsEnabled && remoteObservations && !observations.isEmpty()) {

// Build intent for notification content
Intent viewIntent = new Intent(mContext, LandingActivity.class);
Intent viewIntent = new Intent(context, LandingActivity.class);
//viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(mContext, 0, viewIntent, 0);
PendingIntent.getActivity(context, 0, viewIntent, PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext, MageApplication.MAGE_NOTIFICATION_CHANNEL_ID)
new NotificationCompat.Builder(context, MageApplication.MAGE_NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_new_obs)
.setContentTitle("New MAGE Observation(s)")
.setContentText("Touch for details")
Expand All @@ -76,7 +76,7 @@ public void onObservationCreated(Collection<Observation> observations, Boolean s
.setContentIntent(viewPendingIntent);

// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

// Build the notification and issues it with notification manager.
notificationManager.notify(OBSERVATION_NOTIFICATION_ID, notificationBuilder.build());
Expand Down

0 comments on commit df08224

Please sign in to comment.