【问题标题】:Android notification start when ever I open app每当我打开应用程序时,Android 通知就会开始
【发布时间】: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


【解决方案1】:

启动服务与创建通知无关。您的活动应该只启动服务。它不应创建任何通知。服务启动后,它负责创建通知。

【讨论】:

  • 哦,对不起,那我问的问题太错误了。我的问题应该是如何只启动一次服务。嗯,这是我第一次使用通知,所以我不太擅长这个。那么,如何启动一次服务,然后再也不启动呢?
  • @beginner 调用 startService()
  • @beginner 见developer.android.com/guide/components/services.html。 developer.android.com 应该是您学习 Android 编程的第一站。
  • 哦,我会检​​查的。谢谢。我现在也是 gogole,我的问题有很多答案。我完全混淆了服务和警报。
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多