1

私は、Firebase通知を使用して、ユーザーにアプリに投稿された新しいデータを通知しています。私はここに与えられた例に従っています:https://github.com/firebase/quickstart-android/tree/master/messagingCheckBoxPreferenceがチェックされている場合にのみfirebase通知をプッシュする方法は?

私はまた、ユーザーに好みを提供します。彼らはCheckBoxPreferenceをチェックして通知を受けることを選択したときにのみ通知を受けるべきです。

CheckBoxPreferenceonPreferenceChangeListenerを正常に設定しました。問題は、CheckBoxがチェックされていなくても通知を受け取ってしまうことです。

public class SettingsActivity extends PreferenceActivity { 

    private AppCompatDelegate mDelegate; 

    Preference myPref; 

    boolean isChecked = true; 

    private static final String TAG = "SettingsActivity"; 

    public static final String RECEIVE_NOTIFS = "receiveNotifications"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     getDelegate().installViewFactory(); 
     getDelegate().onCreate(savedInstanceState); 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     myPref = (CheckBoxPreference) findPreference(RECEIVE_NOTIFS); 
     myPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
      @Override 
      public boolean onPreferenceChange(Preference preference, Object o) { 
       isChecked = Boolean.valueOf(o.toString()); 
       if (isChecked) { 
        Toast.makeText(getBaseContext(), "Checked", Toast.LENGTH_SHORT).show(); 
        if (getIntent().getExtras() != null) { 
         String remoteMessage = getIntent().getExtras().getString("remoteMessage"); 
         sendNotification(remoteMessage); 
        } else { 
         Toast.makeText(getBaseContext(), "Error!", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        Toast.makeText(getBaseContext(), "Unchecked", Toast.LENGTH_SHORT).show(); 
       } 
       return true; 
      } 
     }); 

    } 

    public void sendNotification(String messageBody) { 
     Intent intent = new Intent(this, SettingsActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, 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 /* ID of notification */, notificationBuilder.build()); 
    } 

私ははっきりCheckBoxPreferenceがチェックされているときにのみ通知が送られるべきであることは、上記のコードで指定されています。ここ

は、私がSettingsActivityで行われてきたものです。

はここMyFirebaseMessagingService.javaファイルのコードです:私はここでやっている何

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = "MyFirebaseMsgService"; 

    /** 
    * Called when message is received. 
    * 
    * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
    */ 
    // [START receive_message] 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     // TODO(developer): 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. See sendNotification method below. 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 
     Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); 

     Intent intent = new Intent(this, SettingsActivity.class); 
     intent.putExtra("remoteMessage", remoteMessage.getNotification().getBody()); 
     startActivity(intent); 
    } 
    // [END receive_message] 

} 

私はSettingsActivityする意図としてremoteMessageを送信し、そこsendNotification()方法でそれを使用しています。

私が欲しいのは、ユーザがCheckBoxPreferenceをチェックして選択したときにのみ通知を受けるようにしたいということです。

ここで間違っていることを教えてください。

+0

?コンソール? – Shubhank

+0

@Shubhank私はあなたを取得していません。ごめんなさい。そのことをもう一度明確にしてください。 –

+0

「ユーザーに通知するために通知を使用しています。私はカスタムサーバまたはFirebaseコンソールを使っていると思います – Shubhank

答えて

2

Firebase通知を使用する場合、送信されるメッセージは通知メッセージです。アプリがフォアグラウンドにあるとき、あなたのコードは通知を処理します。しかし、アプリがバックグラウンドになると、デフォルトの通知センターがそれを処理します。通知センターにはチェックボックスが何も分かっていないため、通知が常に表示されます。 「

  • ドン:あなたはあなたのロジックの作業を取得するためにいくつかのオプションがありhttps://github.com/firebase/quickstart-android/issues/8#issuecomment-221039175

    私がこれまで見た中で最高の説明のためのクイックスタートレポ上の問題にこの答えを参照してください。ユーザーがボックスをチェックしていない場合は、通知を送信しないでください。だから、表示を抑止するのではなく、単に送信しないでください。たとえば、購読を選択したユーザーのみがトピックに送信できます。

  • 通知の代わりにデータメッセージを送信します。これにより、アプリケーションがバックグラウンドであっても、コードが常に呼び出されます。通知メッセージとデータメッセージの違いについては、Notifications and data in the message payloadのドキュメントを参照してください。

関連の質問:あなたがから通知を送信している

+0

疑問を解決するために多くのおかげで。さて、通知の代わりにデータメッセージを送信する方法を教えてください。 もう一度おねがいします! –

+0

https://firebase.google.com/docs/cloud-messaging/downstreamをご覧ください。 –

関連する問題