Skip to content

Commit c032cd8

Browse files
committed
android code refactor to kotlin
1 parent 1bc08ac commit c032cd8

File tree

11 files changed

+89
-73
lines changed

11 files changed

+89
-73
lines changed

AndroidFirebaseNotification/app/google-services.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
{
22
"project_info": {
3-
"project_number": "246455310098",
3+
"project_number": "2466098",
44
"firebase_url": "https://android-firebase-notific-f1589.firebaseio.com",
55
"project_id": "android-firebase-notific-f1589",
66
"storage_bucket": "android-firebase-notific-f1589.appspot.com"
77
},
88
"client": [
99
{
1010
"client_info": {
11-
"mobilesdk_app_id": "1:246455310098:android:638fe2233cd537dd",
11+
"mobilesdk_app_id": "1:2464df098:android:638fe22dfdf3cd537dd",
1212
"android_client_info": {
1313
"package_name": "com.hellohasan.android_firebase_notification"
1414
}
1515
},
1616
"oauth_client": [
1717
{
18-
"client_id": "246455310098-9kiqefelhe6a85ftg9cq106r1m49nbfd.apps.googleusercontent.com",
18+
"client_id": "24df55310098-9kiqefelhe6a85ftg9cq106r1m49nbfd.apps.googleusercontent.com",
1919
"client_type": 3
2020
}
2121
],
2222
"api_key": [
2323
{
24-
"current_key": "AIzaSyARBxRx0dE4V9EIz7dbRB30LzyBHRhgwM4"
24+
"current_key": "YOUR_OWN_CURRENT_KEY"
2525
}
2626
],
2727
"services": {
2828
"appinvite_service": {
2929
"other_platform_oauth_client": [
3030
{
31-
"client_id": "246455310098-9kiqefelhe6a85ftg9cq106r1m49nbfd.apps.googleusercontent.com",
31+
"client_id": "2466546550098-9kiqefelhe6a85ftg9cq106r1m49nbfd.apps.googleusercontent.com",
3232
"client_type": 3
3333
}
3434
]

AndroidFirebaseNotification/app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
android:supportsRtl="true"
1212
android:theme="@style/AppTheme"
1313
tools:ignore="GoogleAppIndexingWarning">
14-
<activity android:name=".activity.MessageShowActivity"></activity>
14+
1515
<activity android:name=".activity.MainActivity">
1616
<intent-filter>
1717
<action android:name="android.intent.action.MAIN" />
@@ -20,11 +20,15 @@
2020
</intent-filter>
2121
</activity>
2222

23+
<activity android:name=".activity.MessageShowActivity"
24+
android:parentActivityName=".activity.MainActivity"/>
25+
2326
<service android:name=".notification.MyFirebaseMessagingService">
2427
<intent-filter>
2528
<action android:name="com.google.firebase.MESSAGING_EVENT" />
2629
</intent-filter>
2730
</service>
31+
2832
</application>
2933

3034
</manifest>

AndroidFirebaseNotification/app/src/main/java/com/hellohasan/android_firebase_notification/activity/MainActivity.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,32 @@ package com.hellohasan.android_firebase_notification.activity
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.util.Log
56
import com.google.firebase.iid.FirebaseInstanceId
7+
import com.google.firebase.messaging.FirebaseMessaging
68
import com.hellohasan.android_firebase_notification.R
9+
import com.hellohasan.android_firebase_notification.notification.Configuration.Companion.TOPIC_GLOBAL
710
import kotlinx.android.synthetic.main.activity_main.*
811

912
class MainActivity : AppCompatActivity() {
1013

14+
private val TAG = "MainActivity"
15+
1116
override fun onCreate(savedInstanceState: Bundle?) {
1217
super.onCreate(savedInstanceState)
1318
setContentView(R.layout.activity_main)
1419

1520
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
1621
firebaseId.append(it.token)
1722
}
23+
24+
FirebaseMessaging.getInstance().subscribeToTopic(TOPIC_GLOBAL)
25+
.addOnCompleteListener { task ->
26+
27+
if (task.isSuccessful)
28+
Log.d(TAG, "Global topic subscription successful")
29+
else
30+
Log.e(TAG, "Global topic subscription failed. Error: " + task.exception?.localizedMessage)
31+
}
1832
}
1933
}

AndroidFirebaseNotification/app/src/main/java/com/hellohasan/android_firebase_notification/activity/MessageShowActivity.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.hellohasan.android_firebase_notification.activity
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.view.MenuItem
6+
import androidx.core.app.NavUtils
57

68
import com.hellohasan.android_firebase_notification.R
79
import com.squareup.picasso.Picasso
@@ -13,6 +15,7 @@ class MessageShowActivity : AppCompatActivity() {
1315
override fun onCreate(savedInstanceState: Bundle?) {
1416
super.onCreate(savedInstanceState)
1517
setContentView(R.layout.activity_message_show)
18+
supportActionBar?.setDisplayHomeAsUpEnabled(true)
1619

1720
//receive data from MyFirebaseMessagingService class
1821
val title = intent.getStringExtra("title")
@@ -21,13 +24,29 @@ class MessageShowActivity : AppCompatActivity() {
2124
val imageUrl = intent.getStringExtra("image")
2225

2326
//Set data on UI
27+
Picasso.get()
28+
.load(imageUrl)
29+
.placeholder(R.drawable.image_placeholder)
30+
.error(R.drawable.image_placeholder)
31+
.into(featureGraphics)
32+
2433
header.text = title
2534
timeStamp.text = timeStampString
2635
article.text = articleString
36+
}
2737

28-
Picasso.get()
29-
.load(imageUrl)
30-
.error(R.drawable.default_image)
31-
.into(featureGraphics)
38+
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
39+
40+
if (item?.itemId == android.R.id.home)
41+
onBackPressed()
42+
43+
return super.onOptionsItemSelected(item)
44+
}
45+
46+
override fun onBackPressed() {
47+
super.onBackPressed()
48+
49+
val intent = NavUtils.getParentActivityIntent(this)
50+
startActivity(intent)
3251
}
3352
}

AndroidFirebaseNotification/app/src/main/java/com/hellohasan/android_firebase_notification/notification/MyFirebaseMessagingService.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
4343
private fun handleDataMessage(json: JSONObject) {
4444
Log.e(TAG, "push json: $json")
4545

46-
println("Data Message: $json")
47-
4846
try {
4947
val data = json.getJSONObject("data")
5048

5149
val title = data.getString("title")
5250
val message = data.getString("message")
53-
val isBackground = data.getBoolean("is_background")
5451
val imageUrl = data.getString("image")
5552
val timestamp = data.getString("timestamp")
5653
val payload = data.getJSONObject("payload")

AndroidFirebaseNotification/app/src/main/java/com/hellohasan/android_firebase_notification/notification/NotificationUtils.kt

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.hellohasan.android_firebase_notification.notification
22

3-
import android.app.ActivityManager
4-
import android.app.Notification
5-
import android.app.NotificationManager
6-
import android.app.PendingIntent
3+
import android.app.*
74
import android.content.ComponentName
85
import android.content.ContentResolver
96
import android.content.Context
@@ -36,14 +33,16 @@ import com.hellohasan.android_firebase_notification.notification.Configuration.C
3633

3734
class NotificationUtils(private val mContext: Context) {
3835

36+
private val channelId = mContext.getString(R.string.default_notification_channel_id)
37+
3938
@JvmOverloads
4039
fun showNotificationMessage(
4140
title: String,
4241
message: String,
4342
timeStamp: String,
4443
intent: Intent,
45-
imageUrl: String? = null
46-
) {
44+
imageUrl: String? = null) {
45+
4746
// Check for empty push message
4847
if (TextUtils.isEmpty(message))
4948
return
@@ -61,7 +60,8 @@ class NotificationUtils(private val mContext: Context) {
6160
)
6261

6362
val mBuilder = NotificationCompat.Builder(
64-
mContext
63+
mContext,
64+
channelId
6565
)
6666

6767
val alarmSound = Uri.parse(
@@ -140,8 +140,16 @@ class NotificationUtils(private val mContext: Context) {
140140
.setContentText(message)
141141
.build()
142142

143-
val notificationManager =
144-
mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
143+
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
144+
145+
// Since android Oreo notification channel is needed.
146+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
147+
val channel = NotificationChannel(channelId,
148+
"Firebase Notification channel for sample app",
149+
NotificationManager.IMPORTANCE_DEFAULT)
150+
notificationManager.createNotificationChannel(channel)
151+
}
152+
145153
notificationManager.notify(Configuration.NOTIFICATION_ID, notification)
146154
}
147155

@@ -172,8 +180,14 @@ class NotificationUtils(private val mContext: Context) {
172180
.setContentText(message)
173181
.build()
174182

175-
val notificationManager =
176-
mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
183+
val notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
184+
185+
// Since android Oreo notification channel is needed.
186+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
187+
val channel = NotificationChannel(channelId, "Firebase Notification channel for sample app", NotificationManager.IMPORTANCE_DEFAULT)
188+
notificationManager.createNotificationChannel(channel)
189+
}
190+
177191
notificationManager.notify(Configuration.NOTIFICATION_ID_BIG_IMAGE, notification)
178192
}
179193

@@ -213,41 +227,6 @@ class NotificationUtils(private val mContext: Context) {
213227

214228
companion object {
215229

216-
/**
217-
* Method checks if the app is in background or not
218-
*/
219-
fun isAppIsInBackground(context: Context): Boolean {
220-
var isInBackground = true
221-
val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
222-
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
223-
val runningProcesses = am.runningAppProcesses
224-
for (processInfo in runningProcesses) {
225-
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
226-
for (activeProcess in processInfo.pkgList) {
227-
if (activeProcess == context.packageName) {
228-
isInBackground = false
229-
}
230-
}
231-
}
232-
}
233-
} else {
234-
val taskInfo = am.getRunningTasks(1)
235-
val componentInfo = taskInfo[0].topActivity
236-
if (componentInfo.packageName == context.packageName) {
237-
isInBackground = false
238-
}
239-
}
240-
241-
return isInBackground
242-
}
243-
244-
// Clears notification tray messages
245-
fun clearNotifications(context: Context) {
246-
val notificationManager =
247-
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
248-
notificationManager.cancelAll()
249-
}
250-
251230
fun getTimeMilliSec(timeStamp: String): Long {
252231
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
253232
try {
Binary file not shown.
1.41 KB
Loading

AndroidFirebaseNotification/app/src/main/res/layout/activity_message_show.xml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,49 @@
66
android:layout_height="match_parent"
77
tools:context=".activity.MessageShowActivity">
88

9-
<RelativeLayout
9+
<androidx.constraintlayout.widget.ConstraintLayout
1010
android:layout_width="match_parent"
11-
android:layout_height="match_parent">
11+
android:layout_height="wrap_content">
1212

1313
<ImageView
1414
android:id="@+id/featureGraphics"
1515
android:layout_width="match_parent"
16-
android:layout_height="200dp"
16+
android:layout_height="220dp"
1717
android:scaleType="centerCrop"
18-
tools:src="@drawable/default_image" />
18+
app:layout_constraintTop_toTopOf="parent"
19+
android:src="@drawable/image_placeholder"
20+
tools:ignore="ContentDescription" />
1921

2022
<TextView
2123
android:id="@+id/header"
2224
android:layout_width="match_parent"
2325
android:layout_height="wrap_content"
24-
android:layout_below="@+id/featureGraphics"
26+
android:layout_marginStart="16dp"
2527
android:layout_marginTop="24dp"
26-
android:paddingLeft="16dp"
27-
android:paddingRight="16dp"
2828
android:textSize="21sp"
2929
android:textStyle="bold"
30+
app:layout_constraintTop_toBottomOf="@id/featureGraphics"
3031
tools:text="This is Headline" />
3132

3233
<TextView
3334
android:id="@+id/timeStamp"
3435
android:layout_width="wrap_content"
3536
android:layout_height="wrap_content"
36-
android:layout_below="@+id/header"
37-
android:paddingLeft="16dp"
37+
android:layout_marginTop="4dp"
3838
android:textSize="14sp"
39+
app:layout_constraintStart_toStartOf="@id/header"
40+
app:layout_constraintTop_toBottomOf="@+id/header"
3941
tools:text="12-12-2017 21:45" />
4042

41-
4243
<TextView
4344
android:id="@+id/article"
4445
android:layout_width="match_parent"
4546
android:layout_height="wrap_content"
46-
android:layout_below="@+id/timeStamp"
4747
android:padding="16dp"
4848
android:textSize="14sp"
49-
tools:text="This is article. Very large string value" />
49+
app:layout_constraintTop_toBottomOf="@+id/timeStamp"
50+
tools:text="@string/dummy_paragraph" />
5051

51-
</RelativeLayout>
52+
</androidx.constraintlayout.widget.ConstraintLayout>
5253

5354
</ScrollView>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
<resources>
22
<string name="app_name">Android Firebase Notification</string>
3+
<string name="default_notification_channel_id">notification_id_101</string>
4+
<string name="dummy_paragraph">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</string>
35
</resources>

0 commit comments

Comments
 (0)