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
5b476274
Commit
5b476274
authored
Oct 18, 2017
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for new PushManager class from AbstractAuthedActivities
parent
1c73d226
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
22 deletions
+62
-22
AbstractAuthedActivity.java
.../chat/rocket/android/activity/AbstractAuthedActivity.java
+16
-12
PushManager.kt
app/src/main/java/chat/rocket/android/push/PushManager.kt
+46
-10
No files found.
app/src/main/java/chat/rocket/android/activity/AbstractAuthedActivity.java
View file @
5b476274
...
@@ -3,23 +3,25 @@ package chat.rocket.android.activity;
...
@@ -3,23 +3,25 @@ package chat.rocket.android.activity;
import
android.content.Intent
;
import
android.content.Intent
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.support.annotation.Nullable
;
import
android.support.annotation.Nullable
;
import
com.hadisatrio.optional.Optional
;
import
com.hadisatrio.optional.Optional
;
import
chat.rocket.android.push.PushManager
;
import
io.reactivex.android.schedulers.AndroidSchedulers
;
import
io.reactivex.disposables.CompositeDisposable
;
import
io.reactivex.schedulers.Schedulers
;
import
java.util.List
;
import
java.util.List
;
import
chat.rocket.android.LaunchUtil
;
import
chat.rocket.android.LaunchUtil
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.RocketChatCache
;
import
chat.rocket.android.helper.Logger
;
import
chat.rocket.android.helper.Logger
;
import
chat.rocket.android.push.PushConstants
;
import
chat.rocket.android.push.PushConstants
;
import
chat.rocket.android.push.Push
NotificationHandl
er
;
import
chat.rocket.android.push.Push
Manag
er
;
import
chat.rocket.android.service.ConnectivityManager
;
import
chat.rocket.android.service.ConnectivityManager
;
import
chat.rocket.core.models.ServerInfo
;
import
chat.rocket.core.models.ServerInfo
;
import
chat.rocket.persistence.realm.RealmStore
;
import
chat.rocket.persistence.realm.RealmStore
;
import
chat.rocket.persistence.realm.models.ddp.RealmRoom
;
import
chat.rocket.persistence.realm.models.ddp.RealmRoom
;
import
icepick.State
;
import
icepick.State
;
import
io.reactivex.android.schedulers.AndroidSchedulers
;
import
io.reactivex.disposables.CompositeDisposable
;
import
io.reactivex.schedulers.Schedulers
;
import
okhttp3.HttpUrl
;
abstract
class
AbstractAuthedActivity
extends
AbstractFragmentActivity
{
abstract
class
AbstractAuthedActivity
extends
AbstractFragmentActivity
{
@State
protected
String
hostname
;
@State
protected
String
hostname
;
...
@@ -56,10 +58,14 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...
@@ -56,10 +58,14 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
}
}
if
(
intent
.
hasExtra
(
PushConstants
.
HOSTNAME
))
{
if
(
intent
.
hasExtra
(
PushConstants
.
HOSTNAME
))
{
rocketChatCache
.
setSelectedServerHostname
(
intent
.
getStringExtra
(
PushConstants
.
HOSTNAME
));
String
hostname
=
intent
.
getStringExtra
(
PushConstants
.
HOSTNAME
);
HttpUrl
url
=
HttpUrl
.
parse
(
hostname
);
if
(
intent
.
hasExtra
(
PushConstants
.
ROOM_ID
))
{
if
(
url
!=
null
)
{
rocketChatCache
.
setSelectedRoomId
(
intent
.
getStringExtra
(
PushConstants
.
ROOM_ID
));
rocketChatCache
.
setSelectedServerHostname
(
url
.
host
());
if
(
intent
.
hasExtra
(
PushConstants
.
ROOM_ID
))
{
rocketChatCache
.
setSelectedRoomId
(
intent
.
getStringExtra
(
PushConstants
.
ROOM_ID
));
}
}
}
}
else
{
}
else
{
updateHostnameIfNeeded
(
rocketChatCache
.
getSelectedServerHostname
());
updateHostnameIfNeeded
(
rocketChatCache
.
getSelectedServerHostname
());
...
@@ -68,9 +74,7 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...
@@ -68,9 +74,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
);
PushNotificationHandler
PushManager
.
INSTANCE
.
clearMessageStack
(
notificationId
);
.
cleanUpNotificationStack
(
notificationId
);
PushManager
.
INSTANCE
.
clearStack
(
notificationId
);
}
}
}
}
...
...
app/src/main/java/chat/rocket/android/push/PushManager.kt
View file @
5b476274
...
@@ -14,12 +14,18 @@ import android.support.v4.app.NotificationCompat
...
@@ -14,12 +14,18 @@ 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
import
android.text.Spanned
import
android.text.Spanned
import
android.util.Log
import
android.util.SparseArray
import
android.util.SparseArray
import
chat.rocket.android.BuildConfig
import
chat.rocket.android.activity.MainActivity
import
org.json.JSONObject
import
org.json.JSONObject
import
java.util.*
object
PushManager
{
object
PushManager
{
// A map associating a notification id to a list of corresponding messages.
val
messageStack
=
SparseArray
<
ArrayList
<
String
>>()
val
messageStack
=
SparseArray
<
ArrayList
<
String
>>()
val
randomizer
=
Random
()
fun
handle
(
context
:
Context
,
data
:
Bundle
)
{
fun
handle
(
context
:
Context
,
data
:
Bundle
)
{
val
appContext
=
context
.
applicationContext
val
appContext
=
context
.
applicationContext
...
@@ -39,6 +45,11 @@ object PushManager {
...
@@ -39,6 +45,11 @@ object PushManager {
summaryText
,
summaryText
,
style
)
style
)
// We should use Timber here
if
(
BuildConfig
.
DEBUG
)
{
Log
.
d
(
PushMessage
::
class
.
java
.
simpleName
,
pushMessage
.
toString
())
}
val
res
=
appContext
.
resources
val
res
=
appContext
.
resources
val
smallIcon
=
res
.
getIdentifier
(
"rocket_chat_notification"
,
"drawable"
,
appContext
.
packageName
)
val
smallIcon
=
res
.
getIdentifier
(
"rocket_chat_notification"
,
"drawable"
,
appContext
.
packageName
)
...
@@ -50,20 +61,24 @@ object PushManager {
...
@@ -50,20 +61,24 @@ object PushManager {
val
notification
:
Notification
val
notification
:
Notification
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
notification
=
createNotificationForOreoAndAbove
(
appContext
,
pushMessage
,
smallIcon
)
notification
=
createNotificationForOreoAndAbove
(
appContext
,
pushMessage
,
smallIcon
,
data
)
notificationManager
.
notify
(
notificationId
.
toInt
(),
notification
)
notificationManager
.
notify
(
notificationId
.
toInt
(),
notification
)
}
else
{
}
else
{
notification
=
createCompatNotification
(
appContext
,
pushMessage
,
smallIcon
)
notification
=
createCompatNotification
(
appContext
,
pushMessage
,
smallIcon
,
data
)
NotificationManagerCompat
.
from
(
appContext
).
notify
(
notificationId
.
toInt
(),
notification
)
NotificationManagerCompat
.
from
(
appContext
).
notify
(
notificationId
.
toInt
(),
notification
)
}
}
}
}
fun
clearStack
(
notificationId
:
Int
)
{
fun
clear
Message
Stack
(
notificationId
:
Int
)
{
messageStack
.
delete
(
notificationId
)
messageStack
.
delete
(
notificationId
)
}
}
private
fun
createCompatNotification
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
):
Notification
{
private
fun
createCompatNotification
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
,
data
:
Bundle
):
Notification
{
with
(
pushMessage
)
{
with
(
pushMessage
)
{
val
id
=
notificationId
.
toInt
()
val
contentIntent
=
getContentIntent
(
context
,
id
,
data
,
pushMessage
)
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
notificationBuilder
=
NotificationCompat
.
Builder
(
context
)
val
notificationBuilder
=
NotificationCompat
.
Builder
(
context
)
.
setAutoCancel
(
true
)
.
setAutoCancel
(
true
)
.
setShowWhen
(
true
)
.
setShowWhen
(
true
)
...
@@ -73,25 +88,29 @@ object PushManager {
...
@@ -73,25 +88,29 @@ object PushManager {
.
setContentText
(
message
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
.
setSmallIcon
(
smallIcon
)
.
setDeleteIntent
(
getDismissIntent
(
context
,
notificationId
.
toInt
()))
.
setDeleteIntent
(
deleteIntent
)
.
setContentIntent
(
contentIntent
)
if
(
"inbox"
==
style
)
{
if
(
"inbox"
==
style
)
{
val
messages
=
chat
.
rocket
.
android
.
push
.
PushManager
.
messageStack
.
get
(
notificationId
.
toInt
())
val
messages
=
messageStack
.
get
(
notificationId
.
toInt
())
val
messageCount
=
messages
.
size
val
messageCount
=
messages
.
size
if
(
messageCount
>
1
)
{
if
(
messageCount
>
1
)
{
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
val
summary
=
summaryText
.
replace
(
"%n%"
,
messageCount
.
toString
())
.
fromHtml
()
.
fromHtml
()
val
inbox
=
android
.
support
.
v4
.
app
.
NotificationCompat
.
InboxStyle
()
val
inbox
=
NotificationCompat
.
InboxStyle
()
.
setBigContentTitle
(
title
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setSummaryText
(
summary
)
.
setSummaryText
(
summary
)
messages
.
forEach
{
msg
->
messages
.
forEach
{
msg
->
inbox
.
addLine
(
msg
.
fromHtml
())
inbox
.
addLine
(
msg
.
fromHtml
())
}
}
notificationBuilder
.
setStyle
(
inbox
)
notificationBuilder
.
setStyle
(
inbox
)
}
else
{
}
else
{
val
bigText
=
android
.
support
.
v4
.
app
.
NotificationCompat
.
BigTextStyle
()
val
bigText
=
NotificationCompat
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notificationBuilder
.
setStyle
(
bigText
)
notificationBuilder
.
setStyle
(
bigText
)
}
}
}
else
{
}
else
{
...
@@ -103,11 +122,15 @@ object PushManager {
...
@@ -103,11 +122,15 @@ object PushManager {
}
}
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
@RequiresApi
(
Build
.
VERSION_CODES
.
O
)
private
fun
createNotificationForOreoAndAbove
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
):
Notification
{
private
fun
createNotificationForOreoAndAbove
(
context
:
Context
,
pushMessage
:
PushMessage
,
smallIcon
:
Int
,
data
:
Bundle
):
Notification
{
val
notificationManager
:
NotificationManager
=
val
notificationManager
:
NotificationManager
=
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
context
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)
as
NotificationManager
with
(
pushMessage
)
{
with
(
pushMessage
)
{
val
id
=
notificationId
.
toInt
()
val
contentIntent
=
getContentIntent
(
context
,
id
,
data
,
pushMessage
)
val
deleteIntent
=
getDismissIntent
(
context
,
id
)
val
channel
=
NotificationChannel
(
notificationId
,
sender
.
username
,
NotificationManager
.
IMPORTANCE_HIGH
)
val
channel
=
NotificationChannel
(
notificationId
,
sender
.
username
,
NotificationManager
.
IMPORTANCE_HIGH
)
val
notification
=
Notification
.
Builder
(
context
,
pushMessage
.
rid
)
val
notification
=
Notification
.
Builder
(
context
,
pushMessage
.
rid
)
.
setAutoCancel
(
true
)
.
setAutoCancel
(
true
)
...
@@ -117,6 +140,8 @@ object PushManager {
...
@@ -117,6 +140,8 @@ object PushManager {
.
setContentText
(
message
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
.
setSmallIcon
(
smallIcon
)
.
setDeleteIntent
(
deleteIntent
)
.
setContentIntent
(
contentIntent
)
.
build
()
.
build
()
channel
.
enableLights
(
true
)
channel
.
enableLights
(
true
)
...
@@ -144,6 +169,17 @@ object PushManager {
...
@@ -144,6 +169,17 @@ object PushManager {
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
}
}
private
fun
getContentIntent
(
context
:
Context
,
notificationId
:
Int
,
extras
:
Bundle
,
pushMessage
:
PushMessage
):
PendingIntent
{
val
notificationIntent
=
Intent
(
context
,
MainActivity
::
class
.
java
)
notificationIntent
.
addFlags
(
Intent
.
FLAG_ACTIVITY_SINGLE_TOP
or
Intent
.
FLAG_ACTIVITY_CLEAR_TOP
)
notificationIntent
.
putExtra
(
PushConstants
.
PUSH_BUNDLE
,
extras
)
notificationIntent
.
putExtra
(
PushConstants
.
NOT_ID
,
notificationId
)
notificationIntent
.
putExtra
(
PushConstants
.
HOSTNAME
,
pushMessage
.
host
)
notificationIntent
.
putExtra
(
PushConstants
.
ROOM_ID
,
pushMessage
.
rid
)
return
PendingIntent
.
getActivity
(
context
,
randomizer
.
nextInt
(),
notificationIntent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
)
}
data class
PushMessage
(
val
title
:
String
,
data class
PushMessage
(
val
title
:
String
,
val
message
:
String
,
val
message
:
String
,
val
image
:
String
?,
val
image
:
String
?,
...
@@ -187,7 +223,7 @@ object PushManager {
...
@@ -187,7 +223,7 @@ object PushManager {
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
)
{
if
(
notificationId
!=
null
)
{
PushManager
.
clearStack
(
notificationId
)
PushManager
.
clear
Message
Stack
(
notificationId
)
}
}
}
}
}
}
...
...
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