50 | * Modify this method to associate the user's FCM InstanceID token with any server-side account
51 | * maintained by your application.
52 | *
53 | * @param token The new token.
54 | */
55 | private void sendRegistrationToServer(String token) {
56 | // This method is blank, but if you were to build a server that stores users token
57 | // information, this is where you'd send the token to the server.
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/android/example/com/squawker/fcm/SquawkFirebaseMessageService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package android.example.com.squawker.fcm;
17 |
18 | import android.app.NotificationManager;
19 | import android.app.PendingIntent;
20 | import android.content.ContentValues;
21 | import android.content.Context;
22 | import android.content.Intent;
23 | import android.example.com.squawker.MainActivity;
24 | import android.example.com.squawker.R;
25 | import android.example.com.squawker.provider.SquawkContract;
26 | import android.example.com.squawker.provider.SquawkProvider;
27 | import android.media.RingtoneManager;
28 | import android.net.Uri;
29 | import android.os.AsyncTask;
30 | import android.support.v4.app.NotificationCompat;
31 | import android.util.Log;
32 |
33 | import com.google.firebase.messaging.FirebaseMessagingService;
34 | import com.google.firebase.messaging.RemoteMessage;
35 |
36 | import java.util.Map;
37 |
38 | /**
39 | * Listens for squawk FCM messages both in the background and the foreground and responds
40 | * appropriately
41 | * depending on type of message
42 | */
43 | public class SquawkFirebaseMessageService extends FirebaseMessagingService {
44 |
45 | private static final String JSON_KEY_AUTHOR = SquawkContract.COLUMN_AUTHOR;
46 | private static final String JSON_KEY_AUTHOR_KEY = SquawkContract.COLUMN_AUTHOR_KEY;
47 | private static final String JSON_KEY_MESSAGE = SquawkContract.COLUMN_MESSAGE;
48 | private static final String JSON_KEY_DATE = SquawkContract.COLUMN_DATE;
49 |
50 | private static final int NOTIFICATION_MAX_CHARACTERS = 30;
51 | private static String LOG_TAG = SquawkFirebaseMessageService.class.getSimpleName();
52 |
53 | /**
54 | * Called when message is received.
55 | *
56 | * @param remoteMessage Object representing the message received from Firebase Cloud Messaging
57 | */
58 | @Override
59 | public void onMessageReceived(RemoteMessage remoteMessage) {
60 | // There are two types of messages data messages and notification messages. Data messages
61 | // are handled
62 | // here in onMessageReceived whether the app is in the foreground or background. Data
63 | // messages are the type
64 | // traditionally used with FCM. Notification messages are only received here in
65 | // onMessageReceived when the app
66 | // is in the foreground. When the app is in the background an automatically generated
67 | // notification is displayed.
68 | // When the user taps on the notification they are returned to the app. Messages
69 | // containing both notification
70 | // and data payloads are treated as notification messages. The Firebase console always
71 | // sends notification
72 | // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options\
73 |
74 | // The Squawk server always sends just *data* messages, meaning that onMessageReceived when
75 | // the app is both in the foreground AND the background
76 |
77 | Log.d(LOG_TAG, "From: " + remoteMessage.getFrom());
78 |
79 | // Check if message contains a data payload.
80 |
81 | Map