Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
AloqaIM-Android
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
AloqaIM-Android
Commits
1c73d226
Commit
1c73d226
authored
Oct 17, 2017
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Separate code in funs
parent
75ba17fa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
91 additions
and
62 deletions
+91
-62
PushManager.kt
app/src/main/java/chat/rocket/android/push/PushManager.kt
+91
-62
No files found.
app/src/main/java/chat/rocket/android/push/PushManager.kt
View file @
1c73d226
...
@@ -9,6 +9,7 @@ import android.content.Context
...
@@ -9,6 +9,7 @@ import android.content.Context
import
android.content.Intent
import
android.content.Intent
import
android.os.Build
import
android.os.Build
import
android.os.Bundle
import
android.os.Bundle
import
android.support.annotation.RequiresApi
import
android.support.v4.app.NotificationCompat
import
android.support.v4.app.NotificationCompat
import
android.support.v4.app.NotificationManagerCompat
import
android.support.v4.app.NotificationManagerCompat
import
android.text.Html
import
android.text.Html
...
@@ -21,16 +22,22 @@ object PushManager {
...
@@ -21,16 +22,22 @@ object PushManager {
val
messageStack
=
SparseArray
<
ArrayList
<
String
>>()
val
messageStack
=
SparseArray
<
ArrayList
<
String
>>()
fun
handle
(
context
:
Context
,
data
:
Bundle
)
{
fun
handle
(
context
:
Context
,
data
:
Bundle
)
{
val
now
=
System
.
currentTimeMillis
()
val
appContext
=
context
.
applicationContext
val
appContext
=
context
.
applicationContext
val
message
=
data
[
"message"
]
as
String
val
image
=
data
[
"image"
]
as
String
val
ejson
=
data
[
"ejson"
]
as
String
val
notificationId
=
data
[
"notId"
]
as
String
val
notificationId
=
data
[
"notId"
]
as
String
val
style
=
data
[
"style"
]
as
String
val
style
=
data
[
"style"
]
as
String
val
summaryText
=
data
[
"summaryText"
]
as
String
val
summaryText
=
data
[
"summaryText"
]
as
String
val
count
=
data
[
"count"
]
as
String
val
count
=
data
[
"count"
]
as
String
val
pushMessage
=
PushMessage
(
data
[
"title"
]
as
String
,
val
pushMessage
=
PushMessage
(
data
[
"title"
]
as
String
,
data
[
"message"
]
as
String
,
message
,
data
[
"image"
]
as
String
,
image
,
data
[
"ejson"
]
as
String
)
ejson
,
count
,
notificationId
,
summaryText
,
style
)
val
res
=
appContext
.
resources
val
res
=
appContext
.
resources
...
@@ -38,72 +45,85 @@ object PushManager {
...
@@ -38,72 +45,85 @@ object PushManager {
stackMessage
(
notificationId
.
toInt
(),
pushMessage
.
message
)
stackMessage
(
notificationId
.
toInt
(),
pushMessage
.
message
)
val
notificationManager
:
NotificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
val
notification
:
Notification
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
val
notificationManager
:
NotificationManager
=
notification
=
createNotificationForOreoAndAbove
(
appContext
,
pushMessage
,
smallIcon
)
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
notificationManager
.
notify
(
notificationId
.
toInt
(),
notification
)
with
(
pushMessage
)
{
val
channel
=
NotificationChannel
(
notificationId
,
sender
.
username
,
NotificationManager
.
IMPORTANCE_HIGH
)
val
notification
=
Notification
.
Builder
(
appContext
,
pushMessage
.
rid
)
.
setAutoCancel
(
true
)
.
setShowWhen
(
true
)
.
setWhen
(
now
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
.
build
()
channel
.
enableLights
(
true
)
channel
.
enableVibration
(
true
)
notificationManager
.
createNotificationChannel
(
channel
)
notificationManager
.
notify
(
notificationId
.
toInt
(),
notification
)
}
}
else
{
}
else
{
with
(
pushMessage
)
{
notification
=
createCompatNotification
(
appContext
,
pushMessage
,
smallIcon
)
val
notificationBuilder
=
NotificationCompat
.
Builder
(
appContext
)
NotificationManagerCompat
.
from
(
appContext
).
notify
(
notificationId
.
toInt
(),
notification
)
.
setAutoCancel
(
true
)
}
.
setShowWhen
(
true
)
}
.
setDefaults
(
Notification
.
DEFAULT_ALL
)
.
setWhen
(
now
)
fun
clearStack
(
notificationId
:
Int
)
{
.
setContentTitle
(
title
.
fromHtml
())
messageStack
.
delete
(
notificationId
)
.
setContentText
(
message
.
fromHtml
())
}
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
private
fun
createCompatNotification
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
):
Notification
{
.
setDeleteIntent
(
getDismissIntent
(
appContext
,
notificationId
.
toInt
()))
with
(
pushMessage
)
{
val
notificationBuilder
=
NotificationCompat
.
Builder
(
context
)
if
(
"inbox"
==
style
)
{
.
setAutoCancel
(
true
)
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
.
setShowWhen
(
true
)
val
messageCount
=
messages
.
size
.
setDefaults
(
Notification
.
DEFAULT_ALL
)
if
(
messageCount
>
1
)
{
.
setWhen
(
createdAt
)
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
.
setContentTitle
(
title
.
fromHtml
())
.
fromHtml
()
.
setContentText
(
message
.
fromHtml
())
val
inbox
=
NotificationCompat
.
InboxStyle
()
.
setNumber
(
count
.
toInt
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setSmallIcon
(
smallIcon
)
.
setSummaryText
(
summary
)
.
setDeleteIntent
(
getDismissIntent
(
context
,
notificationId
.
toInt
()))
messages
.
forEach
{
msg
->
inbox
.
addLine
(
msg
.
fromHtml
())
if
(
"inbox"
==
style
)
{
}
val
messages
=
chat
.
rocket
.
android
.
push
.
PushManager
.
messageStack
.
get
(
notificationId
.
toInt
())
notificationBuilder
.
setStyle
(
inbox
)
val
messageCount
=
messages
.
size
}
else
{
if
(
messageCount
>
1
)
{
val
bigText
=
NotificationCompat
.
BigTextStyle
()
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
.
bigText
(
message
.
fromHtml
())
.
fromHtml
()
.
setBigContentTitle
(
title
.
fromHtml
())
val
inbox
=
android
.
support
.
v4
.
app
.
NotificationCompat
.
InboxStyle
()
notificationBuilder
.
setStyle
(
bigText
)
.
setBigContentTitle
(
title
.
fromHtml
())
.
setSummaryText
(
summary
)
messages
.
forEach
{
msg
->
inbox
.
addLine
(
msg
.
fromHtml
())
}
}
notificationBuilder
.
setStyle
(
inbox
)
}
else
{
}
else
{
notificationBuilder
.
setContentText
(
message
.
fromHtml
())
val
bigText
=
android
.
support
.
v4
.
app
.
NotificationCompat
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notificationBuilder
.
setStyle
(
bigText
)
}
}
}
else
{
val
notification
=
notificationBuilder
.
build
()
notificationBuilder
.
setContentText
(
message
.
fromHtml
())
NotificationManagerCompat
.
from
(
appContext
).
notify
(
notificationId
.
toInt
(),
notification
)
}
}
return
notificationBuilder
.
build
()
}
}
}
}
fun
clearStack
(
notificationId
:
Int
)
{
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
messageStack
.
delete
(
notificationId
)
private
fun
createNotificationForOreoAndAbove
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
):
Notification
{
val
notificationManager
:
NotificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
with
(
pushMessage
)
{
val
channel
=
NotificationChannel
(
notificationId
,
sender
.
username
,
NotificationManager
.
IMPORTANCE_HIGH
)
val
notification
=
Notification
.
Builder
(
context
,
pushMessage
.
rid
)
.
setAutoCancel
(
true
)
.
setShowWhen
(
true
)
.
setWhen
(
createdAt
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
.
build
()
channel
.
enableLights
(
true
)
channel
.
enableVibration
(
true
)
notificationManager
.
createNotificationChannel
(
channel
)
return
notification
}
}
}
private
fun
stackMessage
(
id
:
Int
,
message
:
String
)
{
private
fun
stackMessage
(
id
:
Int
,
message
:
String
)
{
...
@@ -124,12 +144,20 @@ object PushManager {
...
@@ -124,12 +144,20 @@ object PushManager {
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
}
}
data class
PushMessage
(
val
title
:
String
,
val
message
:
String
,
val
image
:
String
?,
val
ejson
:
String
)
{
data class
PushMessage
(
val
title
:
String
,
val
message
:
String
,
val
image
:
String
?,
val
ejson
:
String
,
val
count
:
String
,
val
notificationId
:
String
,
val
summaryText
:
String
,
val
style
:
String
)
{
val
host
:
String
val
host
:
String
val
rid
:
String
val
rid
:
String
val
type
:
String
val
type
:
String
val
name
:
String
?
val
name
:
String
?
val
sender
:
Sender
val
sender
:
Sender
val
createdAt
:
Long
init
{
init
{
val
json
=
JSONObject
(
ejson
)
val
json
=
JSONObject
(
ejson
)
...
@@ -138,6 +166,7 @@ object PushManager {
...
@@ -138,6 +166,7 @@ object PushManager {
type
=
json
.
getString
(
"type"
)
type
=
json
.
getString
(
"type"
)
name
=
json
.
optString
(
"name"
)
name
=
json
.
optString
(
"name"
)
sender
=
Sender
(
json
.
getString
(
"sender"
))
sender
=
Sender
(
json
.
getString
(
"sender"
))
createdAt
=
System
.
currentTimeMillis
()
}
}
data class
Sender
(
val
sender
:
String
)
{
data class
Sender
(
val
sender
:
String
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment