-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfa19f6
commit 8281125
Showing
7 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...obile/src/main/java/com/pennapps/labs/pennmobile/notifications/PushNotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
PennMobile/src/main/res/drawable/baseline_circle_notifications_24.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters