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
59e7f90f
Commit
59e7f90f
authored
Oct 22, 2017
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change how notifications are being built on Android M and lower
parent
89466fdf
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
165 deletions
+192
-165
AbstractAuthedActivity.java
.../chat/rocket/android/activity/AbstractAuthedActivity.java
+2
-1
PushManager.kt
app/src/main/java/chat/rocket/android/push/PushManager.kt
+186
-158
GCMIntentService.java
...n/java/chat/rocket/android/push/gcm/GCMIntentService.java
+4
-6
No files found.
app/src/main/java/chat/rocket/android/activity/AbstractAuthedActivity.java
View file @
59e7f90f
...
@@ -69,6 +69,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...
@@ -69,6 +69,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
rocketChatCache
.
setSelectedRoomId
(
intent
.
getStringExtra
(
PushConstants
.
ROOM_ID
));
rocketChatCache
.
setSelectedRoomId
(
intent
.
getStringExtra
(
PushConstants
.
ROOM_ID
));
}
}
}
}
PushManager
.
INSTANCE
.
clearHostNotifications
(
hostname
);
}
else
{
}
else
{
updateHostnameIfNeeded
(
rocketChatCache
.
getSelectedServerHostname
());
updateHostnameIfNeeded
(
rocketChatCache
.
getSelectedServerHostname
());
}
}
...
@@ -76,7 +77,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...
@@ -76,7 +77,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
if
(
intent
.
hasExtra
(
PushConstants
.
NOT_ID
))
{
if
(
intent
.
hasExtra
(
PushConstants
.
NOT_ID
))
{
isNotification
=
true
;
isNotification
=
true
;
int
notificationId
=
intent
.
getIntExtra
(
PushConstants
.
NOT_ID
,
0
);
int
notificationId
=
intent
.
getIntExtra
(
PushConstants
.
NOT_ID
,
0
);
PushManager
.
INSTANCE
.
clear
MessageBundle
(
notificationId
);
PushManager
.
INSTANCE
.
clear
NotificationIdStack
(
notificationId
);
}
}
}
}
...
...
app/src/main/java/chat/rocket/android/push/PushManager.kt
View file @
59e7f90f
...
@@ -45,12 +45,17 @@ typealias TupleGroupIdMessageCount = Pair<Int, AtomicInteger>
...
@@ -45,12 +45,17 @@ typealias TupleGroupIdMessageCount = Pair<Int, AtomicInteger>
object
PushManager
{
object
PushManager
{
const
val
REPLY_LABEL
=
"REPLY"
const
val
REPLY_LABEL
=
"REPLY"
const
val
REMOTE_INPUT_REPLY
=
"REMOTE_INPUT_REPLY"
const
val
REMOTE_INPUT_REPLY
=
"REMOTE_INPUT_REPLY"
// Map associating a notification id to a list of corresponding messages ie. an id corresponds
// Map associating a notification id to a list of corresponding messages ie. an id corresponds
// to a user and the corresponding list is all the messages sent by him.
// to a user and the corresponding list is all the messages sent by him.
private
val
messageStack
=
SparseArray
<
ArrayList
<
CharSequence
>>()
private
val
messageStack
=
SparseArray
<
MutableList
<
CharSequence
>>()
// Notifications received from the same server are grouped in a single bundled notification.
// Notifications received from the same server are grouped in a single bundled notification.
// This map associates a host to a group id.
// This map associates a host to a group id.
private
val
groupMap
=
HashMap
<
String
,
TupleGroupIdMessageCount
>()
private
val
groupMap
=
HashMap
<
String
,
TupleGroupIdMessageCount
>()
// Map a hostname to a list of push messages that pertain to it.
private
val
hostToPushMessageList
=
HashMap
<
String
,
MutableList
<
PushMessage
>>()
private
val
randomizer
=
Random
()
private
val
randomizer
=
Random
()
/**
/**
...
@@ -68,237 +73,256 @@ object PushManager {
...
@@ -68,237 +73,256 @@ object PushManager {
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
title
=
data
[
"title"
]
as
String
val
title
=
data
[
"title"
]
as
String
val
p
ushMessage
=
PushMessage
(
title
,
message
,
image
,
ejson
,
count
,
notId
,
summaryText
,
style
)
val
lastP
ushMessage
=
PushMessage
(
title
,
message
,
image
,
ejson
,
count
,
notId
,
summaryText
,
style
)
// We should use Timber here
// We should use Timber here
if
(
BuildConfig
.
DEBUG
)
{
if
(
BuildConfig
.
DEBUG
)
{
Log
.
d
(
PushMessage
::
class
.
java
.
simpleName
,
p
ushMessage
.
toString
())
Log
.
d
(
PushMessage
::
class
.
java
.
simpleName
,
lastP
ushMessage
.
toString
())
}
}
bundleMessage
(
notId
.
toInt
(),
p
ushMessage
.
message
)
bundleMessage
(
notId
.
toInt
(),
lastP
ushMessage
.
message
)
showNotification
(
appContext
,
p
ushMessage
)
showNotification
(
appContext
,
lastP
ushMessage
)
}
}
/**
/**
* Clear all messages corresponding to a specific notification id (aka specific user)
* Clear all messages corresponding to a specific notification id (aka specific user)
*/
*/
fun
clear
MessageBundle
(
notificationId
:
Int
)
{
fun
clear
NotificationIdStack
(
notificationId
:
Int
)
{
messageStack
.
delete
(
notificationId
)
messageStack
.
delete
(
notificationId
)
}
}
private
fun
showNotification
(
context
:
Context
,
pushMessage
:
PushMessage
)
{
fun
clearHostNotifications
(
host
:
String
)
{
val
notificationManager
:
NotificationManager
=
hostToPushMessageList
.
remove
(
host
)
}
private
fun
showNotification
(
context
:
Context
,
lastPushMessage
:
PushMessage
)
{
val
manager
:
NotificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
val
notId
=
pushMessage
.
notificationId
.
toInt
()
val
notId
=
lastPushMessage
.
notificationId
.
toInt
()
val
host
=
lastPushMessage
.
host
val
groupTuple
=
getGroupForHost
(
host
)
val
groupTuple
=
groupMap
[
pushMessage
.
host
]
val
notification
:
Notification
if
(
isAndroidVersionAtLeast
(
Build
.
VERSION_CODES
.
O
))
{
notification
=
createNotificationForOreoAndAbove
(
context
,
pushMessage
)
groupTuple
?.
let
{
val
groupNotification
=
createOreoGroupNotification
(
context
,
pushMessage
)
notificationManager
.
notify
(
groupTuple
.
first
,
groupNotification
)
groupTuple
.
second
.
incrementAndGet
()
groupTuple
.
second
.
incrementAndGet
()
}
if
(
isAndroidVersionAtLeast
(
Build
.
VERSION_CODES
.
O
))
{
notificationManager
.
notify
(
notId
,
notification
)
val
notification
=
createSingleNotificationForOreo
(
context
,
lastPushMessage
)
val
groupNotification
=
createGroupNotificationForOreo
(
context
,
lastPushMessage
)
manager
.
notify
(
notId
,
notification
)
manager
.
notify
(
groupTuple
.
first
,
groupNotification
)
}
else
{
}
else
{
notification
=
createCompatNotification
(
context
,
pushMessage
)
val
notIdListForHostname
:
MutableList
<
PushMessage
>?
=
hostToPushMessageList
.
get
(
host
)
groupTuple
?.
let
{
if
(
notIdListForHostname
==
null
)
{
val
groupNotification
=
createCompatGroupNotification
(
context
,
pushMessage
)
hostToPushMessageList
.
put
(
host
,
arrayListOf
(
lastPushMessage
)
)
NotificationManagerCompat
.
from
(
context
).
notify
(
groupTuple
.
first
,
groupNotification
)
}
else
{
groupTuple
.
second
.
incrementAndGet
(
)
notIdListForHostname
.
add
(
lastPushMessage
)
}
}
val
notification
=
createSingleNotification
(
context
,
lastPushMessage
)
val
groupNotification
=
createGroupNotification
(
context
,
lastPushMessage
)
NotificationManagerCompat
.
from
(
context
).
notify
(
notId
,
notification
)
NotificationManagerCompat
.
from
(
context
).
notify
(
notId
,
notification
)
NotificationManagerCompat
.
from
(
context
).
notify
(
groupTuple
.
first
,
groupNotification
)
}
}
}
}
private
fun
isAndroidVersionAtLeast
(
minVersion
:
Int
)
=
Build
.
VERSION
.
SDK_INT
>=
minVersion
private
fun
isAndroidVersionAtLeast
(
minVersion
:
Int
)
=
Build
.
VERSION
.
SDK_INT
>=
minVersion
private
fun
bundleNotificationsToHost
(
host
:
String
)
{
private
fun
getGroupForHost
(
host
:
String
):
TupleGroupIdMessageCount
{
val
size
=
groupMap
.
size
val
size
=
groupMap
.
size
groupMap
.
get
(
host
)
?.
let
{
var
group
=
groupMap
.
get
(
host
)
groupMap
.
put
(
host
,
TupleGroupIdMessageCount
(
size
+
1
,
AtomicInteger
(
0
)))
if
(
group
==
null
)
{
}
group
=
TupleGroupIdMessageCount
(
size
+
1
,
AtomicInteger
(
0
))
}
groupMap
.
put
(
host
,
group
)
}
private
fun
createCompatGroupNotification
(
context
:
Context
,
pushMessage
:
PushMessage
):
Notification
{
return
group
// Create notification group.
}
bundleNotificationsToHost
(
pushMessage
.
host
)
val
id
=
pushMessage
.
notificationId
.
toInt
()
private
fun
createGroupNotification
(
context
:
Context
,
lastPushMessage
:
PushMessage
):
Notification
{
val
contentIntent
=
getContentIntent
(
context
,
id
,
pushMessage
,
group
=
true
)
with
(
lastPushMessage
)
{
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
id
=
lastPushMessage
.
notificationId
.
toInt
()
val
notGroupBuilder
=
NotificationCompat
.
Builder
(
context
)
val
contentIntent
=
getContentIntent
(
context
,
id
,
lastPushMessage
,
singleConversation
=
true
)
.
setWhen
(
pushMessage
.
createdAt
)
val
deleteIntent
=
getDismissIntent
(
context
,
lastPushMessage
)
.
setContentTitle
(
pushMessage
.
title
.
fromHtml
())
val
builder
=
NotificationCompat
.
Builder
(
context
)
.
setContentText
(
pushMessage
.
message
.
fromHtml
())
.
setWhen
(
createdAt
)
.
setGroup
(
pushMessage
.
host
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setGroup
(
host
)
.
setGroupSummary
(
true
)
.
setGroupSummary
(
true
)
.
setStyle
(
NotificationCompat
.
BigTextStyle
().
bigText
(
pushMessage
.
message
.
fromHtml
()))
.
setContentIntent
(
contentIntent
)
.
setContentIntent
(
contentIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setMessageNotification
()
.
setMessageNotification
()
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
pushMessage
.
host
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
host
)
if
(
subText
.
isNotEmpty
())
{
if
(
subText
.
isNotEmpty
())
{
notGroupB
uilder
.
setSubText
(
subText
)
b
uilder
.
setSubText
(
subText
)
}
}
val
messages
=
messageStack
.
get
(
pushMessage
.
notificationId
.
toInt
())
if
(
style
==
"inbox"
)
{
val
messageCount
=
messages
.
size
val
pushMessageList
=
hostToPushMessageList
.
get
(
host
)
pushMessageList
?.
let
{
val
messageCount
=
pushMessageList
.
size
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
.
fromHtml
()
builder
.
setNumber
(
messageCount
)
if
(
messageCount
>
1
)
{
if
(
messageCount
>
1
)
{
val
summary
=
pushMessage
.
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
val
firstPush
=
pushMessageList
[
0
]
val
singleConversation
=
pushMessageList
.
filter
{
firstPush
.
sender
.
username
!=
it
.
sender
.
username
}.
isEmpty
()
val
inbox
=
NotificationCompat
.
InboxStyle
()
val
inbox
=
NotificationCompat
.
InboxStyle
()
.
setBigContentTitle
(
pushMessage
.
title
.
fromHtml
())
.
setBigContentTitle
(
if
(
singleConversation
)
title
else
summary
)
.
setSummaryText
(
summary
)
notGroupBuilder
.
setStyle
(
inbox
)
for
(
push
in
pushMessageList
)
{
if
(
singleConversation
)
{
inbox
.
addLine
(
push
.
message
)
}
else
{
inbox
.
addLine
(
"<font color='black'>${push.title}</font> <font color='gray'>${push.message}</font>"
.
fromHtml
())
}
}
builder
.
setStyle
(
inbox
)
}
else
{
}
else
{
val
bigText
=
NotificationCompat
.
BigTextStyle
()
val
bigText
=
NotificationCompat
.
BigTextStyle
()
.
bigText
(
pushMessage
.
message
.
fromHtml
())
.
bigText
(
pushMessageList
[
0
].
message
.
fromHtml
())
.
setBigContentTitle
(
pushMessage
.
title
.
fromHtml
())
.
setBigContentTitle
(
pushMessageList
[
0
].
title
.
fromHtml
())
builder
.
setStyle
(
bigText
)
}
}
}
else
{
val
bigText
=
NotificationCompat
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notGroupB
uilder
.
setStyle
(
bigText
)
b
uilder
.
setStyle
(
bigText
)
}
}
return
notGroupBuilder
.
build
()
return
builder
.
build
()
}
}
}
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
private
fun
createOreoGroupNotification
(
context
:
Context
,
pushMessage
:
PushMessage
):
Notification
{
private
fun
createGroupNotificationForOreo
(
context
:
Context
,
lastPushMessage
:
PushMessage
):
Notification
{
// Create notification group.
with
(
lastPushMessage
)
{
bundleNotificationsToHost
(
pushMessage
.
host
)
val
manager
:
NotificationManager
=
val
id
=
pushMessage
.
notificationId
.
toInt
()
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
val
contentIntent
=
getContentIntent
(
context
,
id
,
pushMessage
,
group
=
true
)
val
id
=
notificationId
.
toInt
()
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
contentIntent
=
getContentIntent
(
context
,
id
,
lastPushMessage
,
singleConversation
=
true
)
val
notGroupBuilder
=
Notification
.
Builder
(
context
,
pushMessage
.
notificationId
)
val
deleteIntent
=
getDismissIntent
(
context
,
lastPushMessage
)
.
setWhen
(
pushMessage
.
createdAt
)
val
channelGroup
=
NotificationChannel
(
host
,
host
,
NotificationManager
.
IMPORTANCE_DEFAULT
)
.
setChannelId
(
pushMessage
.
notificationId
)
manager
.
createNotificationChannel
(
channelGroup
)
.
setContentTitle
(
pushMessage
.
title
.
fromHtml
())
val
builder
=
Notification
.
Builder
(
context
,
host
)
.
setContentText
(
pushMessage
.
message
.
fromHtml
())
.
setWhen
(
createdAt
)
.
setGroup
(
pushMessage
.
host
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setGroup
(
host
)
.
setGroupSummary
(
true
)
.
setGroupSummary
(
true
)
.
setStyle
(
Notification
.
BigTextStyle
().
bigText
(
pushMessage
.
message
.
fromHtml
()))
.
setNumber
(
count
.
toInt
())
.
setCategory
(
Notification
.
CATEGORY_MESSAGE
)
.
setContentIntent
(
contentIntent
)
.
setContentIntent
(
contentIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setMessageNotification
(
context
)
.
setMessageNotification
(
context
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
pushMessage
.
host
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
host
)
if
(
subText
.
isNotEmpty
())
{
if
(
subText
.
isNotEmpty
())
{
notGroupB
uilder
.
setSubText
(
subText
)
b
uilder
.
setSubText
(
subText
)
}
}
val
messages
=
messageStack
.
get
(
pushMessage
.
notificationId
.
toInt
())
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
val
messageCount
=
messages
.
size
val
messageCount
=
messages
.
size
if
(
messageCount
>
1
)
{
if
(
messageCount
>
1
)
{
val
summary
=
pushMessage
.
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
val
inbox
=
Notification
.
InboxStyle
()
val
inbox
=
Notification
.
InboxStyle
()
.
setBigContentTitle
(
pushMessage
.
title
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setSummaryText
(
summary
)
.
setSummaryText
(
summary
)
notGroupB
uilder
.
setStyle
(
inbox
)
b
uilder
.
setStyle
(
inbox
)
}
else
{
}
else
{
val
bigText
=
Notification
.
BigTextStyle
()
val
bigText
=
Notification
.
BigTextStyle
()
.
bigText
(
pushMessage
.
message
.
fromHtml
())
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
pushMessage
.
title
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notGroupB
uilder
.
setStyle
(
bigText
)
b
uilder
.
setStyle
(
bigText
)
}
}
return
notGroupBuilder
.
build
()
return
builder
.
build
()
}
}
}
private
fun
create
CompatNotification
(
context
:
Context
,
p
ushMessage
:
PushMessage
):
Notification
{
private
fun
create
SingleNotification
(
context
:
Context
,
lastP
ushMessage
:
PushMessage
):
Notification
{
with
(
p
ushMessage
)
{
with
(
lastP
ushMessage
)
{
val
id
=
notificationId
.
toInt
()
val
id
=
notificationId
.
toInt
()
val
contentIntent
=
getContentIntent
(
context
,
id
,
p
ushMessage
)
val
contentIntent
=
getContentIntent
(
context
,
id
,
lastP
ushMessage
)
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
deleteIntent
=
getDismissIntent
(
context
,
lastPushMessage
)
val
notificationB
uilder
=
NotificationCompat
.
Builder
(
context
)
val
b
uilder
=
NotificationCompat
.
Builder
(
context
)
.
setWhen
(
createdAt
)
.
setWhen
(
createdAt
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
set
Number
(
count
.
toInt
()
)
.
set
GroupSummary
(
false
)
.
setGroup
(
host
)
.
setGroup
(
host
)
.
setDeleteIntent
(
deleteIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setContentIntent
(
contentIntent
)
.
setContentIntent
(
contentIntent
)
.
setMessageNotification
()
.
setMessageNotification
()
.
addReplyAction
(
p
ushMessage
)
.
addReplyAction
(
lastP
ushMessage
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
p
ushMessage
.
host
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
lastP
ushMessage
.
host
)
if
(
subText
.
isNotEmpty
())
{
if
(
subText
.
isNotEmpty
())
{
notificationB
uilder
.
setSubText
(
subText
)
b
uilder
.
setSubText
(
subText
)
}
}
if
(
"inbox"
==
style
)
{
val
pushMessageList
=
hostToPushMessageList
.
get
(
host
)
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
val
messageCount
=
messages
.
size
if
(
messageCount
>
1
)
{
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
.
fromHtml
()
val
inbox
=
NotificationCompat
.
InboxStyle
()
.
setBigContentTitle
(
title
.
fromHtml
())
.
setSummaryText
(
summary
)
messages
.
forEach
{
msg
->
pushMessageList
?.
let
{
inbox
.
addLine
(
msg
.
fromHtml
())
if
(
pushMessageList
.
isNotEmpty
())
{
}
val
messageCount
=
pushMessageList
.
size
notificationBuilder
.
setStyle
(
inbox
)
}
else
{
val
bigText
=
NotificationCompat
.
BigTextStyle
()
val
bigText
=
NotificationCompat
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
bigText
(
pushMessageList
.
last
().
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setBigContentTitle
(
pushMessageList
.
last
().
title
.
fromHtml
())
notificationBuilder
.
setStyle
(
bigTex
t
)
builder
.
setStyle
(
bigText
).
setNumber
(
messageCoun
t
)
}
}
}
else
{
notificationBuilder
.
setContentText
(
message
.
fromHtml
())
}
}
return
notificationB
uilder
.
build
()
return
b
uilder
.
build
()
}
}
}
}
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
private
fun
create
NotificationForOreoAndAbove
(
context
:
Context
,
p
ushMessage
:
PushMessage
):
Notification
{
private
fun
create
SingleNotificationForOreo
(
context
:
Context
,
lastP
ushMessage
:
PushMessage
):
Notification
{
val
manager
:
NotificationManager
=
val
manager
:
NotificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
with
(
p
ushMessage
)
{
with
(
lastP
ushMessage
)
{
val
id
=
notificationId
.
toInt
()
val
id
=
notificationId
.
toInt
()
val
contentIntent
=
getContentIntent
(
context
,
id
,
p
ushMessage
)
val
contentIntent
=
getContentIntent
(
context
,
id
,
lastP
ushMessage
)
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
deleteIntent
=
getDismissIntent
(
context
,
lastPushMessage
)
val
channel
=
NotificationChannel
(
notificationId
,
sender
.
username
,
NotificationManager
.
IMPORTANCE_HIGH
)
val
channel
=
NotificationChannel
(
host
,
host
,
NotificationManager
.
IMPORTANCE_DEFAULT
)
channel
.
lockscreenVisibility
=
Notification
.
VISIBILITY_PUBLIC
channel
.
lockscreenVisibility
=
Notification
.
VISIBILITY_PUBLIC
channel
.
enableLights
(
true
)
channel
.
enableLights
(
true
)
channel
.
enableVibration
(
true
)
channel
.
enableVibration
(
true
)
channel
.
setShowBadge
(
true
)
channel
.
setShowBadge
(
true
)
manager
.
createNotificationChannel
(
channel
)
manager
.
createNotificationChannel
(
channel
)
val
notificationBuilder
=
Notification
.
Builder
(
context
,
notificationId
)
val
builder
=
Notification
.
Builder
(
context
,
host
)
.
setWhen
(
createdAt
)
.
setWhen
(
createdAt
)
.
setChannelId
(
notificationId
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setGroup
(
host
)
.
setGroup
(
host
)
.
setGroupSummary
(
false
)
.
setDeleteIntent
(
deleteIntent
)
.
setDeleteIntent
(
deleteIntent
)
.
setContentIntent
(
contentIntent
)
.
setContentIntent
(
contentIntent
)
.
setMessageNotification
(
context
)
.
setMessageNotification
(
context
)
.
addReplyAction
(
context
,
p
ushMessage
)
.
addReplyAction
(
context
,
lastP
ushMessage
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
p
ushMessage
.
host
)
val
subText
=
RocketChatCache
(
context
).
getHostSiteName
(
lastP
ushMessage
.
host
)
if
(
subText
.
isNotEmpty
())
{
if
(
subText
.
isNotEmpty
())
{
notificationB
uilder
.
setSubText
(
subText
)
b
uilder
.
setSubText
(
subText
)
}
}
channel
.
enableLights
(
true
)
channel
.
enableVibration
(
true
)
if
(
"inbox"
==
style
)
{
if
(
"inbox"
==
style
)
{
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
val
messageCount
=
messages
.
size
val
messageCount
=
messages
.
size
...
@@ -313,24 +337,24 @@ object PushManager {
...
@@ -313,24 +337,24 @@ object PushManager {
inbox
.
addLine
(
msg
.
fromHtml
())
inbox
.
addLine
(
msg
.
fromHtml
())
}
}
notificationB
uilder
.
setStyle
(
inbox
)
b
uilder
.
setStyle
(
inbox
)
}
else
{
}
else
{
val
bigText
=
Notification
.
BigTextStyle
()
val
bigText
=
Notification
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notificationB
uilder
.
setStyle
(
bigText
)
b
uilder
.
setStyle
(
bigText
)
}
}
}
else
{
}
else
{
notificationB
uilder
.
setContentText
(
message
.
fromHtml
())
b
uilder
.
setContentText
(
message
.
fromHtml
())
}
}
return
notificationB
uilder
.
build
()
return
b
uilder
.
build
()
}
}
}
}
private
fun
bundleMessage
(
id
:
Int
,
message
:
CharSequence
)
{
private
fun
bundleMessage
(
id
:
Int
,
message
:
CharSequence
)
{
val
existingStack
:
Array
List
<
CharSequence
>?
=
messageStack
[
id
]
val
existingStack
:
Mutable
List
<
CharSequence
>?
=
messageStack
[
id
]
if
(
existingStack
==
null
)
{
if
(
existingStack
==
null
)
{
val
newStack
=
arrayListOf
<
CharSequence
>()
val
newStack
=
arrayListOf
<
CharSequence
>()
...
@@ -341,18 +365,19 @@ object PushManager {
...
@@ -341,18 +365,19 @@ object PushManager {
}
}
}
}
private
fun
getDismissIntent
(
context
:
Context
,
notificationId
:
Int
):
PendingIntent
{
private
fun
getDismissIntent
(
context
:
Context
,
pushMessage
:
PushMessage
):
PendingIntent
{
val
deleteIntent
=
Intent
(
context
,
DeleteReceiver
::
class
.
java
)
val
deleteIntent
=
Intent
(
context
,
DeleteReceiver
::
class
.
java
)
deleteIntent
.
putExtra
(
"notId"
,
notificationId
)
deleteIntent
.
putExtra
(
"notId"
,
pushMessage
.
notificationId
.
toInt
())
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
deleteIntent
.
putExtra
(
"host"
,
pushMessage
.
host
)
return
PendingIntent
.
getBroadcast
(
context
,
pushMessage
.
notificationId
.
toInt
(),
deleteIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
)
}
}
private
fun
getContentIntent
(
context
:
Context
,
notificationId
:
Int
,
pushMessage
:
PushMessage
,
group
:
Boolean
=
fals
e
):
PendingIntent
{
private
fun
getContentIntent
(
context
:
Context
,
notificationId
:
Int
,
pushMessage
:
PushMessage
,
singleConversation
:
Boolean
=
tru
e
):
PendingIntent
{
val
notificationIntent
=
Intent
(
context
,
MainActivity
::
class
.
java
)
val
notificationIntent
=
Intent
(
context
,
MainActivity
::
class
.
java
)
notificationIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_SINGLE_TOP
or
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
)
notificationIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_SINGLE_TOP
or
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
)
notificationIntent
.
putExtra
(
PushConstants
.
NOT_ID
,
notificationId
)
notificationIntent
.
putExtra
(
PushConstants
.
NOT_ID
,
notificationId
)
notificationIntent
.
putExtra
(
PushConstants
.
HOSTNAME
,
pushMessage
.
host
)
notificationIntent
.
putExtra
(
PushConstants
.
HOSTNAME
,
pushMessage
.
host
)
if
(
!
group
)
{
if
(
singleConversation
)
{
notificationIntent
.
putExtra
(
PushConstants
.
ROOM_ID
,
pushMessage
.
rid
)
notificationIntent
.
putExtra
(
PushConstants
.
ROOM_ID
,
pushMessage
.
rid
)
}
}
return
PendingIntent
.
getActivity
(
context
,
randomizer
.
nextInt
(),
notificationIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
)
return
PendingIntent
.
getActivity
(
context
,
randomizer
.
nextInt
(),
notificationIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
)
...
@@ -392,7 +417,6 @@ object PushManager {
...
@@ -392,7 +417,6 @@ object PushManager {
setAutoCancel
(
true
)
setAutoCancel
(
true
)
setShowWhen
(
true
)
setShowWhen
(
true
)
setColor
(
res
.
getColor
(
R
.
color
.
colorRed400
,
ctx
.
theme
))
setColor
(
res
.
getColor
(
R
.
color
.
colorRed400
,
ctx
.
theme
))
setDefaults
(
Notification
.
DEFAULT_ALL
)
setSmallIcon
(
smallIcon
)
setSmallIcon
(
smallIcon
)
})
})
return
this
return
this
...
@@ -478,8 +502,12 @@ object PushManager {
...
@@ -478,8 +502,12 @@ object PushManager {
class
DeleteReceiver
:
BroadcastReceiver
()
{
class
DeleteReceiver
:
BroadcastReceiver
()
{
override
fun
onReceive
(
context
:
Context
?,
intent
:
Intent
?)
{
override
fun
onReceive
(
context
:
Context
?,
intent
:
Intent
?)
{
val
notificationId
=
intent
?.
extras
?.
getInt
(
"notId"
)
val
notificationId
=
intent
?.
extras
?.
getInt
(
"notId"
)
if
(
notificationId
!=
null
)
{
val
host
=
intent
?.
extras
?.
getString
(
"host"
)
PushManager
.
clearMessageBundle
(
notificationId
)
notificationId
?.
let
{
clearNotificationIdStack
(
notificationId
)
}
host
?.
let
{
clearHostNotifications
(
host
)
}
}
}
}
}
}
...
@@ -487,7 +515,7 @@ object PushManager {
...
@@ -487,7 +515,7 @@ object PushManager {
/**
/**
* *EXPERIMENTAL*
* *EXPERIMENTAL*
*
*
* BroadcastReceiver for notifications' replies.
* BroadcastReceiver for notifications' replies
using Direct Reply feature (Android >= 7)
.
*/
*/
class
ReplyReceiver
:
BroadcastReceiver
()
{
class
ReplyReceiver
:
BroadcastReceiver
()
{
...
@@ -509,7 +537,7 @@ object PushManager {
...
@@ -509,7 +537,7 @@ object PushManager {
manager
.
cancel
(
userNotId
)
manager
.
cancel
(
userNotId
)
groupTuple
?.
second
?.
decrementAndGet
()
groupTuple
?.
second
?.
decrementAndGet
()
}
}
clear
MessageBundle
(
userNotId
)
clear
NotificationIdStack
(
userNotId
)
groupTuple
?.
let
{
groupTuple
?.
let
{
val
groupNotId
=
groupTuple
.
first
val
groupNotId
=
groupTuple
.
first
val
totalNot
=
groupTuple
.
second
.
get
()
val
totalNot
=
groupTuple
.
second
.
get
()
...
...
app/src/main/java/chat/rocket/android/push/gcm/GCMIntentService.java
View file @
59e7f90f
package
chat
.
rocket
.
android
.
push
.
gcm
;
package
chat
.
rocket
.
android
.
push
.
gcm
;
import
com.google.android.gms.gcm.GcmListenerService
;
import
android.annotation.SuppressLint
;
import
android.annotation.SuppressLint
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.res.Resources
;
import
android.content.res.Resources
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.util.Log
;
import
android.util.Log
;
import
com.google.android.gms.gcm.GcmListenerService
;
import
org.json.JSONArray
;
import
org.json.JSONArray
;
import
org.json.JSONException
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
chat.rocket.android.push.PushConstants
;
import
chat.rocket.android.push.PushConstants
;
import
chat.rocket.android.push.PushManager
;
import
chat.rocket.android.push.PushManager
;
import
chat.rocket.android.push.PushNotificationHandler
;
@SuppressLint
(
"NewApi"
)
@SuppressLint
(
"NewApi"
)
public
class
GCMIntentService
extends
GcmListenerService
implements
PushConstants
{
public
class
GCMIntentService
extends
GcmListenerService
implements
PushConstants
{
...
@@ -34,9 +35,6 @@ public class GCMIntentService extends GcmListenerService implements PushConstant
...
@@ -34,9 +35,6 @@ public class GCMIntentService extends GcmListenerService implements PushConstant
extras
=
normalizeExtras
(
applicationContext
,
extras
);
extras
=
normalizeExtras
(
applicationContext
,
extras
);
PushNotificationHandler
pushNotificationHandler
=
new
PushNotificationHandler
();
pushNotificationHandler
.
showNotificationIfPossible
(
applicationContext
,
extras
);
PushManager
.
INSTANCE
.
handle
(
applicationContext
,
extras
);
PushManager
.
INSTANCE
.
handle
(
applicationContext
,
extras
);
}
}
...
...
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