Android notification start when ever I open app

本小妞迷上赌 提交于 2019-12-13 07:23:20

问题


I am adding notification to my app, Everyting is working fine if app is shut down, and it fires notification exactly as i want, but when ever i start app, and on create is called notification fires. If i leave app and start app again, notification will fire again.

However, when ever i open my app notification fires. I do not want that behavior.

This is a code for for notifications and i put code in onCreate: I know that i should move this from onCreate but where to move it? Is it possible to check is alarm already set, if it is, then to not fire notification again.

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;
        }
    }
}

回答1:


Starting a service is independent of creating a notification. Your activity should only start the service. It should not create any notifications. Once your service starts, it is responsible for creating notifications.



来源:https://stackoverflow.com/questions/42583532/android-notification-start-when-ever-i-open-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!