Jonathan Llorente
Jonathan Llorente

Reputation: 67

Turn off push notifications in my app

Successfully implemented Firebase in my app for sending push notifications about my blog, but now I want to know how a user can "turn off" the notifications that I send to the app, here is my code:

package com.lfcchile;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by Jona on 7/23/16.
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_menu_destacado)
                .setContentTitle("LFC Chile")
                .setContentText(remoteMessage.getNotification().getBody())
                .setTicker("Ticker")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true);

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

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

    }

}

Ideas please? thanks in advance!!

Upvotes: 1

Views: 4370

Answers (2)

gsthina
gsthina

Reputation: 1100

You can have a Toggle button for their preferences and then check the value of the toggle before you execute your code. Thus, it becomes as simple as a if-else statement.

package com.lfcchile;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by Jona on 7/23/16.
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_menu_destacado)
                .setContentTitle("LFC Chile")
                .setContentText(remoteMessage.getNotification().getBody())
                .setTicker("Ticker")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if(this.isEnabled == true){ // if statement added here
            notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        }
    }

}

This one will work! Make sure the isEnebled variable is Global.

Upvotes: 1

Andrey Solera
Andrey Solera

Reputation: 2402

you can add some kind of Shared Prefences, and store the value gotten from the user. And then get the value first after getting the notification, if the value of the user is in such case true, then skip the creation of the notification.

Upvotes: 1

Related Questions