Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
InstiApp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
RAHUL SHARMA
InstiApp
Commits
19dfd4bc
Commit
19dfd4bc
authored
Sep 30, 2018
by
Varun Patil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Show notifications in foreground
parent
fa2b195d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletion
+71
-1
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+1
-0
app/src/main/java/app/insti/InstiAppFirebaseMessagingService.java
...main/java/app/insti/InstiAppFirebaseMessagingService.java
+70
-1
No files found.
app/src/main/AndroidManifest.xml
View file @
19dfd4bc
...
@@ -8,6 +8,7 @@
...
@@ -8,6 +8,7 @@
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission
android:name=
"android.permission.VIBRATE"
/>
<application
<application
android:allowBackup=
"true"
android:allowBackup=
"true"
...
...
app/src/main/java/app/insti/InstiAppFirebaseMessagingService.java
View file @
19dfd4bc
package
app.insti
;
package
app.insti
;
import
android.app.Notification
;
import
android.app.PendingIntent
;
import
android.content.Intent
;
import
android.os.Bundle
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.util.Log
;
import
com.google.firebase.messaging.FirebaseMessagingService
;
import
com.google.firebase.messaging.FirebaseMessagingService
;
import
com.google.firebase.messaging.RemoteMessage
;
import
com.google.firebase.messaging.RemoteMessage
;
import
java.util.Map
;
import
app.insti.activity.MainActivity
;
public
class
InstiAppFirebaseMessagingService
extends
FirebaseMessagingService
{
public
class
InstiAppFirebaseMessagingService
extends
FirebaseMessagingService
{
@Override
@Override
public
void
onNewToken
(
String
s
)
{
public
void
onNewToken
(
String
s
)
{
...
@@ -10,9 +22,66 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
...
@@ -10,9 +22,66 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
super
.
onNewToken
(
s
);
super
.
onNewToken
(
s
);
}
}
/** Convert a string to string map to a bundle */
private
Bundle
stringMapToBundle
(
Map
<
String
,
String
>
map
)
{
Bundle
bundle
=
new
Bundle
();
for
(
Map
.
Entry
<
String
,
String
>
entry
:
map
.
entrySet
())
{
bundle
.
putString
(
entry
.
getKey
(),
entry
.
getValue
());
}
return
bundle
;
}
/** Get a PendingIntent to open MainActivity from a notification message */
private
PendingIntent
getNotificationIntent
(
RemoteMessage
remoteMessage
)
{
Intent
intent
=
new
Intent
(
this
,
MainActivity
.
class
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
|
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
);
intent
.
putExtra
(
Constants
.
MAIN_INTENT_EXTRAS
,
stringMapToBundle
(
remoteMessage
.
getData
()));
return
PendingIntent
.
getActivity
(
this
,
0
,
intent
,
0
);
}
@Override
@Override
public
void
onMessageReceived
(
RemoteMessage
remoteMessage
)
{
public
void
onMessageReceived
(
RemoteMessage
remoteMessage
)
{
/* For future functionality */
String
TAG
=
"NOTIFICATION"
;
String
channel
=
getResources
().
getString
(
R
.
string
.
default_notification_channel_id
);
// Check if message contains a data payload.
if
(
remoteMessage
.
getData
().
size
()
>
0
)
{
Log
.
wtf
(
TAG
,
"Message data payload: "
+
remoteMessage
.
getData
());
String
isData
=
remoteMessage
.
getData
().
get
(
"is_data"
);
if
(
isData
!=
null
&&
isData
.
equals
(
"true"
))
{
// TODO: Implement this
}
else
{
/* Get data */
String
title
=
remoteMessage
.
getNotification
().
getTitle
();
String
body
=
remoteMessage
.
getNotification
().
getBody
();
Integer
notification_id
;
try
{
notification_id
=
Integer
.
parseInt
(
remoteMessage
.
getData
().
get
(
Constants
.
FCM_BUNDLE_NOTIFICATION_ID
));
}
catch
(
NumberFormatException
ignored
)
{
return
;
}
/* Check malformed notifications */
if
(
title
==
null
||
body
==
null
)
{
return
;
}
/* Build notification */
Notification
notification
=
new
NotificationCompat
.
Builder
(
this
,
channel
)
.
setSmallIcon
(
R
.
drawable
.
ic_lotusgray
)
.
setColor
(
getResources
().
getColor
(
R
.
color
.
colorPrimary
))
.
setContentTitle
(
title
)
.
setContentText
(
body
)
.
setContentIntent
(
getNotificationIntent
(
remoteMessage
))
.
setVibrate
(
new
long
[]{
0
,
400
})
.
setAutoCancel
(
true
)
.
setPriority
(
NotificationCompat
.
PRIORITY_DEFAULT
)
.
build
();
/* Show notification */
NotificationManagerCompat
notificationManager
=
NotificationManagerCompat
.
from
(
this
);
notificationManager
.
notify
(
notification_id
,
notification
);
}
}
super
.
onMessageReceived
(
remoteMessage
);
super
.
onMessageReceived
(
remoteMessage
);
}
}
}
}
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