Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created November 12, 2018 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesselima/e26bae06507c713524f440d3521faf9d to your computer and use it in GitHub Desktop.
Save jesselima/e26bae06507c713524f440d3521faf9d to your computer and use it in GitHub Desktop.
Test
/*
* 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;
/**
* Listens for changes in the InstanceID
*/
public class SquawkFirebaseInstanceIdService extends FirebaseInstanceIdService {
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);
// 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);
}
/**
* 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.
}
}
/*
* 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