android - 如何从共享首选项 (Android) 中禁用 bool 值的 Firebase 消息传递

标签 android firebase firebase-cloud-messaging

我是 Android 开发新手。 我有 In App Billing,可以让人们购买推送通知。

当我实现 Firebase 消息传递时。它开箱即用。太棒了!

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

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

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

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.
}
}

list :(在应用程序标签内)

<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>

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

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

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

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

亲切的问候,

克里斯

最佳答案

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

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

在用户购买后 - 启用它

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

这种方法也将节省电池生命周期,因为应用程序不会在收到推送时启动。

关于android - 如何从共享首选项 (Android) 中禁用 bool 值的 Firebase 消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40907742/

相关文章:

firebase - 在使用 firebase 的推送通知中,onTokenRefresh 与 getToken 之间有什么区别?

node.js - 如何在云函数中传递动态引用?

android - 当应用程序处于前台或后台时如何使用 FCM 处理通知

android - 我可以在 C 代码中调用 `Log.*` 吗?我想使用 LogCat 进行调试

c# - Xamarin ndk 从 c 函数获取字符串作为返回值

java - 在 android 中使用字符串的数字选择器显示完整的文本值

android - 如何在 Android 应用程序中嵌入 Facebook 视频?

firebase - 使用 VueFire 从 Firebase 获取 .key

javascript - 使 firebase 返回排序后的响应

android - 如何设置onClick on Notification(Firebase)