Created
August 8, 2017 01:40
-
-
Save barbietunnie/190c5fe56f9430f7e6ab1ede19f41570 to your computer and use it in GitHub Desktop.
Sample implementation of FirebaseMessagingService that shows a Notification and saves data in Content Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
import android.app.PendingIntent; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.example.com.squawker.MainActivity; | |
import android.example.com.squawker.R; | |
import android.example.com.squawker.provider.SquawkContract; | |
import android.example.com.squawker.provider.SquawkProvider; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
import java.util.Map; | |
/** | |
* Listens for squawk FCM messages both in the background and the foreground and responds | |
* appropriately | |
* depending on type of message | |
*/ | |
public class SquawkFirebaseMessageService extends FirebaseMessagingService { | |
private static final String JSON_KEY_AUTHOR = SquawkContract.COLUMN_AUTHOR; | |
private static final String JSON_KEY_AUTHOR_KEY = SquawkContract.COLUMN_AUTHOR_KEY; | |
private static final String JSON_KEY_MESSAGE = SquawkContract.COLUMN_MESSAGE; | |
private static final String JSON_KEY_DATE = SquawkContract.COLUMN_DATE; | |
private static final int NOTIFICATION_MAX_CHARACTERS = 30; | |
private static String LOG_TAG = SquawkFirebaseMessageService.class.getSimpleName(); | |
/** | |
* Called when message is received. | |
* | |
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging | |
*/ | |
@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()); | |
// Check if message contains a data payload. | |
Map<String, String> data = remoteMessage.getData(); | |
if (data.size() > 0) { | |
Log.d(LOG_TAG, "Message data payload: " + data); | |
// Send a notification that you got a new message | |
sendNotification(data); | |
insertSquawk(data); | |
} | |
} | |
/** | |
* Inserts a single squawk into the database; | |
* | |
* @param data Map which has the message data in it | |
*/ | |
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) { | |
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()); | |
newMessage.put(SquawkContract.COLUMN_DATE, data.get(JSON_KEY_DATE)); | |
newMessage.put(SquawkContract.COLUMN_AUTHOR_KEY, data.get(JSON_KEY_AUTHOR_KEY)); | |
getContentResolver().insert(SquawkProvider.SquawkMessages.CONTENT_URI, newMessage); | |
return null; | |
} | |
}; | |
insertSquawkTask.execute(); | |
} | |
/** | |
* Create and show a simple notification containing the received FCM message | |
* | |
* @param data Map which has the message data in it | |
*/ | |
private void sendNotification(Map<String, String> data) { | |
Intent intent = new Intent(this, MainActivity.class); | |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
// Create the pending intent to launch the activity | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, | |
PendingIntent.FLAG_ONE_SHOT); | |
String author = data.get(JSON_KEY_AUTHOR); | |
String message = data.get(JSON_KEY_MESSAGE); | |
// If the message is longer than the max number of characters we want in our | |
// notification, truncate it and add the unicode character for ellipsis | |
if (message.length() > NOTIFICATION_MAX_CHARACTERS) { | |
message = message.substring(0, NOTIFICATION_MAX_CHARACTERS) + "\u2026"; | |
} | |
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | |
.setSmallIcon(R.drawable.ic_duck) | |
.setContentTitle(String.format(getString(R.string.notification_message), author)) | |
.setContentText(message) | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment