第二通知取消了第一个通知-Android Studio

问题描述 投票:-1回答:1

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

[当我从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中的第二个通知

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

java android broadcastreceiver alarm
1个回答
1
投票

您正在发布两个具有相同ID(等于1)的通知。这些ID对于不同的通知应该是唯一的。

© www.soinside.com 2019 - 2024. All rights reserved.