问题:所有通知都会在设置当天看到。但是在一天间隔之后,当所有通知一起显示的时间到来时,只有我设置的最后一个通知可见(也许我错了,不是最后一个,而是只显示一个通知)
我已经通过各种网站以及堆栈溢出经历了android中多重通知的不同解决方案。所有人都提到在 notificationManager.notify() 中使用唯一 ID,但我已经这样做了,但结果仍然相同..
这是我的代码,用于生成通知并以一天的间隔重复通知 我已使用共享首选项来获取唯一 ID
Intent intent=new Intent(PolicyDetails.this,AlarmReciever.class);
pref=this.getSharedPreferences("MyPref",0);
editor=pref.edit();
editor.putInt("MID",pref.getInt("MID",0)+1);
editor.commit();
PendingIntent pendingIntent=PendingIntent.getBroadcast(PolicyDetails.this,pref.getInt("MID",0),intent,PendingIntent.FLAG_CANCEL_CURRENT);
// Constants.setMID(Constants.MID+1);
AlarmManager alarmManager=(AlarmManager)PolicyDetails.this.getSystemService(PolicyDetails.this.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
这是我的通知生成器代码
Intent i1=new Intent(context,PaidCancel.class);
Intent i2=new Intent(context,PaidCancel.class);
i1.putExtra("code",1);
i2.putExtra("code",2);
pref=context.getSharedPreferences("MyPref",0);
PendingIntent pi =PendingIntent.getBroadcast(context,0,i1,0);
PendingIntent p2=PendingIntent.getBroadcast(context,1,i2,0);
NotificationCompat.Builder mnotifybuilder=new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_menu_gallery)
.setContentTitle("Alarm fired").setContentText("A Remainder for your task No "+pref.getInt("MID",0)).setAutoCancel(false).addAction(R.id.paid,"Button1",pi).addAction(R.id.dismiss,"Button2",p2).setVibrate(new long[]{1000,1000,1000,1000});
NotificationManager notificationManager=(NotificationManager)`enter code here`context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(pref.getInt("MID",0),mnotifybuilder.build());
Log.d("Increased...","increased"+pref.getInt("MID",0));
这是我的 Paidcancel 类代码
static public class PaidCancel extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getIntExtra("code",0)==1) {
Log.d("see once...", "paid button clivked");
}
else
if(intent.getIntExtra("code",0)==2)
Log.d("see once..","cancel Button clicked");
else
Log.d("not ","no");
}
}
最佳答案
This code works for me to create unique notification id..
SharedPreferences prefs = mContext.getSharedPreferences(MainActivity.class.getSimpleName()
, Context.MODE_PRIVATE);
int notificationNumber = prefs.getInt("notificationNumber", 0);
// generate Notify id from SharedPreferences.
mNotificationManager.notify(notificationNumber, mBuilder.build());
SharedPreferences.Editor editor = prefs.edit();
if (notificationNumber < 5000)
notificationNumber++;
else
notificationNumber = 0;
editor.putInt("notificationNumber", notificationNumber);
editor.commit();
关于java - 多个通知,同时以一天的间隔重复它们。在通知功能中使用唯一 ID,但无法获得所需的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48387370/