我將通知添加到我的應用程序,Everyting工作正常,如果應用程序關閉,它會根據需要激發通知,但是當我啓動應用程序,並在創建時調用通知火災。如果我離開應用並再次啓動應用,通知將再次觸發。Android通知開始時,我曾打開應用程序
但是,當我打開我的應用程序通知火災。我不想要那種行爲。
這是通知的代碼,我把代碼放在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;
}
}
}
什麼應該導致通知? –
我創建的應用程序,我在本地數據庫中保存一些數據,包括應該通知的時間。 Everyting是好的,但問題是,當我打開應用程序和oncreate被稱爲,通知火災 – beginner
請顯示設置通知應發生的時間的代碼。 –