【发布时间】:2017-03-03 16:08:42
【问题描述】:
我正在向我的应用程序添加通知,如果应用程序关闭,Everyting 工作正常,并且它会完全按照我的意愿触发通知,但是当我启动应用程序时,创建时称为通知触发。如果我离开应用程序并再次启动应用程序,通知将再次触发。
但是,每当我打开我的应用程序通知时。我不希望这种行为。
这是用于通知的代码,我将代码放在onCreate 中:
我知道我应该把它从onCreate 移走,但是把它移到哪里呢?
是否可以检查是否已设置警报,如果已设置,则不再触发通知。
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000 , 1000 * 60 * 45, pendingIntent);
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: ");
Realm realm = null;
try{
realm = Realm.getDefaultInstance();
RealmResults<Drop> results = realm.where(Drop.class).equalTo("completed", false).equalTo("deleted", false).findAll();
for(Drop current : results){
if(isNotificationNeeded(current.getAdded(), current.getWhen(), current.isSwitchButtonchecked())){
fireNotification(current);
}
}
} finally {
if(realm!=null){
realm.close();
}
}
}
public class NotificationService extends IntentService {
public static final String TAG = "holaa";
private long vrijemeStartAlarmUDevetSati = 1000*60*60*9; //milisecunds*secoonds*min*hours* - from 9h
private long vrijemeAlarmDoJedanestSati = 1000*60*60*23; //milisecunds*secoonds*min*hours* - to 23h
Bundle bundle;
int i = 0;
public NotificationService() {
super("NotificationService");
Log.d(TAG, "NotificationService: ");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: ");
//realm database
Realm realm = null;
try{
realm = Realm.getDefaultInstance();
RealmResults<Drop> results = realm.where(Drop.class).equalTo("completed", false).equalTo("deleted", false).findAll();
for(Drop current : results){
if(isNotificationNeeded(current.getAdded(), current.getWhen(), current.isSwitchButtonchecked())){
fireNotification(current);
}
}
} finally {
if(realm!=null){
realm.close();
}
}
}
private void fireNotification(Drop drop) {
i = i+1;
String messageTitle = drop.getWhat();
String messageNote = drop.getWhat_note();
int ikonicaBojaNota = drop.getColorPickerRoudIcon();
PugNotification.with(this)
.load()
.identifier(i)
.title(messageTitle)
.message(messageNote)
.bigTextStyle(messageNote)
.smallIcon(R.drawable.ic_drop)
.largeIcon(R.drawable.logo)
.flags(Notification.DEFAULT_ALL)
.autoCancel(true)
.click(Main2Activity.class, bundle)
.color(color)
.simple()
.build();
}
private boolean isNotificationNeeded(long added, long when, boolean switchButtonchecked){
long now = System.currentTimeMillis();
if ((now>when+ vrijemeStartAlarmUDevetSati) && (now < (when + vrijemeAlarmDoJedanestSati)) && switchButtonchecked == true){
bundle = new Bundle();
bundle.putLong("notification", added);
return true;
}
else {
//do nothing
return false;
}
}
}
【问题讨论】:
-
什么应该导致通知?
-
我正在创建应用程序,我将一些数据保存在本地数据库中,包括何时应该触发通知。一切都很好,但问题是当我打开应用程序并调用 oncreate 时,通知会触发
-
请显示设置通知发生时间的代码。
-
我只是添加了新的代码来显示触发通知的原因。但是当应用程序关闭时,一切都很好。就像我想要的那样,它每 45 分钟发出一次火灾通知。但是,当我打开应用程序时,它会立即触发通知,并且每次打开应用程序时都会触发
-
第一个代码sn-p是在Activity的onCreate()中吧?这告诉设备创建通知。如果你不想要这个然后删除它。
标签: android alarmmanager android-pendingintent alarm