Search code examples
javaandroidnotificationsalarmmanagerbroadcast

Unique Notifications at Unique Times with broadcastReceiver


i am currently devoloping small app and i struggle about sending notifications.

My Goal: I have different tasks and they need to send unique notifications at unique time to user even while app is closed.

What I did?: I did create different broadCastReceiver's to make them work in harmony with alarmManager' s but even i changed the request code , flag or channel code, i do get notifications at same time if user enables notifications for more than one task, but alarmManagers for notifications are not supposed to work after same time.

'receiver' part of AndroidManifest.xml

<receiver
            android:name=".BroadcastReceiver"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="pendingIntent">
                </action>
            </intent-filter>
        </receiver>

        <receiver
            android:name=".BroadcastReceiver2"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="pendingIntent2">
                </action>
            </intent-filter>
        </receiver>

first and second broadCastReceiver

public class BroadcastReceiver extends android.content.BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "100")
                    .setSmallIcon(R.drawable.logologo)
                    .setContentTitle("Title")
                    .setContentText("Text")
                    .setPriority(NotificationCompat.PRIORITY_HIGH);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            notificationManager.notify(100, builder.build());
        }
    }
}

public class BroadcastReceiver2 extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"102")
                .setSmallIcon(R.drawable.logologo)
                .setContentTitle("Title")
                .setContentText("Text")
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(102, builder.build());
    }
}

First and second Channel

public void createChannel1()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            CharSequence name = "channel1";
            String description = "channel1 description";

            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel channel1 = new NotificationChannel("100", name, importance);
            channel1.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel1);
        }
    }

  public void createChannel2()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            CharSequence name = "channel2";
            String description = "channel2 description";

            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel channel2 = new NotificationChannel("102", name,importance);
            channel2.setDescription(description);

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel2);
        }
    }

Activity that needed to send Notification from first broadCastReceiver on Channel1 and Activity that needed to send Notification from second broadCastReceiver on Channel2

 button30.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(SmokeActivity.this, BroadcastReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(SmokeActivity.this, 100, intent, 0);

                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

                // Set the alarm to start at 8:30 a.m.
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.set(Calendar.HOUR_OF_DAY, 8);
                calendar.set(Calendar.MINUTE, 30);

                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        1000 * 60, pendingIntent);
            }
        });

 button29.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(WaterActivity.this, "Notifications Set", Toast.LENGTH_SHORT).show();

                Intent intent2 = new Intent(WaterActivity.this, BroadcastReceiver2.class);
                PendingIntent pendingIntent2 = PendingIntent.getBroadcast(WaterActivity.this,0,intent2,0);

                AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);

                // Set the alarm to start at 8:30 a.m.
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTimeInMillis(System.currentTimeMillis());
                calendar2.set(Calendar.HOUR_OF_DAY, 8);
                calendar2.set(Calendar.MINUTE, 30);

                alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(),
                        1000*45, pendingIntent2);
            }
        });

Solution

  • For anyone suffering from same problem, the thing is android system does not allow us to send notification after first 10 minute when you create notification.