1

I am using Preferences concept for my SettingActivity and I have an SwitchPreference toggle button for notification on/off, but enable to write code for off notification, when user toggle to off notification.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohd Qasim
  • 896
  • 9
  • 20

2 Answers2

1

I guess you can do that also by getting the getDefaultSharedPreferences where your preferences are stored by your user settings and if you are using the FCM then here is how you can do that.

 public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "MyFirebaseMsgService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
     //here decide by getting the value of your preference manager
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

     if (preferences.getBoolean("your_switch", true)){
          // Your switch is on
          displayNotification();
     } else {
          // Your switch is off
          return;
     }

  }

 }
vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • thank for your code this solve my problem. but how can i stop custom notification like generated from android code(App) side. – Mohd Qasim Jan 12 '18 at 09:36
  • this is same either you use SharedPreferences from services or from the activity you can access them, check if the flag is enabled or disabled and decide what action needs to be taken according to business logic. – vikas kumar Jan 12 '18 at 09:43
  • is this logic work! if i send notification from FCM(Firebase Clouding Messaging), in notification payload format. because if we send data from FCM, in notification payload format. then it is not required to call onMessageReceived() method if app in background mode. – Mohd Qasim Jan 12 '18 at 09:45
  • this onMessageReceived is common for all notification and data payloads you can check it with. remoteMessage.getData() for data and remoteMessage.getNotification() for notifications. – vikas kumar Jan 12 '18 at 10:08
  • I agreed with you on that, but my ask is if I send message from FCM in notification payload. at that time we cant guaranteed either FirebaseMessagingService class will call or not if our app is closed. why because notification payload does't required FirebaseMessagingService class called – Mohd Qasim Jan 12 '18 at 10:16
  • yeah, u r right , it depends if our app is in foreground or background. – vikas kumar Jan 12 '18 at 10:19
0

You cannot turn on or off notification settings programmatically in android. Instead if you are using Firebase Cloud Messaging (FCM) you can write logic in "FirebaseMessagingService()" class not to generate any notification. But this works only if your app is in foreground state. If your app is in background state or is not running, then the notification sent by the Firebase will be shown in the notification tray.

Refer to this link for more information on receiving notification using FCM in android.

Sonu Sanjeev
  • 944
  • 8
  • 9
  • @sanjeev have you saw daily hunt app or way2online. those app are providing notification off/on if we put off, then notification nerver show. either in foreground or background mode. – Mohd Qasim Jan 12 '18 at 09:26
  • @MohdQasim If you go to Settings -> Installed apps -> Daily Hunt -> Notification, you can see that the notification is still on. – Sonu Sanjeev Jan 12 '18 at 09:34
  • yeah i observe that thing,notification is on only if we off from app – Mohd Qasim Jan 12 '18 at 09:47
  • your answer last sentence is confusing me to accept your answer... even I close my app(Daily Hunt) which is now in background(close from recent apps), that time also i will not get any notification. – Mohd Qasim Jan 12 '18 at 09:59
  • Are you asking about the link or the background handling? – Sonu Sanjeev Jan 12 '18 at 10:00
  • about background handling – Mohd Qasim Jan 12 '18 at 10:04
  • If your app is in background the onMessageReceived() in FirebaseMessagingService class will not be called, and the notification is generated directly. – Sonu Sanjeev Jan 12 '18 at 10:08
  • yes Agreed,but how (daily hunt) people are stoping those notification. is these peoples are sending notification in data payload format from FCM – Mohd Qasim Jan 12 '18 at 10:22
  • @MohdQasim : When user turns the toggle on or off, with the help of an api you can store the notification status as on or off. And when sending notification they can use this data to identify which users have turned off notifications. – Sonu Sanjeev Jan 12 '18 at 14:02