如何从共享首选项中禁用布尔值上的 Firebase Messaging (Android)

Chr*_*lee 4 android firebase firebase-cloud-messaging

我是 Android 开发新手。我有应用程序内结算功能,可以让人们购买推送通知。

当我实现 Firebase 消息传递时。它开箱即用。惊人的!

但。如果没有购买,我现在想禁用它。(在共享首选项中存储布尔值)

我不知道如何处理这个问题。

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());
}
}
Run Code Online (Sandbox Code Playgroud)

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.
}
}
Run Code Online (Sandbox Code Playgroud)

清单:(在应用程序标签内)

<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>
Run Code Online (Sandbox Code Playgroud)

我在共享首选项中存储了一个名为“通知”的布尔值

基本上我的问题是:我该放在哪里

if(boolean == true){
// do notification (no clue what this method would be)
}
Run Code Online (Sandbox Code Playgroud)

我必须在哪里寻找或搜索什么?请指出我正确的方向:)

亲切的问候,

克里斯

Dim*_*ira 5

您可以禁用整个服务,以简化您的代码

<service
    android:name=".MyFirebaseMessagingService"
    android:enabled="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)

用户购买后 - 启用它

PackageManager pm  = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName(this.getApplicationContext(), MyFirebaseMessagingService.class);
pm.setComponentEnabledSetting(
    componentName,
    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
    PackageManager.DONT_KILL_APP
);
Run Code Online (Sandbox Code Playgroud)

这种方法还可以节省电池寿命,因为收到推送时应用程序不会启动。