我正在开发一个客户会议应用程序,用户可以在其中存储即将发布的会议信息必须在会议开始前2小时发布有关会议的通知。但是会发生的是,通知仅针对添加的最后一次会议触发。这可能会发生,因为我正在用PendingIntent
创建一个PendingIntent.FLAG_UPDATE_CURRENT
。使用PendingIntent.FLAG_CANCEL_CURRENT
也无济于事。添加下面评论中提到的请求代码无济于事请提供解决方案,以便为每次会议触发通知。
感谢帮助。
AlarmManager
代码:
private void setAlarm(int yyyy,int MM,int dd,int hh, String id){
Calendar cal = Calendar.getInstance();
cal.set(yyyy, MM, dd, (hh-2), 0);
PendingIntent pending = PendingIntent.getBroadcast(getActivity(), 0,new Intent(getActivity(), notify.class).putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
//alarm.cancel(pending); //read what happens with cancel
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pending);
}
Notification
代码:
public class notify extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String id = intent.getStringExtra("id");
Datastore ds = new Datastore(context);
SQLiteDatabase sql = ds.getReadableDatabase();
Log.e("id in notify", id);
Cursor curse= sql.rawQuery("SELECT NAME , CONTACT , DATE , TIME , ADDRESS , DETAILS FROM "+fields.table_name+" WHERE ID ='"+id+"' ", null);
curse.moveToFirst();
Bundle bundle = new Bundle();
bundle.putString("id",id);
bundle.putString("name", curse.getString(curse.getColumnIndex("NAME")));
bundle.putString("contact", curse.getString(curse.getColumnIndex("CONTACT")));
bundle.putString("date",curse.getString(curse.getColumnIndex("DATE")));
bundle.putString("time", curse.getString(curse.getColumnIndex("TIME")));
bundle.putString("address", curse.getString(curse.getColumnIndex("ADDRESS")));
bundle.putString("details",curse.getString(curse.getColumnIndex("DETAILS")));
PendingIntent pending = PendingIntent.getActivity(context, 0, new Intent(context, finaldetails.class).putExtras(bundle),PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
Notification notification = mBuilder.setSmallIcon(R.drawable.ic_launcher).setTicker("You have a meeting").setWhen(0)
.setAutoCancel(true)
.setContentTitle("Upcoming Meeting")
.setContentText("You have a meeting with "+bundle.getString("name"))
.setStyle(new NotificationCompat.BigTextStyle().bigText("You have a meeting"))
.setContentIntent(pending)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(001, notification);
}
}
在以下代码中,您为所指定的所有挂起意图指定请求标识为零。要创建单独的警报,您需要为所有待处理的意图提供不同的请求代码。由于它是相同的,它只考虑最后的警报。
PendingIntent pending = PendingIntent.getBroadcast(getActivity(), 0,new Intent(getActivity(), notify.class).putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT);