2017-03-03 57 views
0

我將通知添加到我的應用程序,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; 
 
     } 
 
    } 
 
}

+0

什麼應該導致通知? –

+0

我創建的應用程序,我在本地數據庫中保存一些數據,包括應該通知的時間。 Everyting是好的,但問題是,當我打開應用程序和oncreate被稱爲,通知火災 – beginner

+0

請顯示設置通知應發生的時間的代碼。 –

回答

1

啓動服務是獨立創建的通知。您的活動只應啓動服務。它不應該創建任何通知。一旦你的服務開始,它負責創建通知。

+0

哦,對不起,然後我問我的問題如此錯誤。我的問題應該是如何只啓動一次服務。那麼,這是我第一次使用通知,所以即時通訊不太好。那麼,如何開始服務一次,而不再一次? – beginner

+0

@beginner致電startService() –

+0

@beginner請參閱https://developer.android.com/guide/components/services.html。在學習Android編程時,developer.android.com應該是您的第一站。 –

相關問題