我试图在我的应用中实施通知。
我使用带有一些Extras(模型对象)的意图的PendingIntent的AlarmManager但是,当我在BroadcastReceiver类中接收时,我的所有额外内容都消失了
这里是代码: MainActivity
private void saveNotification(Task t) {
Intent intent = new Intent(this, MyAlarmBroadcastReceiver.class);
intent.putExtra("title", t.getTitle()); //not null object
intent.putExtra("text", t.getText()); //not null object
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); - I tried, but it didn't work
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 12345678, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
Toast.makeText(this, "seting the notification in 5 seconds", Toast.LENGTH_SHORT).show();
}
MyAlarmBroadcastReceiver:
public class MyAlarmBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String taskTitle = bundle.getString("title"); //null here
String taskText = bundle.getString("text"); // also null here
startNotification(context, taskTitle , taskText);
}
}
我的猜测是onReceive方法中收到的意图可能不是saveNotification方法中发出的原始意图,但我没有想法如何获取原始意图。
提前致谢