Infinite 3 vuotta sitten
vanhempi
sitoutus
57d629aca6

+ 2 - 1
app/.gitignore

@@ -1 +1,2 @@
-/build
+/build
+google-services.json

+ 5 - 0
app/build.gradle

@@ -2,6 +2,8 @@ plugins {
     id 'com.android.application'
 }
 
+apply plugin: 'com.google.gms.google-services'
+
 android {
     compileSdkVersion 30
     buildToolsVersion "30.0.1"
@@ -47,5 +49,8 @@ dependencies {
     implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
     implementation "androidx.recyclerview:recyclerview:1.2.1"
     implementation 'com.facebook.shimmer:shimmer:0.5.0'
+    implementation platform('com.google.firebase:firebase-bom:28.2.1')
+    implementation 'com.google.firebase:firebase-messaging'
+
 
 }

+ 8 - 0
app/src/main/AndroidManifest.xml

@@ -19,6 +19,14 @@
             </intent-filter>
         </activity>
         <activity android:name=".activities.MainActivity" />
+
+
+        <service android:name=".services.NotificationService"  android:exported="true">
+            <intent-filter>
+                <action android:name="com.google.firebase.MESSAGING_EVENT" />
+            </intent-filter>
+        </service>
+
     </application>
 
 </manifest>

+ 19 - 1
app/src/main/java/club/thepenguins/android/activities/MainActivity.java

@@ -10,10 +10,12 @@ import androidx.fragment.app.FragmentManager;
 
 import android.content.res.Configuration;
 import android.os.Bundle;
+import android.util.Log;
 import android.view.MenuItem;
 
 
 import com.google.android.material.navigation.NavigationView;
+import com.google.firebase.messaging.FirebaseMessaging;
 
 
 import club.thepenguins.android.R;
@@ -31,6 +33,7 @@ public class MainActivity extends AppCompatActivity {
 
     private NavigationView nvDrawer;
     private ActionBarDrawerToggle drawerToggle;
+    private static String TAG = "MainActivity";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -63,6 +66,8 @@ public class MainActivity extends AppCompatActivity {
 
         mDrawer.addDrawerListener(drawerToggle);
 
+        FCM();
+
 
         FragmentManager fragmentManager = getSupportFragmentManager();
 
@@ -169,7 +174,7 @@ public class MainActivity extends AppCompatActivity {
 
             default:
 
-                 fragmentClass = HomeFragment.class;
+                fragmentClass = HomeFragment.class;
 
         }
 
@@ -196,5 +201,18 @@ public class MainActivity extends AppCompatActivity {
 
     }
 
+    private void FCM() {
+
+        FirebaseMessaging.getInstance().subscribeToTopic("NewPost").addOnCompleteListener(task -> {
+            if (task.isSuccessful()) {
+                Log.d(TAG, "Successfully Subscribed to Notification Service");
+
+            } else {
+                Log.d(TAG, "Subscription to Notification Service failed");
+            }
+        });
+
+    }
+
 
 }

+ 98 - 8
app/src/main/java/club/thepenguins/android/services/NotificationService.java

@@ -1,15 +1,105 @@
 package club.thepenguins.android.services;
 
-import android.app.Service;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
-import android.os.IBinder;
+import android.os.Build;
+import android.widget.RemoteViews;
 
-import androidx.annotation.Nullable;
+import androidx.core.app.NotificationCompat;
+
+import com.google.firebase.messaging.FirebaseMessagingService;
+import com.google.firebase.messaging.RemoteMessage;
+
+import club.thepenguins.android.R;
+import club.thepenguins.android.activities.MainActivity;
+
+public class NotificationService extends FirebaseMessagingService {
 
-public class NotificationService extends Service {
-    @Nullable
     @Override
-    public IBinder onBind(Intent intent) {
-        return null;
+    public void
+    onMessageReceived(RemoteMessage remoteMessage) {
+
+        if (remoteMessage.getNotification() != null) {
+
+            showNotification(
+                    remoteMessage.getNotification().getTitle(),
+                    remoteMessage.getNotification().getBody());
+        }
+    }
+
+
+    private RemoteViews getCustomDesign(String title,
+                                        String message) {
+        RemoteViews remoteViews = new RemoteViews(
+                getApplicationContext().getPackageName(),
+                R.layout.notification);
+        remoteViews.setTextViewText(R.id.title, title);
+        remoteViews.setTextViewText(R.id.message, message);
+        remoteViews.setImageViewResource(R.id.icon,
+                R.mipmap.ic_launcher);
+        return remoteViews;
+    }
+
+    public void showNotification(String title,
+                                 String message) {
+        Intent intent
+                = new Intent(this, MainActivity.class);
+        String channel_id = "notification_channel";
+
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+
+        PendingIntent pendingIntent
+                = PendingIntent.getActivity(
+                this, 0, intent,
+                PendingIntent.FLAG_ONE_SHOT);
+
+
+        NotificationCompat.Builder builder
+                = new NotificationCompat
+                .Builder(getApplicationContext(),
+                channel_id)
+                .setSmallIcon(R.mipmap.ic_launcher)
+                .setVibrate(new long[]{1000, 1000, 1000,
+                        1000, 1000})
+                .setAutoCancel(true)
+                .setOnlyAlertOnce(true)
+                .setContentIntent(pendingIntent);
+
+        // A customized design for the notification can be
+        // set only for Android versions 4.1 and above. Thus
+        // condition for the same is checked here.
+        if (Build.VERSION.SDK_INT
+                >= Build.VERSION_CODES.JELLY_BEAN) {
+            builder = builder.setContent(
+                    getCustomDesign(title, message));
+        } // If Android Version is lower than Jelly Beans,
+        // customized layout cannot be used and thus the
+        // layout is set as follows
+        else {
+            builder = builder.setContentTitle(title)
+                    .setContentText(message)
+                    .setSmallIcon(R.drawable.ic_launcher_background);
+        }
+        // Create an object of NotificationManager class to
+        // notify the
+        // user of events that happen in the background.
+        NotificationManager notificationManager
+                = (NotificationManager) getSystemService(
+                Context.NOTIFICATION_SERVICE);
+        // Check if the Android Version is greater than Oreo
+        if (Build.VERSION.SDK_INT
+                >= Build.VERSION_CODES.O) {
+            NotificationChannel notificationChannel
+                    = new NotificationChannel(
+                    channel_id, "web_app",
+                    NotificationManager.IMPORTANCE_HIGH);
+            notificationManager.createNotificationChannel(
+                    notificationChannel);
+        }
+
+        notificationManager.notify(0, builder.build());
     }
-}
+}

+ 45 - 0
app/src/main/res/layout/notification.xml

@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/linear_layout"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="horizontal"
+    android:padding="20dp">
+
+    <LinearLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content">
+
+        <ImageView
+            android:id="@+id/icon"
+            android:layout_width="50dp"
+            android:layout_height="50dp"
+            android:padding="5dp"
+            android:src="@drawable/ic_launcher_background" />
+    </LinearLayout>
+
+    <LinearLayout
+        android:layout_width="0dp"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:orientation="vertical"
+        android:padding="5dp">
+
+        <TextView
+            android:id="@+id/title"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="Title"
+            android:textColor="#000"
+            android:textStyle="bold" />
+
+        <TextView
+            android:id="@+id/message"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:text="Message"
+            android:textSize="15sp" />
+
+    </LinearLayout>
+
+</LinearLayout>

+ 1 - 1
build.gradle

@@ -6,7 +6,7 @@ buildscript {
     }
     dependencies {
         classpath "com.android.tools.build:gradle:4.1.2"
-
+        classpath 'com.google.gms:google-services:4.3.8'
         // NOTE: Do not place your application dependencies here; they belong
         // in the individual module build.gradle files
     }