Skip to content

Commit

Permalink
Add Topic messaging
Browse files Browse the repository at this point in the history
- Subscribe to topics
- Unsubscribe from topics
  • Loading branch information
SimonMarquis committed Oct 5, 2019
1 parent f3aa9d8 commit c8c4fac
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
53 changes: 53 additions & 0 deletions app/src/main/java/fr/smarquis/fcm/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@

package fr.smarquis.fcm;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -37,13 +45,19 @@
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import fr.smarquis.fcm.payloads.Payload;

import static android.content.DialogInterface.BUTTON_NEGATIVE;
import static android.content.DialogInterface.BUTTON_POSITIVE;
import static android.widget.Toast.LENGTH_LONG;

public class MainActivity extends AppCompatActivity implements Messages.Listener {

private PresenceEventListener presence;
Expand Down Expand Up @@ -204,6 +218,45 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
});
break;
case R.id.action_topics:
// Extracted from com.google.firebase.messaging.FirebaseMessaging
Pattern pattern = Pattern.compile("[a-zA-Z0-9-_.~%]{1,900}");
@SuppressLint("InflateParams") View view = LayoutInflater.from(this).inflate(R.layout.topics_dialog, null, false);
EditText input = view.findViewById(R.id.input);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.menu_topics)
.setView(view)
.setPositiveButton(R.string.topics_subscribe, (d, which) -> {
String topic = input.getText().toString();
FirebaseMessaging.getInstance().subscribeToTopic(topic)
.addOnSuccessListener(this, success -> Toast.makeText(this, getString(R.string.topics_subscribed, topic), LENGTH_LONG).show())
.addOnFailureListener(this, error -> Toast.makeText(this, Util.printStackTrace(error), LENGTH_LONG).show());
})
.setNegativeButton(R.string.topics_unsubscribe, (d, which) -> {
String topic = input.getText().toString();
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic)
.addOnSuccessListener(this, success -> Toast.makeText(this, getString(R.string.topics_unsubscribed, topic), LENGTH_LONG).show())
.addOnFailureListener(this, error -> Toast.makeText(this, Util.printStackTrace(error), LENGTH_LONG).show());
}).show();
input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {
boolean matches = pattern.matcher(s).matches();
dialog.getButton(BUTTON_POSITIVE).setEnabled(matches);
dialog.getButton(BUTTON_NEGATIVE).setEnabled(matches);
}
});
// Trigger afterTextChanged()
input.setText(null);
return true;
case R.id.action_delete_all:
Notifications.removeAll(this);
List<Message<Payload>> removedMessages = messages.get();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/smarquis/fcm/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public class Util {

private static String printStackTrace(@NonNull Throwable exception) {
static String printStackTrace(@NonNull Throwable exception) {
StringWriter trace = new StringWriter();
exception.printStackTrace(new PrintWriter(trace));
return trace.toString();
Expand Down
36 changes: 36 additions & 0 deletions app/src/main/res/layout/topics_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
~ Copyright 2019 Simon Marquis
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/unit_3"
android:layout_marginRight="@dimen/unit_3"
android:importantForAutofill="no"
android:inputType="text"
android:lines="1"
android:maxLines="1"
tools:ignore="LabelFor" />

</FrameLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
android:icon="@android:drawable/presence_invisible"
android:title=""
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_topics"
android:title="@string/menu_topics"
app:showAsAction="never" />
<item
android:id="@+id/action_share_token"
android:title="@string/menu_share_token"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
<string name="copied_to_clipboard">Copied to clipboard</string>
<string name="menu_share_token">Share token</string>
<string name="menu_invalidate_token">Invalidate token</string>
<string name="menu_topics">Topics</string>
<string name="menu_delete_all">Delete all</string>
<string name="snackbar_item_deleted" tools:ignore="PluralsCandidate">%d deleted</string>
<string name="snackbar_item_undo">Undo</string>
<string name="topics_subscribe">Subscribe</string>
<string name="topics_subscribed">Subscribed to topic: `%s`</string>
<string name="topics_unsubscribe">Unsubscribe</string>
<string name="topics_unsubscribed">Unsubscribed from topic: `%s`</string>
</resources>

0 comments on commit c8c4fac

Please sign in to comment.