Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "android.example.com.squawker"
minSdkVersion 16
targetSdkVersion 25
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -21,26 +21,20 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
implementation fileTree(dir: 'libs', include: ['*.jar'])

// RecyclerView
compile 'com.android.support:recyclerview-v7:25.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'

// Schematic dependencies for ContentProvider
apt 'net.simonvt.schematic:schematic-compiler:0.6.3'
compile 'net.simonvt.schematic:schematic:0.6.3'
testImplementation 'junit:junit:4.13.2'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
annotationProcessor 'net.simonvt.schematic:schematic-compiler:0.6.3'
implementation 'net.simonvt.schematic:schematic:0.6.3'
implementation "androidx.preference:preference:1.1.1"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

// Preferences Dependencies
compile 'com.android.support:preference-v7:25.1.0'
implementation platform('com.google.firebase:firebase-bom:29.0.3')
implementation 'com.google.firebase:firebase-analytics'

// Firebase dependency
compile 'com.google.firebase:firebase-messaging:10.0.1'
implementation 'com.google.firebase:firebase-messaging:23.0.0'
}
// Apply the Google Services plugin. Make sure to add the google-services.json file in the app
// folder. You download it from the Firebase console
apply plugin: 'com.google.gms.google-services'
26 changes: 19 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
android:authorities="android.example.com.squawker.provider.provider"
android:exported="false" />

<service
android:name=".fcm.SquawkFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".fcm.SquawkFirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

<activity
android:name=".following.FollowingPreferenceActivity"
android:parentActivityName=".MainActivity">
Expand All @@ -33,13 +46,12 @@
android:value=".MainActivity" />
</activity>


<!-- Id service -->
<service android:name=".fcm.SquawkFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="false" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="false" />

<!-- Service listening for any incoming messages -->
<service android:name=".fcm.SquawkFirebaseMessageService">
Expand Down
44 changes: 27 additions & 17 deletions app/src/main/java/android/example/com/squawker/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@
import android.example.com.squawker.provider.SquawkContract;
import android.example.com.squawker.provider.SquawkProvider;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.CursorLoader;
import androidx.loader.content.Loader;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.firebase.iid.FirebaseInstanceId;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity implements
LoaderManager.LoaderCallbacks<Cursor> {
Expand Down Expand Up @@ -86,13 +86,23 @@ protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setAdapter(mAdapter);

// Start the loader
getSupportLoaderManager().initLoader(LOADER_ID_MESSAGES, null, this);

// Get token from the ID Service you created and show it in a log
String token = FirebaseInstanceId.getInstance().getToken();
String msg = getString(R.string.message_token_format, token);
Log.d(LOG_TAG, msg);

LoaderManager.getInstance(this).initLoader(LOADER_ID_MESSAGES, null, this);

FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(task -> {
if (!task.isSuccessful()) {
Log.w(MainActivity.LOG_TAG, "Fetching FCM registration token failed", task.getException());
return;
}

// Get new FCM registration token
String token = task.getResult();

// Log and toast
String msg = getString(R.string.message_token_format, token);
Log.d(MainActivity.LOG_TAG, msg);
});
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import android.database.Cursor;
import android.example.com.squawker.provider.SquawkContract;
import android.support.v7.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,37 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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.
*/
* Copyright (C) 2017 The Android Open Source Project
*
* 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.
*/

package android.example.com.squawker.fcm;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.google.firebase.messaging.FirebaseMessagingService;

/**
* Listens for changes in the InstanceID
*/
public class SquawkFirebaseInstanceIdService extends FirebaseInstanceIdService {
public class SquawkFirebaseInstanceIdService extends FirebaseMessagingService{

private static String LOG_TAG = SquawkFirebaseInstanceIdService.class.getSimpleName();

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(LOG_TAG, "Refreshed token: " + refreshedToken);
public void onNewToken(String token) {
Log.d(LOG_TAG, "Refreshed token: " + token);

// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
// FCM registration token to your app server.
sendRegistrationToServer(token);
}

/**
* Persist token to third-party servers.
* <p>
* 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) {
// This method is blank, but if you were to build a server that stores users token
// information, this is where you'd send the token to the server.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* 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.
*/
* Copyright (C) 2017 The Android Open Source Project
*
* 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.
*/

package android.example.com.squawker.fcm;

import android.app.NotificationManager;
Expand All @@ -27,9 +28,10 @@
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

Expand Down Expand Up @@ -57,22 +59,6 @@ public class SquawkFirebaseMessageService extends FirebaseMessagingService {
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// There are two types of messages data messages and notification messages. Data messages
// are handled
// here in onMessageReceived whether the app is in the foreground or background. Data
// messages are the type
// traditionally used with FCM. Notification messages are only received here in
// onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated
// notification is displayed.
// When the user taps on the notification they are returned to the app. Messages
// containing both notification
// and data payloads are treated as notification messages. The Firebase console always
// sends notification
// messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options\

// The Squawk server always sends just *data* messages, meaning that onMessageReceived when
// the app is both in the foreground AND the background

Log.d(LOG_TAG, "From: " + remoteMessage.getFrom());

Expand All @@ -99,9 +85,8 @@ private void insertSquawk(final Map<String, String> data) {

// Database operations should not be done on the main thread
AsyncTask<Void, Void, Void> insertSquawkTask = new AsyncTask<Void, Void, Void>() {

@Override
protected Void doInBackground(Void... voids) {
protected Void doInBackground(Void... voids) {
ContentValues newMessage = new ContentValues();
newMessage.put(SquawkContract.COLUMN_AUTHOR, data.get(JSON_KEY_AUTHOR));
newMessage.put(SquawkContract.COLUMN_MESSAGE, data.get(JSON_KEY_MESSAGE).trim());
Expand Down Expand Up @@ -151,4 +136,4 @@ private void sendNotification(Map<String, String> data) {

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import android.example.com.squawker.R;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;
import android.view.MenuItem;

/**
Expand Down
Loading