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
75ba17fa
Commit
75ba17fa
authored
Oct 17, 2017
by
Leonardo Aramaki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New implementation for the push handler in Kotlin
parent
afe0cff6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
182 additions
and
2 deletions
+182
-2
AndroidManifest.xml
app/src/main/AndroidManifest.xml
+4
-0
AbstractAuthedActivity.java
.../chat/rocket/android/activity/AbstractAuthedActivity.java
+5
-1
PushManager.kt
app/src/main/java/chat/rocket/android/push/PushManager.kt
+170
-0
GCMIntentService.java
...n/java/chat/rocket/android/push/gcm/GCMIntentService.java
+3
-1
No files found.
app/src/main/AndroidManifest.xml
View file @
75ba17fa
...
@@ -76,6 +76,10 @@
...
@@ -76,6 +76,10 @@
</intent-filter>
</intent-filter>
</service>
</service>
<receiver
android:name=
".push.PushManager$DeleteReceiver"
android:exported=
"false"
>
</receiver>
<meta-data
<meta-data
android:name=
"io.fabric.ApiKey"
android:name=
"io.fabric.ApiKey"
android:value=
"12ac6e94f850aaffcdff52001af77ca415d06a43"
/>
android:value=
"12ac6e94f850aaffcdff52001af77ca415d06a43"
/>
...
...
app/src/main/java/chat/rocket/android/activity/AbstractAuthedActivity.java
View file @
75ba17fa
...
@@ -4,6 +4,8 @@ import android.content.Intent;
...
@@ -4,6 +4,8 @@ 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.android.schedulers.AndroidSchedulers
;
import
io.reactivex.disposables.CompositeDisposable
;
import
io.reactivex.disposables.CompositeDisposable
;
import
io.reactivex.schedulers.Schedulers
;
import
io.reactivex.schedulers.Schedulers
;
...
@@ -65,8 +67,10 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
...
@@ -65,8 +67,10 @@ 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
);
PushNotificationHandler
PushNotificationHandler
.
cleanUpNotificationStack
(
intent
.
getIntExtra
(
PushConstants
.
NOT_ID
,
0
));
.
cleanUpNotificationStack
(
notificationId
);
PushManager
.
INSTANCE
.
clearStack
(
notificationId
);
}
}
}
}
...
...
app/src/main/java/chat/rocket/android/push/PushManager.kt
0 → 100644
View file @
75ba17fa
package
chat.rocket.android.push
import
android.app.Notification
import
android.app.NotificationChannel
import
android.app.NotificationManager
import
android.app.PendingIntent
import
android.content.BroadcastReceiver
import
android.content.Context
import
android.content.Intent
import
android.os.Build
import
android.os.Bundle
import
android.support.v4.app.NotificationCompat
import
android.support.v4.app.NotificationManagerCompat
import
android.text.Html
import
android.text.Spanned
import
android.util.SparseArray
import
org.json.JSONObject
object
PushManager
{
val
messageStack
=
SparseArray
<
ArrayList
<
String
>>()
fun
handle
(
context
:
Context
,
data
:
Bundle
)
{
val
now
=
System
.
currentTimeMillis
()
val
appContext
=
context
.
applicationContext
val
notificationId
=
data
[
"notId"
]
as
String
val
style
=
data
[
"style"
]
as
String
val
summaryText
=
data
[
"summaryText"
]
as
String
val
count
=
data
[
"count"
]
as
String
val
pushMessage
=
PushMessage
(
data
[
"title"
]
as
String
,
data
[
"message"
]
as
String
,
data
[
"image"
]
as
String
,
data
[
"ejson"
]
as
String
)
val
res
=
appContext
.
resources
val
smallIcon
=
res
.
getIdentifier
(
"rocket_chat_notification"
,
"drawable"
,
appContext
.
packageName
)
stackMessage
(
notificationId
.
toInt
(),
pushMessage
.
message
)
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
O
)
{
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
(
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
{
with
(
pushMessage
)
{
val
notificationBuilder
=
NotificationCompat
.
Builder
(
appContext
)
.
setAutoCancel
(
true
)
.
setShowWhen
(
true
)
.
setDefaults
(
Notification
.
DEFAULT_ALL
)
.
setWhen
(
now
)
.
setContentTitle
(
title
.
fromHtml
())
.
setContentText
(
message
.
fromHtml
())
.
setNumber
(
count
.
toInt
())
.
setSmallIcon
(
smallIcon
)
.
setDeleteIntent
(
getDismissIntent
(
appContext
,
notificationId
.
toInt
()))
if
(
"inbox"
==
style
)
{
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
->
inbox
.
addLine
(
msg
.
fromHtml
())
}
notificationBuilder
.
setStyle
(
inbox
)
}
else
{
val
bigText
=
NotificationCompat
.
BigTextStyle
()
.
bigText
(
message
.
fromHtml
())
.
setBigContentTitle
(
title
.
fromHtml
())
notificationBuilder
.
setStyle
(
bigText
)
}
}
else
{
notificationBuilder
.
setContentText
(
message
.
fromHtml
())
}
val
notification
=
notificationBuilder
.
build
()
NotificationManagerCompat
.
from
(
appContext
).
notify
(
notificationId
.
toInt
(),
notification
)
}
}
}
fun
clearStack
(
notificationId
:
Int
)
{
messageStack
.
delete
(
notificationId
)
}
private
fun
stackMessage
(
id
:
Int
,
message
:
String
)
{
val
existingStack
:
ArrayList
<
String
>?
=
messageStack
[
id
]
if
(
existingStack
==
null
)
{
val
newStack
=
arrayListOf
<
String
>()
newStack
.
add
(
message
)
messageStack
.
put
(
id
,
newStack
)
}
else
{
existingStack
.
add
(
0
,
message
)
}
}
private
fun
getDismissIntent
(
context
:
Context
,
notificationId
:
Int
):
PendingIntent
{
val
deleteIntent
=
Intent
(
context
,
DeleteReceiver
::
class
.
java
)
deleteIntent
.
putExtra
(
"notId"
,
notificationId
)
return
PendingIntent
.
getBroadcast
(
context
,
notificationId
,
deleteIntent
,
0
)
}
data class
PushMessage
(
val
title
:
String
,
val
message
:
String
,
val
image
:
String
?,
val
ejson
:
String
)
{
val
host
:
String
val
rid
:
String
val
type
:
String
val
name
:
String
?
val
sender
:
Sender
init
{
val
json
=
JSONObject
(
ejson
)
host
=
json
.
getString
(
"host"
)
rid
=
json
.
getString
(
"rid"
)
type
=
json
.
getString
(
"type"
)
name
=
json
.
optString
(
"name"
)
sender
=
Sender
(
json
.
getString
(
"sender"
))
}
data class
Sender
(
val
sender
:
String
)
{
val
_id
:
String
val
username
:
String
val
name
:
String
init
{
val
json
=
JSONObject
(
sender
)
_id
=
json
.
getString
(
"_id"
)
username
=
json
.
getString
(
"username"
)
name
=
json
.
getString
(
"name"
)
}
}
}
class
DeleteReceiver
:
BroadcastReceiver
()
{
override
fun
onReceive
(
context
:
Context
?,
intent
:
Intent
?)
{
val
notificationId
=
intent
?.
extras
?.
getInt
(
"notId"
)
if
(
notificationId
!=
null
)
{
PushManager
.
clearStack
(
notificationId
)
}
}
}
// String extensions
fun
String
.
fromHtml
():
Spanned
{
return
Html
.
fromHtml
(
this
)
}
}
\ No newline at end of file
app/src/main/java/chat/rocket/android/push/gcm/GCMIntentService.java
View file @
75ba17fa
...
@@ -14,6 +14,7 @@ import org.json.JSONObject;
...
@@ -14,6 +14,7 @@ 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.PushNotificationHandler
;
import
chat.rocket.android.push.PushNotificationHandler
;
@SuppressLint
(
"NewApi"
)
@SuppressLint
(
"NewApi"
)
...
@@ -35,7 +36,8 @@ public class GCMIntentService extends GcmListenerService implements PushConstant
...
@@ -35,7 +36,8 @@ public class GCMIntentService extends GcmListenerService implements PushConstant
PushNotificationHandler
pushNotificationHandler
=
new
PushNotificationHandler
();
PushNotificationHandler
pushNotificationHandler
=
new
PushNotificationHandler
();
pushNotificationHandler
.
showNotificationIfPossible
(
applicationContext
,
extras
);
// pushNotificationHandler.showNotificationIfPossible(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