Skip to content

Commit

Permalink
Draft for notifs
Browse files Browse the repository at this point in the history
  • Loading branch information
baronhsieh2005 committed Nov 12, 2024
1 parent bfa19f6 commit 8281125
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions PennMobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dependencies {
testImplementation platform(libs.androidx.compose.bom)
androidTestImplementation platform(libs.androidx.compose.bom)
implementation platform(libs.firebase.bom)
implementation(libs.firebase.messaging)

implementation libs.bundles.compose
implementation libs.bundles.material
Expand Down
14 changes: 13 additions & 1 deletion PennMobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name="androidx.multidex.MultiDexApplication"
Expand Down Expand Up @@ -55,7 +56,12 @@
</activity>
<service android:name=".dining.widget.DiningHallWidgetAdapter"
android:permission="android.permission.BIND_REMOTEVIEWS" />

<service android:name=".notifications.PushNotificationService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Expand All @@ -65,6 +71,12 @@
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/penn_red" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/baseline_circle_notifications_24" />

<receiver
android:name=".laundry.LaundryBroadcastReceiver"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pennapps.labs.pennmobile

import StudentLifeRf2
import android.Manifest
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
Expand All @@ -20,8 +21,10 @@ import android.view.inputmethod.InputMethodManager
import android.webkit.CookieManager
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.graphics.ColorUtils
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
Expand Down Expand Up @@ -77,6 +80,18 @@ class MainActivity : AppCompatActivity() {
private lateinit var mFirebaseAnalytics: FirebaseAnalytics
val mNetworkManager by lazy { OAuth2NetworkManager(this) }

// Declare the launcher at the top of your Activity/Fragment:
private val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) { isGranted: Boolean ->
if (isGranted) {
// FCM SDK (and your app) can post notifications.
} else {
// TODO: Inform user that that your app will not show notifications.
}
}

override fun onCreate(savedInstanceState: Bundle?) {
if (Build.VERSION.SDK_INT > 28) {
setTheme(R.style.DarkModeApi29)
Expand All @@ -91,6 +106,7 @@ class MainActivity : AppCompatActivity() {
setTheme(R.style.DarkBackground)
}
Utils.getCurrentSystemTime()
askNotificationPermission()

setSupportActionBar(binding.include.toolbar)
fragmentManager = supportFragmentManager
Expand Down Expand Up @@ -132,6 +148,25 @@ class MainActivity : AppCompatActivity() {
}
}

private fun askNotificationPermission() {
// This is only necessary for API level >= 33 (TIRAMISU)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
PackageManager.PERMISSION_GRANTED
) {
// FCM SDK (and your app) can post notifications.
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
// TODO: display an educational UI explaining to the user the features that will be enabled
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
// If the user selects "No thanks," allow the user to continue without notifications.
} else {
// Directly ask for the permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}

private fun onExpandableBottomNavigationItemSelected() {
binding.include.expandableBottomBar.setOnNavigationItemSelectedListener { item ->
val position =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.pennapps.labs.pennmobile.notifications

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.pennapps.labs.pennmobile.MainActivity
import com.pennapps.labs.pennmobile.R
import java.net.URL

class PushNotificationService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
// Update Server/Database
Log.d("FCM Registration", "Refreshed token: $token")
}

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
Log.d("Notification", "Notification received!")
val title = message.notification?.title
val body = message.notification?.body
val imageUrl = message.notification?.imageUrl

val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val notificationChannel =
NotificationChannel(
"MAIN_CHANNEL",
"Main Channel",
NotificationManager.IMPORTANCE_HIGH,
)
notificationManager.createNotificationChannel(notificationChannel)

val mainActivityIntent = Intent(this, MainActivity::class.java)
mainActivityIntent.apply {
flags += Intent.FLAG_ACTIVITY_NEW_TASK
flags += Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, PendingIntent.FLAG_IMMUTABLE)

val notificationBuilder =
NotificationCompat
.Builder(this, "MAIN_CHANNEL")
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.drawable.baseline_circle_notifications_24)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)

imageUrl?.let {
val bitmap = BitmapFactory.decodeStream(URL(imageUrl.toString()).openConnection().getInputStream())
notificationBuilder.setLargeIcon(bitmap)
notificationBuilder.setStyle(NotificationCompat.BigPictureStyle().bigPicture(bitmap))
}

notificationManager.notify(1, notificationBuilder.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#011F5B" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,18.5c-0.83,0 -1.5,-0.67 -1.5,-1.5h3c0,0.83 -0.67,1.5 -1.5,1.5zM17,16L7,16v-1l1,-1v-2.61C8,9.27 9.03,7.47 11,7v-0.5c0,-0.57 0.43,-1 1,-1s1,0.43 1,1L13,7c1.97,0.47 3,2.28 3,4.39L16,14l1,1v1z"/>

</vector>
1 change: 1 addition & 0 deletions PennMobile/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
<color name="light_blue_200">#FF81D4FA</color>
<color name="light_blue_600">#FF039BE5</color>
<color name="light_blue_900">#FF01579B</color>
<color name="penn_red">#990000</color>

</resources>
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ coordinatorlayout = "1.2.0"
customalertviewdialogue = "a1fc69d54d"
espressoCore = "3.5.0"
exifinterface = "1.3.6"
firebaseBom = "31.5.0"
firebaseBom = "33.5.1"
firebaseCrashlyticsKtx = "18.6.0"
firebaseCrashalytics = "2.9.9"
firebaseMessaging = "24.0.3"
glanceAppwidget = "1.1.0"
glide = "4.11.0"
googleMapsServices = "2.2.0"
Expand Down Expand Up @@ -103,6 +104,7 @@ firebase-analytics = { module = "com.google.firebase:firebase-analytics" }
firebase-bom = { module = "com.google.firebase:firebase-bom", version.ref = "firebaseBom" }
firebase-crashlytics = { module = "com.google.firebase:firebase-crashlytics" }
firebase-crashlytics-ktx = { module = "com.google.firebase:firebase-crashlytics-ktx", version.ref = "firebaseCrashlyticsKtx" }
firebase-messaging = { module = "com.google.firebase:firebase-messaging", version.ref = "firebaseMessaging" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
google-maps-services = { module = "com.google.maps:google-maps-services", version.ref = "googleMapsServices" }
joda-time = { module = "joda-time:joda-time", version.ref = "jodaTime" }
Expand Down

0 comments on commit 8281125

Please sign in to comment.