简体   繁体   English

在 Android Studio 中设置多个警报

[英]Set multiple alarms in Android Studio

I cannot seem to find an answer to this question, have been searching everywhere.我似乎找不到这个问题的答案,一直在到处寻找。

I am trying to manage two separate notifications in my project.我正在尝试在我的项目中管理两个单独的通知。 I have a notification that send to the user when a bill is due, and a notification that is sent when a payment is successful.我有一个在账单到期时发送给用户的通知,以及一个在付款成功时发送的通知。

When I call the notification from my AlarmReceiver class, the payment notification is called, even for the bill is due notification.当我从 AlarmReceiver class 调用通知时,会调用付款通知,即使是账单到期通知。 I think this is because it is called last in the OnReceive, and so this cancels out the bill is due notification.我认为这是因为它在 OnReceive 中最后被调用,因此取消了账单到期通知。

Is there a type of if or switch statement that I can put in my OnReceive, to see check which request code has been called (101 or 102) and then call that notification?是否有一种 if 或 switch 语句可以放入我的 OnReceive 中,以查看调用了哪个请求代码(101 或 102)然后调用该通知?

Here is my code:这是我的代码:

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


    }


}

I already tried to make two channel IDs, but it still calls the second notification that's in the OnReceive我已经尝试创建两个频道 ID,但它仍然调用 OnReceive 中的第二个通知

You need to use a unique notification id.您需要使用唯一的通知 ID。 Currently you are using the same id for both notifications:目前,您对两个通知使用相同的 id:

notificationManager.notify(1, notification);

See also "Show the notification" from the guide on notifications另请参阅通知指南中的“显示通知”

Is there a type of if or switch statement that I can put in my OnReceive, to see check which request code has been called (101 or 102) and then call that notification?是否有一种 if 或 switch 语句可以放入我的 OnReceive 中,以查看调用了哪个请求代码(101 或 102)然后调用该通知?

The AlarmReceiver is triggered by some other component. AlarmReceiver由其他一些组件触发。 This component can put some flag as extra to the Intent which is used for the broadcast.该组件可以将一些标志作为额外的标志添加到用于广播的Intent中。 Then you can evaluate the Intent extra in onReceive()然后您可以在onReceive()中评估Intent extra

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM