1 package com.moiseum.wolnelektury.connection;
3 import android.app.NotificationManager;
4 import android.app.PendingIntent;
5 import android.content.Context;
6 import android.graphics.Bitmap;
7 import android.graphics.BitmapFactory;
8 import android.media.RingtoneManager;
9 import android.net.Uri;
10 import android.support.v4.app.NotificationCompat;
11 import android.support.v4.content.ContextCompat;
12 import android.util.Log;
14 import com.google.firebase.messaging.FirebaseMessaging;
15 import com.google.firebase.messaging.FirebaseMessagingService;
16 import com.google.firebase.messaging.RemoteMessage;
17 import com.moiseum.wolnelektury.BuildConfig;
18 import com.moiseum.wolnelektury.R;
19 import com.moiseum.wolnelektury.base.WLApplication;
20 import com.moiseum.wolnelektury.utils.SharedPreferencesUtils;
21 import com.moiseum.wolnelektury.view.main.MainActivity;
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
27 import timber.log.Timber;
30 * Created by Piotr Ostrowski on 27.08.2018.
32 public class WolneLekturyFirebaseMessagingService extends FirebaseMessagingService {
34 private static final String TAG = WolneLekturyFirebaseMessagingService.class.getSimpleName();
35 private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
38 public void onNewToken(String s) {
39 if (BuildConfig.DEBUG) {
40 Log.d(TAG, "Refreshed token: " + s);
42 FirebaseMessaging.getInstance().subscribeToTopic(getString(R.string.default_notification_topic));
46 public void onMessageReceived(RemoteMessage remoteMessage) {
47 Log.d(TAG, "Received notification " + remoteMessage);
48 if (preferences.getNotifications()) {
49 String title = remoteMessage.getData().get("title");
50 String body = remoteMessage.getData().get("body");
51 String imageUrl = remoteMessage.getData().get("imageUrl");
52 Bitmap bitmap = getBitmapFromUrl(imageUrl);
53 sendNotification(title, body, bitmap);
55 Log.d(TAG, "Skipping notification cause of user preference");
59 private void sendNotification(String title, String body, Bitmap image) {
60 MainActivity.MainIntent intent = new MainActivity.MainIntent(R.string.app_name, this);
61 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
62 Uri defaultUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
64 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
65 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
67 .setSmallIcon(R.drawable.ic_notification)
68 .setContentTitle(title)
70 .setColor(ContextCompat.getColor(this, R.color.colorAccent))
72 .setStyle(new NotificationCompat.BigPictureStyle()
74 .setBigContentTitle(title)
79 .setContentIntent(pendingIntent);
81 if (notificationManager != null) {
82 notificationManager.notify(0, notificationBuilder.build());
87 *To get a Bitmap image from the URL received
89 private Bitmap getBitmapFromUrl(String imageUrl) {
90 if (imageUrl == null) {
94 URL url = new URL(imageUrl);
95 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
96 connection.setDoInput(true);
98 InputStream input = connection.getInputStream();
99 return BitmapFactory.decodeStream(input);
100 } catch (Exception e) {
101 Timber.tag(TAG).e(e, "Failed to fetch notification image " + e.getMessage());