在Android Studio中设置多个警报

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android Studio中设置多个警报相关的知识,希望对你有一定的参考价值。

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

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

[当我从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:

以上是关于在Android Studio中设置多个警报的主要内容,如果未能解决你的问题,请参考以下文章

在Android警报管理器中设置时间-立即触发警报[重复]

如何检测何时在 Android 中设置了新警报

Android Studio中设置Compose 代码模版

如何在 Android Studio 代码编辑器中设置最大行长?

使用 Android Studio 在模拟器中设置 GPS 位置

如何在 Flutter 中设置闹钟?