Added Android code
[wl-app.git] / Android / app / src / main / java / com / moiseum / wolnelektury / connection / WolneLekturyFirebaseMessagingService.java
1 package com.moiseum.wolnelektury.connection;
2
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;
13
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;
22
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26
27 import timber.log.Timber;
28
29 /**
30  * Created by Piotr Ostrowski on 27.08.2018.
31  */
32 public class WolneLekturyFirebaseMessagingService extends FirebaseMessagingService {
33
34         private static final String TAG = WolneLekturyFirebaseMessagingService.class.getSimpleName();
35         private SharedPreferencesUtils preferences = WLApplication.getInstance().getPreferences();
36
37         @Override
38         public void onNewToken(String s) {
39                 if (BuildConfig.DEBUG) {
40                         Log.d(TAG, "Refreshed token: " + s);
41                 }
42                 FirebaseMessaging.getInstance().subscribeToTopic(getString(R.string.default_notification_topic));
43         }
44
45         @Override
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);
54                 } else {
55                         Log.d(TAG, "Skipping notification cause of user preference");
56                 }
57         }
58
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);
63
64                 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
65                 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
66                                 .setLargeIcon(image)
67                                 .setSmallIcon(R.drawable.ic_notification)
68                                 .setContentTitle(title)
69                                 .setContentText(body)
70                                 .setColor(ContextCompat.getColor(this, R.color.colorAccent))
71                                 .setColorized(true)
72                                 .setStyle(new NotificationCompat.BigPictureStyle()
73                                                 .bigPicture(image)
74                                                 .setBigContentTitle(title)
75                                                 .setSummaryText(body)
76                                 )
77                                 .setAutoCancel(true)
78                                 .setSound(defaultUri)
79                                 .setContentIntent(pendingIntent);
80
81                 if (notificationManager != null) {
82                         notificationManager.notify(0, notificationBuilder.build());
83                 }
84         }
85
86         /*
87          *To get a Bitmap image from the URL received
88          * */
89         private Bitmap getBitmapFromUrl(String imageUrl) {
90                 if (imageUrl == null) {
91                         return null;
92                 }
93                 try {
94                         URL url = new URL(imageUrl);
95                         HttpURLConnection connection = (HttpURLConnection) url.openConnection();
96                         connection.setDoInput(true);
97                         connection.connect();
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());
102                         return null;
103                 }
104         }
105 }