Notification wird eher zufällig als festgelegt gesendet

  • Antworten:6
quidproquo
  • Forum-Beiträge: 6

19.06.2017, 21:28:46 via Website

Hallo,

in der onCreate der MainActivity ist folgender code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    intent_anmeldeActivity = new Intent(this, anmeldeActivity.class);
    intent_WebViewActivity = new Intent(this, WebViewActivity.class);

    prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    prefsEditor = prefs.edit();


    Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 07);
        calendar.set(Calendar.MINUTE, 00);
        calendar.set(Calendar.SECOND, 00);

    intent_notification = new Intent(this, NotificationClass.class);

    Intent intent1 = new Intent(MainActivity.this, NotificationClass.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

das ist die NotificationClass:

public class NotificationClass extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub

   loadText loadText = new loadText();
   loadText.startAsyncTask(context);
} }

loadText extended AsyncTask und in onPostExecute von loadText wird die Klasse NotificationBuilding aufgerufen:

public class NotificationBuilding {

Context mContext = null;

int ID = 1;
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long when = System.currentTimeMillis();

public void startNotificationBuilding(Context con, String title, String text) {

    this.mContext = con;

    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(mContext, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
            mContext)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setColor(Color.argb(255, 234, 146, 21))
            .setContentTitle(title)
            .setContentText(text)
            .setSound(alarmSound)
            .setAutoCancel(true)
            .setWhen(when)
            .setContentIntent(pendingIntent)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
            .setLights(Color.argb(255, 234, 146, 21), 1000, 10000)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(text));

    notificationManager.notify(ID, mNotifyBuilder.build());
    ID++;
} }

Ich hab die Vermutung, dass es daran liegt, dass der Alarm bei jedem Öffnen der app der Alarm gestellt wird. Ist es, wenn es daran liegt, sinnvoller, beim Öffnen der app zu überprüfen, ob es der erste Start der app ist und nur wenn es das erste Mal ist, den Alarm zu stellen?

Danke im Voraus!

— geändert am 19.06.2017, 21:52:37

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

19.06.2017, 21:38:56 via App

Hast du mal AlarmManager#setExactRepeating versucht?

PS: Wenn du schon in 2 Androidforen die selbe Frage stellst, könntest du deinen Post wenigstens so anpassen dass er hier richtig lesbar ist...

LG Pascal //It's not a bug, it's a feature. :) ;)

quidproquoswa00

Antworten
swa00
  • Forum-Beiträge: 3.704

19.06.2017, 21:44:32 via Website

Ich mach dann mal "drüben" zu ..... :-)

Liebe Grüße - Stefan
[ App - Entwicklung ]

quidproquoPascal P.

Antworten
quidproquo
  • Forum-Beiträge: 6

19.06.2017, 21:55:40 via Website

Hab es geändert, danke.
Ja, es war von vornherein auf exactRepeating eingestellt und auch da waren Fehler enthalten, sodass ich es später auf inexactRepeating gestellt habe (nach Recherche auf stackoverflow (glaube ich)). Die Initialisierung des Alarms ist nun in einer if-Verzweigung die in der onCreate der MainActivity prüft, ob es der erste Start der app ist und bis jetzt kamen noch keine notifications nicht gegen 07:00 Uhr. Passt also bisher so.

— geändert am 19.06.2017, 21:58:41

Antworten
quidproquo
  • Forum-Beiträge: 6

19.06.2017, 21:56:22 via Website

Ok danke.

Ps: Der jsoup-Part ist inzwischen fertig swa00. Danke noch mal für die Hilfe davor.

— geändert am 19.06.2017, 23:09:23

swa00

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

20.06.2017, 16:48:54 via Website

Zum testen würde ich immer aktuelle Zeit + 1 min oder so nehmen.
Dann kannst du beim Empfangen des Broadcasts auch schauen obs Fehler gibt im Log.

LG Pascal //It's not a bug, it's a feature. :) ;)

quidproquoswa00

Antworten
quidproquo
  • Forum-Beiträge: 6

04.07.2017, 22:43:37 via Website

Danke, daran habe ich schon so oder so ähnlich gemacht :).
Zur app: Nun funktioniert es noch immer noch eher nicht so wie gedacht, obwohl es rein code-technisch sinnvoll aussieht.
Siehst du / jemand von euch einen Fehler in der AndroidManifest.xml-Datei?

<receiver android:name=".NotificationClass">
        android:name=".NotificationClass"
        android:exported="true"
        android:process=":remote"
    </receiver>

Danke im Voraus!

Antworten