我是 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/