Firebase Cloud Message学習
22070 ワード
graddleとdependency/Firebase dependency comple‘comple.google.firebase:firebase-messaging:10.0.1’を使用します.
appyplugin:「comple.gms.google-services」はまずFirebaseのgoogleアカウントを登録して、FCMを使う過程で、APPのFirebase token、String token=FirebaseInstance Id.getInstance()を知っています.String msg=get String(R.string.message)
本プロジェクトで使用するFCMの2つの機能:1、serverは1つに伝播し、googleのFirebaseで検証情報を送るのはnotification msg 2、serverはマルチに伝播し、FirebaseMessaging.get Instance().subscribeTopic(key)を使う.同じtopic subscribeのアプリを維持して放送することができます.
exted FirebaseMessagingService override onMessage Received(RemoteMessage remoteMessage)は、remoteMessage.getDataを通じてFCMのデータを取得します.
appyplugin:「comple.gms.google-services」はまずFirebaseのgoogleアカウントを登録して、FCMを使う過程で、APPのFirebase token、String token=FirebaseInstance Id.getInstance()を知っています.String msg=get String(R.string.message)
本プロジェクトで使用するFCMの2つの機能:1、serverは1つに伝播し、googleのFirebaseで検証情報を送るのはnotification msg 2、serverはマルチに伝播し、FirebaseMessaging.get Instance().subscribeTopic(key)を使う.同じtopic subscribeのアプリを維持して放送することができます.
exted FirebaseMessagingService override onMessage Received(RemoteMessage remoteMessage)は、remoteMessage.getDataを通じてFCMのデータを取得します.
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);
}
}
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());
}