1

I have this inside myMainActivity:

public void sendNotificationIfTimeEnd01() {
    Intent intent01 = new Intent(Intent.ACTION_VIEW,
            Uri.parse("https://www.google.de/?gws_rd=ssl"));
    PendingIntent pendingIntent01 = PendingIntent.getActivity(this, 1, intent01, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_stat_notification);
    builder.setContentIntent(pendingIntent01);
    builder.setAutoCancel(true);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
    builder.setContentTitle(gamesJuliToStringArray[0]);
    builder.setContentText("Ready");
    builder.setSubText("click for google");
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_01, builder.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I call it inside my fragment like this:

if (diffDays <= 0) {
    String diffDaysAvailable = "ready";
    ((TextView) android.findViewById(R.id.readyLeft)).setText(diffDaysAvailable);
    ((TextView) 
    activity.sendNotificationIfTimeEnd01();
    Log.d("MyApp", "notification 1");
}

I basically get a sample notification if diffDays <= 0. That works so far.

The problem is that the notification always pops up, when I restart the app.

I googled and read that one should use shared preferences to solve this issue. (Not experienced with it). I have this so far:

        final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        // push notification once time is up
        final String notification01 = sharedPreferences.getString("notification01", "not01");

But have no idea how to continue and solve this issue.

whereisSQL
  • 638
  • 2
  • 13
  • 23
Max Brian
  • 13
  • 5
  • We can use `SharedPreferences` for storing data. You can use `puXxxx()` to store and `getXxxx()` to retrieve from it. Its not that complicated.. – Emil Aug 22 '15 at 18:01
  • possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Emil Aug 22 '15 at 18:31

2 Answers2

2

Do something like this:

if (diffDays <= 0) {
    final SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    final boolean notification01 = sharedPreferences.getBoolean("notification01", false);

    if (!notification01) {
        String diffDaysAvailable = "ready";
        ((TextView) android.findViewById(R.id.readyLeft)).setText(diffDaysAvailable);
        activity.sendNotificationIfTimeEnd01();
        Log.d("MyApp", "notification 1");
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("notification01", true);
        editor.apply();
    }
}

Before sending any notification, this will check if the boolean is false or not. If it is false, then send the notification and set that boolean true, else nothing.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
0

This line initializes your SharedPreference with the name you provided and which will remain Private to your app

SharedPreferences sharedPreferences = getActivity().getSharedPreferences("YOUR_PREFERENCE_NAME", Context.MODE_PRIVATE);

Call this method when you want a boolean data is stored in preference which is associated with a key (a string literal).

public void setValue() {
 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.putBoolean("your_key", true);
 editor.commit();
}

With this method you can check if you have already put a true value in your preference with a key.

public boolean isValueExists() {
 boolean value = sharedPreferences.getBoolean("your_key", false);
 return value;
}
Emil
  • 2,786
  • 20
  • 24