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
cd68a18a
Commit
cd68a18a
authored
Sep 30, 2018
by
Varun Patil
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix follow bug in foreground notifications
parent
19dfd4bc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
33 deletions
+79
-33
app/src/main/java/app/insti/Constants.java
app/src/main/java/app/insti/Constants.java
+4
-0
app/src/main/java/app/insti/InstiAppFirebaseMessagingService.java
...main/java/app/insti/InstiAppFirebaseMessagingService.java
+65
-33
app/src/main/java/app/insti/notifications/NotificationId.java
...src/main/java/app/insti/notifications/NotificationId.java
+10
-0
No files found.
app/src/main/java/app/insti/Constants.java
View file @
cd68a18a
...
@@ -33,6 +33,10 @@ public class Constants {
...
@@ -33,6 +33,10 @@ public class Constants {
public
static
final
String
FCM_BUNDLE_ID
=
"id"
;
public
static
final
String
FCM_BUNDLE_ID
=
"id"
;
public
static
final
String
FCM_BUNDLE_EXTRA
=
"extra"
;
public
static
final
String
FCM_BUNDLE_EXTRA
=
"extra"
;
public
static
final
String
FCM_BUNDLE_NOTIFICATION_ID
=
"notification_id"
;
public
static
final
String
FCM_BUNDLE_NOTIFICATION_ID
=
"notification_id"
;
public
static
final
String
FCM_BUNDLE_ACTION
=
"action"
;
public
static
final
String
FCM_BUNDLE_IS_DATA
=
"is_data"
;
public
static
final
String
FCM_BUNDLE_ACTION_STARTING
=
"starting"
;
public
static
final
String
DATA_TYPE_EVENT
=
"event"
;
public
static
final
String
DATA_TYPE_EVENT
=
"event"
;
public
static
final
String
DATA_TYPE_BODY
=
"body"
;
public
static
final
String
DATA_TYPE_BODY
=
"body"
;
...
...
app/src/main/java/app/insti/InstiAppFirebaseMessagingService.java
View file @
cd68a18a
...
@@ -14,8 +14,11 @@ import com.google.firebase.messaging.RemoteMessage;
...
@@ -14,8 +14,11 @@ import com.google.firebase.messaging.RemoteMessage;
import
java.util.Map
;
import
java.util.Map
;
import
app.insti.activity.MainActivity
;
import
app.insti.activity.MainActivity
;
import
app.insti.notifications.NotificationId
;
public
class
InstiAppFirebaseMessagingService
extends
FirebaseMessagingService
{
public
class
InstiAppFirebaseMessagingService
extends
FirebaseMessagingService
{
String
channel
;
@Override
@Override
public
void
onNewToken
(
String
s
)
{
public
void
onNewToken
(
String
s
)
{
/* For future functionality */
/* For future functionality */
...
@@ -32,56 +35,85 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
...
@@ -32,56 +35,85 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
}
}
/** Get a PendingIntent to open MainActivity from a notification message */
/** Get a PendingIntent to open MainActivity from a notification message */
private
PendingIntent
getNotificationIntent
(
RemoteMessage
remoteMessage
)
{
private
PendingIntent
getNotificationIntent
(
RemoteMessage
remoteMessage
,
Integer
notificationId
)
{
Intent
intent
=
new
Intent
(
this
,
MainActivity
.
class
);
Intent
intent
=
new
Intent
(
this
,
MainActivity
.
class
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
|
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
);
intent
.
setFlags
(
Intent
.
FLAG_ACTIVITY_NEW_TASK
|
Intent
.
FLAG_ACTIVITY_CLEAR_TASK
);
intent
.
putExtra
(
Constants
.
MAIN_INTENT_EXTRAS
,
stringMapToBundle
(
remoteMessage
.
getData
()));
intent
.
putExtra
(
Constants
.
MAIN_INTENT_EXTRAS
,
stringMapToBundle
(
remoteMessage
.
getData
()));
return
PendingIntent
.
getActivity
(
this
,
0
,
intent
,
0
);
return
PendingIntent
.
getActivity
(
this
,
notificationId
,
intent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
}
}
@Override
@Override
public
void
onMessageReceived
(
RemoteMessage
remoteMessage
)
{
public
void
onMessageReceived
(
RemoteMessage
remoteMessage
)
{
String
TAG
=
"NOTIFICATION"
;
String
TAG
=
"NOTIFICATION"
;
String
channel
=
getResources
().
getString
(
R
.
string
.
default_notification_channel_id
);
channel
=
getResources
().
getString
(
R
.
string
.
default_notification_channel_id
);
// Check if message contains a data payload.
// Check if message contains a data payload.
if
(
remoteMessage
.
getData
().
size
()
>
0
)
{
if
(
remoteMessage
.
getData
().
size
()
>
0
)
{
Log
.
wtf
(
TAG
,
"Message data payload: "
+
remoteMessage
.
getData
());
Log
.
wtf
(
TAG
,
"Message data payload: "
+
remoteMessage
.
getData
());
String
isData
=
remoteMessage
.
getData
().
get
(
"is_data"
);
String
isData
=
remoteMessage
.
getData
().
get
(
Constants
.
FCM_BUNDLE_IS_DATA
);
if
(
isData
!=
null
&&
isData
.
equals
(
"true"
))
{
if
(
isData
!=
null
&&
isData
.
equals
(
"true"
))
{
// TODO: Implement this
String
type
=
remoteMessage
.
getData
().
get
(
Constants
.
FCM_BUNDLE_TYPE
);
}
else
{
String
action
=
remoteMessage
.
getData
().
get
(
Constants
.
FCM_BUNDLE_ACTION
);
/* 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
(
type
.
equals
(
Constants
.
DATA_TYPE_EVENT
)
&&
action
.
equals
(
Constants
.
FCM_BUNDLE_ACTION_STARTING
))
{
if
(
title
==
null
||
body
==
null
)
{
return
;
}
sendEventStartingNotification
(
remoteMessage
);
}
/* Build notification */
}
else
{
Notification
notification
=
new
NotificationCompat
.
Builder
(
this
,
channel
)
sendMessageNotification
(
remoteMessage
);
.
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
);
}
}
/** Send a event is starting notification */
private
void
sendEventStartingNotification
(
RemoteMessage
remoteMessage
)
{
int
notification_id
=
NotificationId
.
getID
();
Notification
notification
=
new
NotificationCompat
.
Builder
(
this
,
channel
)
.
setSmallIcon
(
R
.
drawable
.
ic_lotusgray
)
.
setColor
(
getResources
().
getColor
(
R
.
color
.
colorPrimary
))
.
setContentTitle
(
"TEST"
)
.
setContentText
(
"TEST"
)
.
setContentIntent
(
getNotificationIntent
(
remoteMessage
,
notification_id
))
.
setVibrate
(
new
long
[]{
0
,
400
})
.
setAutoCancel
(
true
)
.
setPriority
(
NotificationCompat
.
PRIORITY_DEFAULT
)
.
build
();
/* Show notification */
NotificationManagerCompat
notificationManager
=
NotificationManagerCompat
.
from
(
this
);
notificationManager
.
notify
(
notification_id
,
notification
);
}
/** Send a standard notification from foreground */
private
void
sendMessageNotification
(
RemoteMessage
remoteMessage
)
{
/* 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
,
notification_id
))
.
setVibrate
(
new
long
[]{
0
,
400
})
.
setAutoCancel
(
true
)
.
setPriority
(
NotificationCompat
.
PRIORITY_DEFAULT
)
.
build
();
/* Show notification */
NotificationManagerCompat
notificationManager
=
NotificationManagerCompat
.
from
(
this
);
notificationManager
.
notify
(
notification_id
,
notification
);
}
}
}
app/src/main/java/app/insti/notifications/NotificationId.java
0 → 100644
View file @
cd68a18a
package
app.insti.notifications
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
NotificationId
{
private
final
static
AtomicInteger
c
=
new
AtomicInteger
(
0
);
public
static
int
getID
()
{
return
c
.
incrementAndGet
();
}
}
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