Yashwant Kumar
Yashwant Kumar

Reputation: 143

Push notification using SwitchMaterial toggle button

what I'm unto ?

I'm basically creating a toggle button which sends notification when the check status is true.

what have been done so far?

in Activity_setting.xml

<com.google.android.material.switchmaterial.SwitchMaterial
            android:id="@+id/toggleButtonAlert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:checked="true" />

screenshot

in SettingActivity.java

public class SettingsActivity extends AppCompatActivity  {


    SharedPreferences notificationsPreferences;
    SharedPreferences eScoreAlertPreferences;
    SharedPreferences.Editor escoreEditor;
    SwitchMaterial toggleButtonAlert;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        toggleButtonAlert = findViewById(R.id.toggleButtonAlert);
        toggleButtonAlert.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                eScoreAlertPreferences  = getSharedPreferences("AlertLastChange", Context.MODE_PRIVATE);

                escoreEditor= eScoreAlertPreferences.edit();
                editor.putBoolean("key_ToggleValue", true).commit();           // Saving boolean - true/false

            } else {
                eScoreAlertPreferences  = getSharedPreferences("AlertLastChange", Context.MODE_PRIVATE);
                escoreEditor= eScoreAlertPreferences.edit();
                editor.putBoolean("key_ToggleValue", false).commit();           // Saving boolean - true/false
            }

        });

    }
}

retrieve values in FeedActivity.java(another java class) and calling the push notification function

public class FeedActivity extends AppCompatActivity  {


    private Boolean AlertStat;
    public  NotificationManager notificationManager;

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        View v2 = inflater.inflate(R.layout.fragment_feed, container, false);
        SharedPreferences prefs=context.getSharedPreferences("AlertLastChange", Context.MODE_PRIVATE);
        AlertStat = prefs.getBoolean("key_ToggleValue", true);
        makePushEscoreNotification();

    }
    
    private void makePushEscoreNotification() {


        if (AlertStat) {
            String channelId = "1234";
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("sometitle")
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText("Some text"))
                    .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +context.getPackageName()+"/"+R.raw.negativetone))
                    .setVibrate(new long[]{0L});

            notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId, "ReviewProbe",NotificationManager.IMPORTANCE_HIGH);
                assert notificationManager != null;
                notificationManager.createNotificationChannel(channel);

            }
            assert notificationManager != null;
            builder.setPriority(NotificationCompat.PRIORITY_HIGH);
            notificationManager.notify(0, builder.build());

        }

    }
}

Problem: what went wrong ? the toggle button doesn't save the state and is always enabled even after we try to manually disable it . the notification push section is working fine

Upvotes: 0

Views: 292

Answers (0)

Related Questions