我想在特定时间使用推送通知。我正在使用这些代码;
这是我的 AlarmReceiver 类:
public class AlarmReceiver extends BroadcastReceiver {
private static int NOTIFICATION_ID = 1;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manger = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.arrrow_icon,
"test", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID, new Intent(context, AlarmReceiver.class),0);
Bundle extras = intent.getExtras();
String title = extras.getString("title");
String note = extras.getString("note");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID++, notification);
}
};
这是我的主要部分;
意图alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class); alarmintent.putExtra("title", "test"); alarmintent.putExtra("note", "test message"); PendingIntent 发件人 = PendingIntent .getBroadcast(getApplicationContext(), 1, alarmintent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
如果我的应用程序正在工作,则推送通知正常工作。但是当我强制关闭通知不起作用时。感谢帮助。