Remove Token from FCM

0

Hi, I have a problem with push notifications using FIREBASE CLOUD MESSAGING, specifically the unsubscribe Token after pressing a button and thus stop receiving notifications. So my question is: How can I unsubscribe a service token or device? I've been trying to use:

FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().deleteInstanceId(); 
FirebaseInstanceId.getInstance()
      .deleteToken(FirebaseInstanceId.getInstance().getToken(),null);

but no method worked for me

I leave the code of my classes:

MainActivity:

public class MainActivity extends AppCompatActivity {

private Button sa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sa = (Button) findViewById(R.id.sa);

    FirebaseMessaging.getInstance().subscribeToTopic("test");
    FirebaseInstanceId.getInstance().getToken();

    sa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FirebaseMessaging.getInstance().unsubscribeFromTopic("test");


            try {
                //FirebaseInstanceId.getInstance().deleteInstanceId();
                FirebaseInstanceId.getInstance().deleteInstanceId();
               // FirebaseInstanceId.getInstance().deleteToken(FirebaseInstanceId.getInstance().getToken(),null);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(),"Borrado",Toast.LENGTH_SHORT).show();
        }
    });
}
}

FirebaseInstanceIDService

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    String token= FirebaseInstanceId.getInstance().getToken();

    registerToken(token);


}

private void registerToken(String token) {

   OkHttpClient client = new OkHttpClient();
    RequestBody body = new FormBody.Builder()
            .add("Token", token)
            .build();

    Request request = new Request.Builder()
            .url("http://192.168.0.107:8080/android/notificacion/registro.php")
            .post(body)
            .build();
    try {
        client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

 }

FirebaseMessagingService

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {
    Intent i = new Intent(this,MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Nueva Notificación")
            .setContentText(message)
            .setSmallIcon(R.drawable.default_img)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());
}
}

I hope and you can help me, Thanks!

    
asked by Hugo Flores 16.04.2017 в 05:22
source

1 answer

1

The problem is that removing the token is only a matter of time before it is generated again.

To solve your problem you should define a Firebase property for example "OptedOutNotifications" and set it to true for those who decide that way. With this property working you can filter the sending of notifications for users who have it in a certain value.

    
answered by 30.05.2017 в 18:07