-3

我已经成功地为我的应用实现了 Firebase 推送通知,但现在我希望 ToggleButton/Switch 存储用户选择,我的意思是,当用户激活开关时,我希望存储该状态,因为当用户关闭应用程序,状态自动更改为“禁用”,这是我的代码:

我已经尝试了一些教程,但我无法解决这个问题......

package com.lfcchile;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

import com.google.firebase.messaging.FirebaseMessaging;

/**
 * Created by Jona on 7/25/16.
 */
public class NotificationSettings extends AppCompatActivity {

    private static final String TAG = "FCM Service";
    private Switch switchComunidad, switchBlog, switchEquipo, switchPartidos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_settings);

        switchComunidad = (Switch) findViewById(R.id.switchComunidad);
        switchBlog = (Switch) findViewById(R.id.switchBlog);
        switchEquipo = (Switch) findViewById(R.id.switchEquipo);
        switchPartidos = (Switch) findViewById(R.id.switchPartidos);

        switchComunidad.setTextOn("On");
        switchComunidad.setTextOff("Off");
        switchBlog.setTextOn("On");
        switchBlog.setTextOff("Off");
        switchEquipo.setTextOn("On");
        switchEquipo.setTextOff("Off");
        switchPartidos.setTextOn("On");
        switchPartidos.setTextOff("Off");

        //Funciones que controlan las acciones de cada Switch
        switchComunidad.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchComunidad.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Comunidad");
                    Log.d(TAG, "Suscrito al tema Comunidad");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Comunidad");
                    Log.d(TAG, "Suscrito al tema Comunidad");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchBlog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchBlog.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Blog");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Blog");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchEquipo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchEquipo.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Equipo");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Equipo");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (switchPartidos.isChecked()){
                    FirebaseMessaging.getInstance().subscribeToTopic("Partidos");
                    Toast.makeText(getApplicationContext(), "Activado Correctamente",
                            Toast.LENGTH_LONG).show();
                }else {
                    FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos");
                    Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                            Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

提前致谢!

4

1 回答 1

2

我为您构建了一个简化的示例。

private static final String SWITCH_PARTIDOS_STATE = "switchPartidosState";
private SharedPreferences sharedPreferences;
private Switch switchPartidos;  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getSharedPreferences("myname", Context.MODE_PRIVATE);

    switchPartidos.setChecked(sharedPreferences.getBoolean(SWITCH_PARTIDOS_STATE, false));

    switchPartidos.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            sharedPreferences.edit().putBoolean(SWITCH_PARTIDOS_STATE, isChecked).commit();
            if (isChecked){
                FirebaseMessaging.getInstance().subscribeToTopic("Partidos");
                Toast.makeText(getApplicationContext(), "Activado Correctamente",
                        Toast.LENGTH_LONG).show();
            }else {
                FirebaseMessaging.getInstance().unsubscribeFromTopic("Partidos");
                Toast.makeText(getApplicationContext(), "Desactivado Correctamente",
                        Toast.LENGTH_LONG).show();
            }
        }
    });
}

希望能帮助到你!

于 2016-08-03T06:30:24.820 回答