Java tutorial
/* * Copyright (c) 2016 escoand. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.escoand.readdaily; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.media.RingtoneManager; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class PushMessageService extends FirebaseMessagingService { @Override public void onMessageReceived(final RemoteMessage remoteMessage) { RemoteMessage.Notification notification = remoteMessage.getNotification(); String title = getString(R.string.app_title); String message = ""; Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); LogHandler.log(Log.WARN, "received message"); // get title if (notification != null && notification.getTitle() != null && !notification.getTitle().isEmpty()) title = notification.getTitle(); // get message if (notification != null && notification.getBody() != null && !notification.getBody().isEmpty()) message = notification.getBody(); else if (remoteMessage.getData().containsKey("message")) message = remoteMessage.getData().get("message"); // build notification NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.icon_notification) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .setContentTitle(title).setContentText(message).setAutoCancel(true) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentIntent(pendingIntent); ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)) .notify((int) remoteMessage.getSentTime(), builder.build()); } }