Java 使用broadcastReceiver在唯一时间发出唯一通知

Java 使用broadcastReceiver在唯一时间发出唯一通知,java,android,notifications,alarmmanager,broadcast,Java,Android,Notifications,Alarmmanager,Broadcast,我目前正在开发一个小应用程序,我很难发送通知 public class BroadcastReceiver extends android.content.BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { { NotificationCompat.Builder builder = new Notificatio

我目前正在开发一个小应用程序,我很难发送通知

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());
    }
}
我的目标是:我有不同的任务,他们甚至需要在唯一的时间向用户发送唯一的通知 当应用程序关闭时

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());
    }
}
我做了什么?:我创建了不同的广播接收器,使它们与 alarmManager的,但即使我更改了请求代码、标志或通道代码,我也会在 同时,如果用户为多个任务启用通知,但为通知启用AlarmManager 不应该在同一时间之后工作

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());
    }
}
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>
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());
    }
}
第一和第二频道

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());
    }
}
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);
        }
    }
需要从Channel1和上的第一个broadCastReceiver发送通知的活动 需要从通道2上的第二个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());
    }
}
 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);
            }
        });

对于任何有同样问题的人来说,android系统不允许我们在创建通知的前10分钟后发送通知

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());
    }
}