[ ] android . |
Android-: (Fabric method). Firebase Cloud Messaging ( FCM). , , - .
FCM ( ), -. (// ), , FCM . FCM Google . , , , , .
push- , . UI- -, : / . , , . , . , - . , -, , , if else, , , , .
, , . , , , . . , , .
:
Android Studio
core . parent teacher .
push- , .
. , .
:
:
FCM Android- push- , FirebaseMessagingService.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
FCM :
-> FCM Android
-> Firebase
onMessageReceived() RemoteMessage, , : , Map , push- .
. push , , , onMessageReceived() . firebase-messaging push` . . ( ) data, MyFirebaseMessagingService. FCM. data.
-> Firebase
, , :
public class TeacherFirebaseMessagingService extends FirebaseMessagingService {
private static final String KEY_PUSH_TYPE = "type";
private static final String KEY_PUSH_TITLE = "title";
private static final String KEY_PUSH_CONTENT = "content";
private static final String TYPE_NEW_CHILD = "add_child";
private static final String TYPE_BIRTHDAY = "birthday";
private static final String EMPTY_STRING = "";
private static final int DEFAULT_NOTIFICATION_ID = 15;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Map data = remoteMessage.getData();
if (data.containsKey(KEY_PUSH_TYPE)) {
NotificationCompat.Builder notificationBuilder;
String notificationTitle = null;
if (data.containsKey(KEY_PUSH_TITLE)) {
notificationTitle = data.get(KEY_PUSH_TITLE);
}
String notificationContent = null;
if (data.containsKey(KEY_PUSH_CONTENT)) {
notificationContent = data.get(KEY_PUSH_CONTENT);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
String pushType = data.get(KEY_PUSH_TYPE);
if (pushType.equals(TYPE_NEW_CHILD)) {
builder.setSmallIcon(R.drawable.ic_add_child)
.setContentTitle(notificationTitle != null ? notificationTitle : EMPTY_STRING)
.setContentText(notificationContent != null ? notificationContent : EMPTY_STRING);
} else if (pushType.equals(TYPE_BIRTHDAY)) {
// notificationBuilder = ....
//
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(DEFAULT_NOTIFICATION_ID, builder.build());
}
}
title content remoteMessage:
private static final String KEY_TITLE = "title";
private static final String KEY_CONTENT = "content";
:
private static final String TYPE_NEW_CHILD = "add_child";
private static final String TYPE_BIRTHDAY = "birthday";
, private NotificationCompat.Builder. VK , :
TeacherFirebaseMessagingReceiver god object`. . , - .
.
, core:
public class CoreFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
CoreFirebaseMessagingService .
CoreNotification. , .
public abstract class CoreNotification {
public static final String KEY_FROM_PUSH = "CoreNotification.FromNotification";
private static final String KEY_TITLE = "title";
private static final String KEY_CONTENT = "body";
protected static final String STRING_EMPTY = "";
protected RemoteMessage remoteMessage;
public CoreNotification(RemoteMessage remoteMessage) {
this.remoteMessage = remoteMessage;
}
protected String getTitleFromMessage() {
Map data = remoteMessage.getData();
if (data.containsKey(KEY_TITLE)) {
return data.get(KEY_TITLE);
} else {
return STRING_EMPTY;
}
}
protected String getContentFromMessage() {
Map data = remoteMessage.getData();
if (data.containsKey(KEY_CONTENT)) {
return data.get(KEY_CONTENT);
} else {
return STRING_EMPTY;
}
}
public String getTitle() {
return getTitleFromMessage();
}
public String getContent() {
return getContentFromMessage();
}
protected abstract PendingIntent configurePendingIntent(Context context);
protected abstract @DrawableRes int largeIcon();
protected abstract String getNotificationTag();
}
RemoteMessage.
. STRING_EMPTY , protected.
- . , , Java- , CoreNotification , . . title content . , getTitleFromMessage() getContentFromMessage(). ( title content RemoteMessage.getData() , ). protected, title content - .
CoreNotificationCreator. . CoreNotification.
public abstract class CoreNotificationCreator {
private static final String KEY_NOTIFICATION_TAG = "CoreNotificationCreator.TagKey";
private static final String DEFAULT_TAG = "CoreNotificationCreator.DefaultTag";
private static final String KEY_TYPE = "type";
private NotificationManager notificationManager;
public CoreNotificationCreator(Context context) {
notificationManager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
}
public void showNotification(Context context, RemoteMessage remoteMessage) {
String notificationType = getNotificationType(remoteMessage);
CoreNotification notification = factoryMethod(notificationType, remoteMessage);
if (notification != null) {
NotificationCompat.Builder builder = builderFromPushNotification(context, notification);
notify(builder);
}
}
private String getNotificationType(RemoteMessage remoteMessage) {
Map data = remoteMessage.getData();
if (data.containsKey(KEY_TYPE)) {
return data.get(KEY_TYPE);
}
return "";
}
@Nullable
protected abstract CoreNotification factoryMethod(String messageType, RemoteMessage remoteMessage);
private final static int DEFAULT_NOTIFICATION_ID = 15;
private static final
@DrawableRes
int SMALL_ICON_RES_ID = R.drawable.ic_notification_small;
protected NotificationCompat.Builder builderFromPushNotification(Context context, CoreNotification notification) {
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), notification.largeIcon());
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(SMALL_ICON_RES_ID)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentTitle(notification.getTitle())
.setContentText(notification.getContent())
.setLargeIcon(largeIcon);
builder.getExtras().putString(KEY_NOTIFICATION_TAG, notification.getNotificationTag());
builder.setContentIntent(notification.configurePendingIntent(context));
return builder;
}
private void notify(@NonNull NotificationCompat.Builder builder) {
final String notificationTag = getNotificationTag(builder);
notificationManager.cancel(notificationTag, DEFAULT_NOTIFICATION_ID);
notificationManager.notify(notificationTag, DEFAULT_NOTIFICATION_ID, builder.build());
}
private String getNotificationTag(NotificationCompat.Builder builder) {
Bundle extras = builder.getExtras();
if (extras.containsKey(KEY_NOTIFICATION_TAG)) {
return extras.getString(KEY_NOTIFICATION_TAG);
} else {
return DEFAULT_TAG;
}
}
}
showNotification() public . . .
public void showNotification(Context context, RemoteMessage remoteMessage) {
String notificationType = getNotificationType(remoteMessage);
CoreNotification notification = factoryMethod(notificationType, remoteMessage);
if (notification != null) {
NotificationCompat.Builder builder = builderFromPushNotification(context, notification);
notify(builder);
}
}
showNotification() , remoteMessage. remoteMessage , CoreNotification.
factoryMethod() @Nullable , , . . .
, , , . : NotificationCreator.
public class TeacherNotificationCreator extends CoreNotificationCreator {
public TeacherNotificationCreator(Context context) {
super(context);
}
@Nullable
@Override
protected CoreNotification factoryMethod(String messageType, RemoteMessage remoteMessage) {
switch (messageType) {
case NewChildNotification.TYPE:
return new NewChildNotification(remoteMessage);
case BirthdayNotification.TYPE:
return new BirthdayNotification(remoteMessage);
}
return null;
}
}
messageType , CoreNotification .
, :
class NewChildNotification extends CoreNotification {
static final String TYPE = "add_child";
private static final String KEY_CHILD_NAME = "child_name";
NewChildNotification(RemoteMessage remoteMessage) {
super(remoteMessage);
}
@Override
protected PendingIntent configurePendingIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class)
.setPackage(context.getApplicationContext().getPackageName())
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(CoreNotification.KEY_FROM_PUSH, getAddChildInfo());
return PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
@Override
protected int largeIcon() {
return R.drawable.ic_add_child;
}
@Override
protected String getNotificationTag() {
return getClass().getName() + getChildName();
}
private String getChildName() {
Map data = remoteMessage.getData();
if (data.containsKey(KEY_CHILD_NAME)) {
return data.get(KEY_CHILD_NAME);
}
return STRING_EMPTY;
}
private String getAddChildInfo() {
return "New child " + getChildName() + " was added to your group";
}
}
configurePendingIntent() , push-
:
public class ParentNotificationCreator extends CoreNotificationCreator {
public ParentNotificationCreator(Context context) {
super(context);
}
@Nullable
@Override
protected CoreNotification factoryMethod(String messageType, RemoteMessage remoteMessage) {
switch (messageType) {
case PickUpNotification.TYPE:
return new PickUpNotification(remoteMessage);
case GradeNotification.TYPE:
return new GradeNotification(remoteMessage);
default:
return null;
}
}
}
.
. , Firebase. , Android Studio : Tools -> Firebase -> Cloud Messaging Gradle- firebase . FCM Android-
CoreNotificationCreator ( ) + , CoreNotification. . CoreNotification , - , . CoreNotification :
, , , , , , push- .
. , . . , , Android- , .
. . !