Last active
March 15, 2020 07:08
-
-
Save geekykant/2cacfa1a74152f2df1118c4f2e7f9f42 to your computer and use it in GitHub Desktop.
Firebase FCM Notification (Big Image & Normal Normal Notification)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
| private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); | |
| @Override | |
| public void onNewToken(String token) { | |
| Log.d(TAG, "Refreshed token: " + token); | |
| } | |
| @Override | |
| public void onMessageReceived(RemoteMessage remoteMessage) { | |
| Log.d(TAG, "From: " + remoteMessage.getFrom()); | |
| // Check if message contains a data payload. | |
| if (remoteMessage.getData().size() > 0) { | |
| Log.d(TAG, "Message data payload: " + remoteMessage.getData()); | |
| Map<String, String> notif = remoteMessage.getData(); | |
| String title = notif.get("title"); | |
| String message = notif.get("message"); | |
| String image_url = notif.get("image_url"); | |
| // Log.d(TAG, "onMessageReceived: " + title + " " + message + " " + image_url); | |
| if (!TextUtils.isEmpty(image_url) && !TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { | |
| getImageNotif(notif.get("title"), notif.get("message"), notif.get("image_url")); | |
| } else if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) { | |
| createNotification(title, message); | |
| } | |
| } | |
| // Check if message contains a notification payload. | |
| if (remoteMessage.getNotification() != null) { | |
| Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); | |
| if (!TextUtils.isEmpty(remoteMessage.getNotification().getTitle()) && !TextUtils.isEmpty(remoteMessage.getNotification().getBody())) { | |
| createNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody()); | |
| } | |
| } | |
| } | |
| private void getImageNotif(final String title, final String message, String url) { | |
| Glide.with(getApplicationContext()) | |
| .asBitmap() | |
| .load(url) | |
| .into(new CustomTarget<Bitmap>() { | |
| @Override | |
| public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { | |
| displayImageNotification(title, message, resource); | |
| } | |
| @Override | |
| public void onLoadCleared(@Nullable Drawable placeholder) { | |
| } | |
| }); | |
| } | |
| private void displayImageNotification(String title, String message, Bitmap bitmap) { | |
| NotificationCompat.Builder mBuilder; | |
| // define sound URI, the sound to be played when there's a notification | |
| Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
| // intent triggered, you can add other intent for other actions | |
| Intent intent = new Intent(MyFirebaseMessagingService.this, SplashActivity.class); | |
| PendingIntent pIntent = PendingIntent.getActivity(MyFirebaseMessagingService.this, 0, intent, 0); | |
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
| // Configure the channel | |
| int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
| NotificationChannel channel = null; | |
| channel = new NotificationChannel(getString(R.string.default_notification_channel), "Push Notifications", importance); | |
| channel.setDescription("Reminders"); | |
| // Register the channel with the notifications manager | |
| NotificationManager mNotificationManager = | |
| (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| mNotificationManager.createNotificationChannel(channel); | |
| mBuilder = | |
| // Builder class for devices targeting API 26+ requires a channel ID | |
| new NotificationCompat.Builder(this, getString(R.string.default_notification_channel)) | |
| .setSmallIcon(R.drawable.ic_cart) | |
| .setColor(getColor(R.color.colorAccent)) | |
| .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)) | |
| .setContentTitle(title) | |
| .setContentIntent(pIntent) | |
| .setSound(soundUri) | |
| .setContentText(message); | |
| } else { | |
| mBuilder = | |
| // this Builder class is deprecated | |
| new NotificationCompat.Builder(this) | |
| .setSmallIcon(R.drawable.ic_cart) | |
| .setColor(getColor(R.color.colorAccent)) | |
| .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap)) | |
| .setContentTitle(title) | |
| .setContentIntent(pIntent) | |
| .setSound(soundUri) | |
| .setContentText(message); | |
| } | |
| NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; | |
| Notification notification = mBuilder.build(); | |
| notificationManager.notify(0, notification); | |
| } | |
| private void createNotification(@NonNull String title, @NonNull String message) { | |
| NotificationCompat.Builder mBuilder; | |
| // define sound URI, the sound to be played when there's a notification | |
| Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
| // intent triggered, you can add other intent for other actions | |
| Intent intent = new Intent(MyFirebaseMessagingService.this, SplashActivity.class); | |
| PendingIntent pIntent = PendingIntent.getActivity(MyFirebaseMessagingService.this, 0, intent, 0); | |
| if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { | |
| // Configure the channel | |
| int importance = NotificationManager.IMPORTANCE_DEFAULT; | |
| NotificationChannel channel = null; | |
| channel = new NotificationChannel(getString(R.string.default_notification_channel), "Push Notifications", importance); | |
| channel.setDescription("Reminders"); | |
| // Register the channel with the notifications manager | |
| NotificationManager mNotificationManager = | |
| (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| mNotificationManager.createNotificationChannel(channel); | |
| mBuilder = | |
| // Builder class for devices targeting API 26+ requires a channel ID | |
| new NotificationCompat.Builder(this, getString(R.string.default_notification_channel)) | |
| .setSmallIcon(R.drawable.ic_cart) | |
| .setColor(getColor(R.color.colorAccent)) | |
| .setContentTitle(title) | |
| .setContentIntent(pIntent) | |
| .setSound(soundUri) | |
| .setContentText(message); | |
| } else { | |
| mBuilder = | |
| // this Builder class is deprecated | |
| new NotificationCompat.Builder(this) | |
| .setSmallIcon(R.drawable.ic_cart) | |
| .setColor(getColor(R.color.colorAccent)) | |
| .setContentTitle(title) | |
| .setContentIntent(pIntent) | |
| .setSound(soundUri) | |
| .setContentText(message); | |
| } | |
| NotificationManager mNotificationManager = | |
| (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
| // mId allows you to update the notification later on. | |
| mNotificationManager.notify(101, mBuilder.build()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment