Java 第二个通知取消了第一个通知-Android Studio
我试图在我的项目中管理两个单独的通知。我有一个在账单到期时发送给用户的通知,以及一个在付款成功时发送的通知 当我从AlarmReceiver类调用通知时,将调用付款通知,即使是账单到期通知。我认为这是因为它被称为OnReceive中的last,因此这取消了账单到期通知 是否有一种if或switch语句可以放在OnReceive中,以检查调用了哪个请求代码(101或102),然后调用该通知 这是我的密码:Java 第二个通知取消了第一个通知-Android Studio,java,android,broadcastreceiver,alarm,Java,Android,Broadcastreceiver,Alarm,我试图在我的项目中管理两个单独的通知。我有一个在账单到期时发送给用户的通知,以及一个在付款成功时发送的通知 当我从AlarmReceiver类调用通知时,将调用付款通知,即使是账单到期通知。我认为这是因为它被称为OnReceive中的last,因此这取消了账单到期通知 是否有一种if或switch语句可以放在OnReceive中,以检查调用了哪个请求代码(101或102),然后调用该通知 这是我的密码: public class AlarmReceiver extends BroadcastRe
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(等于1)的通知。对于不同的通知,这些ID应该是唯一的 公共无效通知(int id, 通知)。
发布要在状态栏中显示的通知如果应用程序已经发布了具有相同id的通知,但尚未取消,则该通知将被更新的信息替换。
我将第二个通知的通知ID更改为2,它现在同时发送两个通知