|
1 | 1 | package yankov.tsvetilian.watchit.Services; |
2 | 2 |
|
| 3 | +import android.app.NotificationChannel; |
| 4 | +import android.app.NotificationManager; |
| 5 | +import android.app.PendingIntent; |
| 6 | +import android.content.Intent; |
| 7 | +import android.os.Build; |
| 8 | +import android.support.v4.app.NotificationCompat; |
| 9 | +import android.support.v4.app.NotificationManagerCompat; |
| 10 | + |
3 | 11 | import com.google.firebase.messaging.FirebaseMessagingService; |
4 | 12 | import com.google.firebase.messaging.RemoteMessage; |
5 | 13 |
|
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import yankov.tsvetilian.watchit.Activities.PlayerActivity; |
| 17 | +import yankov.tsvetilian.watchit.Models.WatchModel; |
6 | 18 | import yankov.tsvetilian.watchit.NetworkHandler; |
| 19 | +import yankov.tsvetilian.watchit.R; |
7 | 20 |
|
8 | 21 | public class PlayMessagingService extends FirebaseMessagingService { |
9 | 22 |
|
10 | 23 | @Override |
11 | 24 | public void onMessageReceived(RemoteMessage remoteMessage) { |
12 | | - //TODO |
| 25 | + createWatchObject(remoteMessage.getData()); |
| 26 | + } |
| 27 | + |
| 28 | + private void createWatchObject(Map<String, String> remoteMessage) { |
| 29 | + String seasonCheck = null; |
| 30 | + |
| 31 | + if (!remoteMessage.get("season").equals("null")) { |
| 32 | + seasonCheck = remoteMessage.get("season"); |
| 33 | + } |
| 34 | + |
| 35 | + WatchModel watch = new WatchModel( |
| 36 | + remoteMessage.get("name"), |
| 37 | + remoteMessage.get("play"), |
| 38 | + remoteMessage.get("playFrom"), |
| 39 | + remoteMessage.get("sourceName"), |
| 40 | + remoteMessage.get("key"), |
| 41 | + remoteMessage.get("path"), |
| 42 | + remoteMessage.get("poster"), |
| 43 | + remoteMessage.get("description"), |
| 44 | + remoteMessage.get("time"), |
| 45 | + false, |
| 46 | + false, |
| 47 | + seasonCheck |
| 48 | + ); |
| 49 | + |
| 50 | + createChannel(watch); |
| 51 | + } |
| 52 | + |
| 53 | + private void createChannel(WatchModel watch) { |
| 54 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 55 | + NotificationChannel notificationChannel = new NotificationChannel("WATCHIT", "WatchIt Channel", NotificationManager.IMPORTANCE_HIGH); |
| 56 | + NotificationManager notificationManager = getSystemService(NotificationManager.class); |
| 57 | + if (notificationManager != null) { |
| 58 | + notificationManager.createNotificationChannel(notificationChannel); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + buildNotification(watch); |
| 63 | + } |
| 64 | + |
| 65 | + private void buildNotification(WatchModel watch) { |
| 66 | + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); |
| 67 | + |
| 68 | + Intent startPlayer = new Intent(getApplicationContext(), PlayerActivity.class); |
| 69 | + startPlayer.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 70 | + startPlayer.putExtra("watch", watch); |
| 71 | + PendingIntent play = PendingIntent.getActivity(this, 90, startPlayer, PendingIntent.FLAG_UPDATE_CURRENT); |
| 72 | + |
| 73 | + |
| 74 | + Intent watchLater = new Intent(getApplicationContext(), PlayerActivity.class); |
| 75 | + watchLater.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 76 | + watchLater.putExtra("later", watch); |
| 77 | + PendingIntent later = PendingIntent.getActivity(this, 100, watchLater, PendingIntent.FLAG_UPDATE_CURRENT); |
| 78 | + |
| 79 | + NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "WATCHIT") |
| 80 | + .setSmallIcon(R.drawable.ic_play) |
| 81 | + .setContentTitle(watch.getPresentableName()) |
| 82 | + .setContentText(watch.getDescription()) |
| 83 | + .setContentIntent(play) |
| 84 | + .setStyle(new NotificationCompat.BigTextStyle() |
| 85 | + .setBigContentTitle(watch.getPresentableName()) |
| 86 | + .bigText(watch.getDescription()) |
| 87 | + .setSummaryText(watch.getSourceName())) |
| 88 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 89 | + .addAction(R.drawable.ic_play, "Play", play) |
| 90 | + .addAction(R.drawable.ic_later, "Later", later) |
| 91 | + .setAutoCancel(true); |
| 92 | + |
| 93 | + notificationManager.notify(110, notification.build()); |
13 | 94 | } |
14 | 95 |
|
15 | 96 | @Override |
|
0 commit comments