Android通知开始时,我曾打开应用程序

问题描述:

我将通知添加到我的应用程序,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

请显示设置通知应发生的时间的代码。 –

启动服务是独立创建的通知。您的活动只应启动服务。它不应该创建任何通知。一旦你的服务开始,它负责创建通知。

+0

哦,对不起,然后我问我的问题如此错误。我的问题应该是如何只启动一次服务。那么,这是我第一次使用通知,所以即时通讯不太好。那么,如何开始服务一次,而不再一次? – beginner

+0

@beginner致电startService() –

+0

@beginner请参阅https://developer.android.com/guide/components/services.html。在学习Android编程时,developer.android.com应该是您的第一站。 –