Skip to content

Commit

Permalink
Notifications fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Romidorka committed Dec 15, 2023
1 parent 733864d commit 9e6fde2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.example.medicineremindernew.fragments.CalculatorBolusa;
import com.example.medicineremindernew.fragments.HomeFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

Expand Down Expand Up @@ -60,8 +61,6 @@ protected void onCreate(Bundle savedInstanceState) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS},101);
}
}
// NotificationService.makeNotify(this, "Боярошник", "228:223");
// NotificationService.makeNotify(this, "Пустырник", "69:1337");
}

@SuppressLint("NonConstantResourceId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Build;

import com.example.medicineremindernew.DatabaseHelper;
import com.example.medicineremindernew.activities.MainActivity;
import com.example.medicineremindernew.fragments.HomeFragment;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -135,7 +136,7 @@ void add_alarm(Pill pill){
}

private PendingIntent getAlarmInfoPendingIntent() {
Intent alarmInfoIntent = new Intent(context, HomeFragment.class);
Intent alarmInfoIntent = new Intent(context, MainActivity.class);
alarmInfoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return PendingIntent.getActivity(context, 0, alarmInfoIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
Expand All @@ -149,9 +150,9 @@ private PendingIntent getAlarmActionPendingIntent(String name) {
intent.putExtra("name", name);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
return PendingIntent.getActivity(context, name.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}else{
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return PendingIntent.getActivity(context, name.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}

Expand Down Expand Up @@ -206,15 +207,15 @@ public void add_alarm_notify(Pill pill){
intent.putExtra("name", pill.name);
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
intent.putExtra("time", df.format(alarm_calendar.getTime()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pill.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

alarm_calendar.add(Calendar.MINUTE, -15);
alarm_calendar.add(Calendar.DAY_OF_MONTH, +1);
// alarm_calendar.add(Calendar.DAY_OF_MONTH, +1);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);

System.out.println(pill.name + " " + alarm_calendar.getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_calendar.getTimeInMillis(), pendingIntent);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarm_calendar.getTimeInMillis(), pendingIntent);
System.out.println("setAlarmNotify");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class NotificationService {
private static final int PILLS_CHANNEL_ID = 99;
public static void makeNotify(Context ctx, String title, String time){
Log.i("Notification", "NOTIFY!!!");
Log.i("Notification", "NOTIFY!!! " + title);
NotificationManager mNotificationManager;

Intent ii = new Intent(ctx, HomeFragment.class);
Expand All @@ -46,11 +46,11 @@ public static void makeNotify(Context ctx, String title, String time){
mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationChannel channel = new NotificationChannel(
title,
"pills",
"Pills",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(title);
mBuilder.setChannelId("pills");

Notification notification = mBuilder.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.example.medicineremindernew.AddingPill;

import com.example.medicineremindernew.alarm.Pill;
import com.example.medicineremindernew.calendar.CalendarAdapter;
import com.example.medicineremindernew.calendar.CalendarUtils;
import com.example.medicineremindernew.DatabaseHelper;
Expand All @@ -35,6 +36,7 @@
import com.example.medicineremindernew.PillsView;
import com.example.medicineremindernew.R;
import com.example.medicineremindernew.alarm.AlarmController;
import com.example.medicineremindernew.firebase.PillData;
import com.example.medicineremindernew.firebase.PillsManager;

import java.time.LocalDate;
Expand Down Expand Up @@ -139,6 +141,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
pillAdapter.notifyDataSetChanged();
onCalendarItem(db);
pillAdapter.notifyDataSetChanged();


return null;
});

Expand Down Expand Up @@ -275,4 +279,5 @@ public void openDatePickerAfter() {
datePickerDialog.show();
}


}

0 comments on commit 9e6fde2

Please sign in to comment.