forked from JunTingLin/drug_android_frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: push notification service and related updates (JunTingLin#31)
* feat: push notification service * update: notification layout * update: app icon * update: change push notification host platform
- Loading branch information
Showing
27 changed files
with
272 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"project_info": { | ||
"project_number": "492668968463", | ||
"project_id": "fcm-push-notification-e4aec", | ||
"storage_bucket": "fcm-push-notification-e4aec.appspot.com" | ||
}, | ||
"client": [ | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:492668968463:android:0678bdeeba840fcb938d7a", | ||
"android_client_info": { | ||
"package_name": "com.junting.drug_android_frontend" | ||
} | ||
}, | ||
"oauth_client": [ | ||
{ | ||
"client_id": "492668968463-vp40cg6ueq2vu2l58cjqn3uckn07dvnn.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyDifL__af4zqstAbbX2j4u2RCZ-kVra6IQ" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [ | ||
{ | ||
"client_id": "492668968463-vp40cg6ueq2vu2l58cjqn3uckn07dvnn.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"configuration_version": "1" | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/junting/drug_android_frontend/MyFirebaseMessagingService.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,71 @@ | ||
package com.junting.drug_android_frontend | ||
|
||
import android.annotation.SuppressLint | ||
import com.google.firebase.messaging.FirebaseMessagingService | ||
|
||
import android.app.NotificationChannel | ||
import android.app.NotificationManager | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import android.util.Log | ||
import android.widget.RemoteViews | ||
import androidx.core.app.NotificationCompat | ||
import com.google.firebase.messaging.RemoteMessage | ||
|
||
const val channelId = "notification_channel" | ||
const val channelName = "com.junting.drug_android_frontend" | ||
class MyFirebaseMessagingService : FirebaseMessagingService(){ | ||
|
||
override fun onNewToken(token: String) { | ||
Log.d("New token", "$token") | ||
} | ||
override fun onMessageReceived(remoteMessage: RemoteMessage) { | ||
|
||
// if (remoteMessage.data.isNotEmpty()) { | ||
Log.d("Message", remoteMessage.notification!!.title!!) | ||
generateNotification(remoteMessage.notification!!.title!!, remoteMessage.notification!!.body!!) | ||
// } | ||
} | ||
|
||
// @SuppressLint("RemoteViewLayout") | ||
// fun getRemoteView(notificationTitle: String, notificationDescription: String) : RemoteViews{ | ||
// | ||
// val remoteViews = RemoteViews("com.junting.drug_android_frontend", R.layout.notification) | ||
// | ||
// remoteViews.setTextViewText(R.id.notification_title, notificationTitle) | ||
// remoteViews.setTextViewText(R.id.notification_description, notificationDescription) | ||
// remoteViews.setImageViewResource(R.id.notification_logo, R.drawable.app_image) | ||
// | ||
// return remoteViews | ||
// } | ||
fun generateNotification(notificationTitle: String, notificationDescription: String){ | ||
|
||
val intent = Intent(this, MainActivity::class.java) | ||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) | ||
|
||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE) | ||
|
||
var builder: NotificationCompat.Builder = NotificationCompat.Builder(applicationContext, channelId) | ||
.setSmallIcon(R.drawable.app_image) | ||
.setContentTitle(notificationTitle) | ||
.setContentText(notificationDescription) | ||
.setAutoCancel(true) | ||
.setVibrate(longArrayOf(1000, 1000, 1000, 1000)) | ||
.setOnlyAlertOnce(true) | ||
.setContentIntent(pendingIntent) | ||
|
||
|
||
// builder = builder.setContent(getRemoteView(notificationTitle, notificationDescription)) | ||
|
||
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ | ||
val notificationChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH) | ||
notificationManager.createNotificationChannel(notificationChannel) | ||
} | ||
|
||
notificationManager.notify(0, builder.build()) | ||
} | ||
} |
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,46 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:padding="10dp" | ||
tools:ignore="UselessParent"> | ||
|
||
<ImageView | ||
android:id="@+id/notification_logo" | ||
android:layout_width="70dp" | ||
android:layout_height="70dp" | ||
android:layout_marginLeft="10dp" | ||
android:layout_marginTop="10dp" | ||
android:src="@drawable/app_image"/> | ||
|
||
<TextView | ||
android:id="@+id/notification_title" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Title" | ||
android:textSize="20sp" | ||
android:textStyle="bold" | ||
android:layout_toRightOf="@id/notification_logo" | ||
android:layout_marginLeft="10dp" | ||
android:layout_marginTop="15dp"/> | ||
|
||
<TextView | ||
android:id="@+id/notification_description" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="This is description" | ||
android:textSize="20sp" | ||
android:textStyle="normal" | ||
android:layout_toRightOf="@id/notification_logo" | ||
android:layout_below="@+id/notification_title" | ||
android:layout_marginLeft="10dp" /> | ||
|
||
</RelativeLayout> | ||
|
||
|
||
</RelativeLayout> |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@drawable/ic_launcher_background" /> | ||
<foreground android:drawable="@drawable/ic_launcher_foreground" /> | ||
<background android:drawable="@color/ic_launcher_background"/> | ||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/> | ||
</adaptive-icon> |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@drawable/ic_launcher_background" /> | ||
<foreground android:drawable="@drawable/ic_launcher_foreground" /> | ||
<background android:drawable="@color/ic_launcher_background"/> | ||
<foreground android:drawable="@mipmap/ic_launcher_foreground"/> | ||
</adaptive-icon> |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="ic_launcher_background">#A7D7B8</color> | ||
</resources> |
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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
// Top-level build file where you can add configuration options common to all sub-projects/modules. | ||
buildscript { | ||
dependencies { | ||
classpath 'com.google.gms:google-services:4.3.15' | ||
} | ||
} | ||
plugins { | ||
id 'com.android.application' version '7.4.1' apply false | ||
id 'com.android.library' version '7.4.1' apply false | ||
id 'org.jetbrains.kotlin.android' version '1.7.21' apply false | ||
id 'com.google.gms.google-services' version '4.3.15' apply false // Firebase cloud messaging | ||
} |