-1

I am stuck with my app which needs to repeat one notification after one hour (Medical purpose). As my field is totally opposite and I am noob in coding any help will be appreciated. I know I have to add something in notification receiver to repeat notifications. But every time I try to repeat the app crashes.

(This is a little but unique idea to solve a real world problem I will credit everyone from whom I've received even a little help )

Here is my MainActivity

private NotificationManagerCompat manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    manager =  NotificationManagerCompat.from(this);
}

public void Shownotification(View v) {

    String title = "You Did it!";
    String message = "Some Text";

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .build();
        manager.notify(0,notification);
}

Notification Channel

public class App extends Application {

public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";

@Override
public void onCreate() {
    super.onCreate();

    createNotificationChannels();
}

private void createNotificationChannels() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel1 = new NotificationChannel(
                CHANNEL_1_ID,
                "Channel 1",
                NotificationManager.IMPORTANCE_HIGH
        );
        channel1.setDescription("This is Channel 1");

        NotificationChannel channel2 = new NotificationChannel(
                CHANNEL_2_ID,
                "Channel 2",
                NotificationManager.IMPORTANCE_LOW
        );
        channel2.setDescription("This is Channel 2");

        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(channel1);
        manager.createNotificationChannel(channel2);
    }
}

Notification Reciever

public class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String title = "You Did it!";
    String message = "Some Text";

    Notification notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle(title)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notification);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MINUTE, 1);
    Intent i = new Intent("android.action.DISPLAY_NOTIFICATION");
    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    PendingIntent broadcast = PendingIntent.getBroadcast(context, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), broadcast);

}
Tanveer Ahmed
  • 426
  • 1
  • 4
  • 15

1 Answers1

0

you can set new notification when previous one triggers.in onReceive Method of BroadcastReceiver.