java - 在Android Studio中设置多个警报

标签 java android alarmmanager android-alarms

我似乎找不到这个问题的答案,一直到处搜寻。

我正在尝试在我的项目中管理两个单独的通知。我有一个通知,在帐单到期时会发送给用户,而在付款成功时会发送给用户。

当我从AlarmReceiver类调用通知时,即使是账单到期通知,也会调用付款通知。我认为这是因为它在OnReceive中被称为最后一个,因此这取消了该账单的到期通知。

我可以在OnReceive中放入某种if或switch语句,以查看已调用了哪个请求代码(101或102),然后调用该通知吗?

这是我的代码:

public class AlarmReceiver extends BroadcastReceiver {

    private static final String CHANNEL_ID = "bills.channelId";


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

        // run notification 1
        userHasBill(context, intent);
        // run notification 2
        userPaymentIsSuccessful(context, intent);


    }

    /**
     * Notification to call when the user sets a reminder
     * @param intent
     */
    public void userHasBill(Context context, Intent intent) {

        Intent notificationIntent = new Intent(context, MainMenuActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(EmailActivity.class);
        stackBuilder.addNextIntent(notificationIntent);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(101, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(context);

        // Notification Builder and setting parameters?
        Notification notification = builder.setContentTitle("Bill Reminder!")
                .setContentText("Just a reminder that you have a bill due!")
                .setTicker("Just a reminder that you have a bill due!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent).build();


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "bills",
                    IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(1, notification);


    }


    /**
     * Notification to call when bill payment is successful
     *
     * @param context current context
     * @param intent  intent to go to home activity
     */
    public void userPaymentIsSuccessful(Context context, Intent intent) {

        Intent notificationIntent = new Intent(context, MainMenuActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(EmailActivity.class);
        stackBuilder.addNextIntent(notificationIntent);

        PendingIntent pendingIntent = stackBuilder.getPendingIntent(102, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(context);

        Notification notification = builder.setContentTitle("Payment successful!")
                .setContentText("Your payment was successful!")
                .setTicker("Your Payment was Successful!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent).build();


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "bills",
                    IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(1, notification);


    }


}


我已经尝试制作两个频道ID,但是它仍然调用OnReceive中的第二个通知

最佳答案

您需要使用唯一的通知ID。当前,您为两个通知使用相同的ID:

notificationManager.notify(1, notification);

另请参阅通知指南中的"Show the notification"

我可以在OnReceive中放入某种if或switch语句,以查看已调用了哪个请求代码(101或102),然后调用该通知吗?

AlarmReceiver由其他组件触发。该组件可以为广播使用的Intent附加一些标志。然后,您可以在Intent中评估onReceive()附加项目

关于java - 在Android Studio中设置多个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124970/

相关文章:

java - 没有 "java.lang.Class<org.springframework.data.repository.Repository<?, ?>>"类型的合格 bean

java - Guice 将对象注入(inject)到类构造函数中

Android:让 PhoneStateListener 永远运行的推荐方法?

android - 在特定日期和时间显示通知

java - 随机存取图像(图片)文件

java - Android:设置最大音量

android - 使用 iText 在 Android 中将文本转换为 PDF

java - 如何在Android中显示绘制位图

java - 在测试类的其他方法中使用对象

android - 在 API => 19 中以精确的时间间隔重复警报管理器?