Skip to content

Commit

Permalink
Merge pull request #148 from cipo7741/master
Browse files Browse the repository at this point in the history
Fix issue #126 Notifications should work again
  • Loading branch information
lordi authored Aug 2, 2021
2 parents f5fa901 + fda5649 commit 2b7ac47
Showing 1 changed file with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package de.smasi.tickmate.notifications;

import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;

Expand All @@ -22,47 +24,51 @@
public class TickmateNotificationBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "Tickmate";

private static final String CHANNEL_ID = "Tickmate";

@Override
public void onReceive(Context context, Intent intent) {

Boolean enabled = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("notification-enabled", false);
.getBoolean("notification-enabled", false);

Log.d(TAG, "Alarm received; enabled=" + enabled.toString());

if (enabled) {


createNotificationChannel(context);
//Locale locale = Locale.getDefault();
//Calendar.getInstance().getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, locale)

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.glyphicons_152_check_white)
.setContentTitle(context.getString(R.string.reminder_title))
.setAutoCancel(true)
.setContentText(context.getString(R.string.reminder_text));
new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.glyphicons_152_check_white)
.setContentTitle(context.getString(R.string.reminder_title))
.setAutoCancel(true)
.setContentText(context.getString(R.string.reminder_text));
Intent resultIntent = new Intent(context, Tickmate.class);


PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
}

public static void updateAlarm(Context context)
{

public static void updateAlarm(Context context) {
Calendar cal = Calendar.getInstance();
java.text.DateFormat timeFmt = java.text.DateFormat.getTimeInstance(java.text.DateFormat.SHORT);

Boolean enabled = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("notification-enabled", false);
.getBoolean("notification-enabled", false);

String timeString = PreferenceManager.getDefaultSharedPreferences(context)
.getString("notification-time", "");
.getString("notification-time", "");

try {
cal.setTime(timeFmt.parse(timeString));
Expand All @@ -73,7 +79,7 @@ public static void updateAlarm(Context context)

Log.d(TAG, "Updating alarm; enabled=" + enabled.toString() + ", time=" + timeString);

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(context, TickmateNotificationBroadcastReceiver.class);
intent.putExtra("onetime", Boolean.FALSE);
Expand All @@ -95,13 +101,30 @@ public static void updateAlarm(Context context)
}

java.text.DateFormat dateFmt = java.text.DateFormat.getDateTimeInstance(
java.text.DateFormat.MEDIUM, java.text.DateFormat.LONG);
java.text.DateFormat.MEDIUM, java.text.DateFormat.LONG);

Log.i(TAG, "Setting alarm at: " + dateFmt.format(alarmTime.getTime()));

am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
AlarmManager.INTERVAL_DAY, pi);
}

}

private static void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.reminder_settings_title);
String description = context.getString(R.string.reminder_settings_summary);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(channel);
}
}
}

0 comments on commit 2b7ac47

Please sign in to comment.