我正在研究Android通知。我编写了一个代码,在其中我为意图和通知管理器设置了不同的ID。我希望android显示两个不同的通知。但是,该通知的主体上仅显示一个通知,并且仅显示最后一个设置的文本。
我看过一些相同的帖子,他们提到他们对意图和经理有一个唯一的ID。每次单击按钮时,我都为它们设置了唯一的ID,但是,它仍然显示一个带有最后设置的文本的通知。
我的主要活动代码:
private int h;
private int min;
public static int myId = 0;
Calendar c;
public static String notificationHead;
public static String notificationBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button picker = findViewById(R.id.timePicker);
picker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timePicker = new TimePickerFragment();
timePicker.show(getSupportFragmentManager(), "time picker");
}
});
Button picker1 = findViewById(R.id.datePicker);
picker1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getSupportFragmentManager(), "date picker");
}
});
Button alarm1 = findViewById(R.id.alarm1);
alarm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myId += 1;
//Toast.makeText(v.getContext(), Integer.toString(myId), Toast.LENGTH_SHORT).show();
notificationHead = "Good Notes Keeper";
notificationBody = "Take shower";
startAlarm(c, myId);
}
});
Button alarm2 = findViewById(R.id.alarm2);
alarm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myId = 2;
notificationHead = "Good Notes Keeper";
notificationBody = "Brush teeth";
startAlarm(c, myId);
}
});
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// c = Calendar.getInstance();
min = minute;
h = hourOfDay;
// c.set(Calendar.HOUR_OF_DAY, h);
// c.set(Calendar.MINUTE, min);
// c.set(Calendar.MINUTE, minute);
// c.set(Calendar.SECOND, 0);
// startAlarm(c);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
c = Calendar.getInstance();
c.set(year, month, dayOfMonth);
c.set(Calendar.HOUR_OF_DAY, h);
c.set(Calendar.MINUTE, min);
}
private void startAlarm(Calendar c, int id){
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
}
我的alertreceiver代码
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationHelper helper = new NotificationHelper(context);
NotificationCompat.Builder nb = helper.getChannelNotification();
helper.getManager().notify(MainActivity.myId, nb.build());
Log.e("Alert", String.valueOf(MainActivity.myId));
}
}
notificationhelper类
public class NotificationHelper extends ContextWrapper {
public static final String CHANNEL_ID = "channelId";
private NotificationManager mManager;
public NotificationHelper(Context context){
super(context);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createChannel();
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createChannel(){
NotificationChannel channel = new
NotificationChannel(CHANNEL_ID, "channel1",
NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(channel);
}
public NotificationManager getManager(){
if(mManager == null){
mManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
public NotificationCompat.Builder getChannelNotification() {
return new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setContentTitle(MainActivity.notificationHead)
.setContentText(MainActivity.notificationBody)
.setSmallIcon(R.drawable.gnkbw);
}
}
如果有人纠正我会很有帮助。