在我的应用程序中,我将使用此代码来使用通知:
private void addDefaultNotification(){
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence text = "Notification Text";
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Sample notification text.";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, NotificationViewer.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon,text,when);
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
notification.ledARGB = Color.RED;
notification.ledOffMS = 300;
notification.ledOnMS = 300;
notification.defaults |= Notification.DEFAULT_LIGHTS;
//notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notificationManager.notify(com.example.NotifivcationSample.Constants.NOTIFICATION_ID, notification);
}
现在我正在收到通知。通过调用该函数。但我想要的是,即使应用程序当时没有在设备上运行,它也应该通知用户,并且应该在所需时间通知通知。
是否可以 ?如果是,那么请帮助我。谢谢。
编辑:
public class AlarmNotificationReceiver extends BroadcastReceiver{
//private Intent intent;
private NotificationManager notificationManager;
private Notification notification;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
long value1 = intent.getLongExtra("param1", 0);
String value2 = intent.getStringExtra("param2");
addTwoMonthNotification();
}
}
我已经这样做了,但无法在该接收器类中创建通知。为什么 ?我该怎么办?