2016-12-01 87 views
1

我是Android开发新手。 我有在应用程序结算,让人们购买推送通知。如何从共享首选项(Android)的布尔值禁用Firebase消息传递

当我实施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()); 
} 
} 

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

清单:(内部应用标签)

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

我已经存储在共享偏好的布尔称为 “通知”

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

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

我在哪里寻找或做什么搜索? 请点我在正确的方向:)

亲切的问候,

克里斯

+0

其中所有通知都显示根据通知变量的状态禁用它们。 我可能不清楚你的问题! –

+0

你使用'notification'或'data'类型的消息吗? – Wilik

+0

@nightcoder。问题是我不知道通知显示的位置在哪里。 – Chrizlee

回答

0
@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
    // check condition pay or not 
    if(CheckCondition){ 
     // if user pay/bought product 
     sendPushNotification(remoteMessage.getNotification().getBody()); 
     } 
    else{ 
    // else user not pay/bought product 
    // do not show Push to user 
     }  
    } 

    private void sendNotification(String messageBody) 
    { 
     Intent intent = new Intent(MyFirebaseMessagingService.this, MainActivity.class); 
     intent.putExtra("msg",messageBody); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("FCM Message") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 , notificationBuilder.build()); 

    } 
+0

谢谢, 我会给它一个镜头。 sendPushNotification()方法来自哪里? 这个函数对我来说没有意义,因为里面没有实际的函数调用。 (我以为他们只记录getBody()) – Chrizlee

+0

你必须创建该功能来显示设备上的推送通知。 –

+0

你有** ** CheckCondition **的想法? –

1

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

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

这种方法还可延长电池使用时间,因为在收到推送时应用程序不会启动。

+0

谢谢,要给这个镜头。不错,安全的电池以及 – Chrizlee

+0

我可以只激活一次吗? @Dima – Chrizlee

+0

@Chrizlee是的,它将保持活动 –