Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Firebase Messaging on boolean from Shared Preferences (Android)

I am new to Android development. I have In App Billing that lets people buy push notifications.

When I implemented Firebase Messaging. It just worked out of the box. Awesome!

But. I now want to disable it if it is not bought. (store boolean in shared preferences)

I have no idea how to approach this.

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());
}
}

and

public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";



@Override
public void onTokenRefresh() {
        // Get updated InstanceID token.

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);

}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.
}
}

manifest: (inside application tags)

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name=".FirebaseIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

I have a boolean stored in Shared preferences called "notifications"

Basically my question is: where do i put

if(boolean == true){
// do notification (no clue what this method would be)
}

Where and what do I have to look for or do a search for? Please point me in the right direction :)

Kind regards,

Chris

like image 955
Chrizlee Avatar asked Sep 14 '25 05:09

Chrizlee


1 Answers

You can disable whole service, to simplify your code

<service
    android:name=".MyFirebaseMessagingService"
    android:enabled="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

And after user purchase - enable it

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName(this.getApplicationContext(), MyFirebaseMessagingService.class);
pm.setComponentEnabledSetting(
    componentName,
    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
    PackageManager.DONT_KILL_APP
);

This approach will also conserve battery life, because app won't be launched when push received.

like image 122
Dima Rostopira Avatar answered Sep 16 '25 23:09

Dima Rostopira