Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-fcm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
ym
react-native-fcm
Commits
35d4bf03
Commit
35d4bf03
authored
Sep 26, 2017
by
Libin Lu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
requestPermissions for android
parent
d14cbc73
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
59 deletions
+62
-59
android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
...rc/main/java/com/evollu/react/fcm/FIRMessagingModule.java
+48
-42
android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java
.../main/java/com/evollu/react/fcm/SendNotificationTask.java
+14
-17
No files found.
android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
View file @
35d4bf03
...
@@ -24,6 +24,7 @@ import com.google.firebase.messaging.RemoteMessage.Notification;
...
@@ -24,6 +24,7 @@ import com.google.firebase.messaging.RemoteMessage.Notification;
import
android.app.Application
;
import
android.app.Application
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.util.Log
;
import
android.util.Log
;
import
android.content.Context
;
import
android.content.Context
;
...
@@ -57,7 +58,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -57,7 +58,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@ReactMethod
@ReactMethod
public
void
getInitialNotification
(
Promise
promise
){
public
void
getInitialNotification
(
Promise
promise
){
Activity
activity
=
getCurrentActivity
();
Activity
activity
=
getCurrentActivity
();
if
(
activity
==
null
||
activity
.
getIntent
().
getAction
()
==
"android.intent.action.MAIN"
){
if
(
activity
==
null
||
activity
.
getIntent
().
getAction
()
.
equals
(
"android.intent.action.MAIN"
)
){
promise
.
resolve
(
null
);
promise
.
resolve
(
null
);
return
;
return
;
}
}
...
@@ -65,17 +66,22 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -65,17 +66,22 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
}
@ReactMethod
@ReactMethod
public
void
requestPermissions
(){
public
void
requestPermissions
(
Promise
promise
){
if
(
NotificationManagerCompat
.
from
(
getReactApplicationContext
()).
areNotificationsEnabled
()){
promise
.
resolve
(
true
);
}
else
{
promise
.
reject
(
null
,
"Notification disabled"
);
}
}
}
@ReactMethod
@ReactMethod
public
void
getFCMToken
(
Promise
promise
)
{
public
void
getFCMToken
(
Promise
promise
)
{
try
{
try
{
Log
.
d
(
TAG
,
"Firebase token: "
+
FirebaseInstanceId
.
getInstance
().
getToken
());
Log
.
d
(
TAG
,
"Firebase token: "
+
FirebaseInstanceId
.
getInstance
().
getToken
());
promise
.
resolve
(
FirebaseInstanceId
.
getInstance
().
getToken
());
promise
.
resolve
(
FirebaseInstanceId
.
getInstance
().
getToken
());
}
catch
(
Throwable
e
)
{
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
promise
.
reject
(
null
,
e
.
getMessage
());
promise
.
reject
(
null
,
e
.
getMessage
());
}
}
}
}
...
@@ -89,7 +95,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -89,7 +95,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
promise
.
reject
(
null
,
e
.
getMessage
());
promise
.
reject
(
null
,
e
.
getMessage
());
}
}
}
}
@ReactMethod
@ReactMethod
public
void
presentLocalNotification
(
ReadableMap
details
)
{
public
void
presentLocalNotification
(
ReadableMap
details
)
{
Bundle
bundle
=
Arguments
.
toBundle
(
details
);
Bundle
bundle
=
Arguments
.
toBundle
(
details
);
...
@@ -104,11 +110,11 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -104,11 +110,11 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@ReactMethod
@ReactMethod
public
void
cancelLocalNotification
(
String
notificationID
)
{
public
void
cancelLocalNotification
(
String
notificationID
)
{
mFIRLocalMessagingHelper
.
cancelLocalNotification
(
notificationID
);
mFIRLocalMessagingHelper
.
cancelLocalNotification
(
notificationID
);
}
}
@ReactMethod
@ReactMethod
public
void
cancelAllLocalNotifications
()
{
public
void
cancelAllLocalNotifications
()
{
mFIRLocalMessagingHelper
.
cancelAllLocalNotifications
();
mFIRLocalMessagingHelper
.
cancelAllLocalNotifications
();
}
}
@ReactMethod
@ReactMethod
...
@@ -148,13 +154,13 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -148,13 +154,13 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@ReactMethod
@ReactMethod
public
void
getBadgeNumber
(
Promise
promise
)
{
public
void
getBadgeNumber
(
Promise
promise
)
{
promise
.
resolve
(
mBadgeHelper
.
getBadgeCount
());
promise
.
resolve
(
mBadgeHelper
.
getBadgeCount
());
}
}
private
void
sendEvent
(
String
eventName
,
Object
params
)
{
private
void
sendEvent
(
String
eventName
,
Object
params
)
{
getReactApplicationContext
()
getReactApplicationContext
()
.
getJSModule
(
DeviceEventManagerModule
.
RCTDeviceEventEmitter
.
class
)
.
getJSModule
(
DeviceEventManagerModule
.
RCTDeviceEventEmitter
.
class
)
.
emit
(
eventName
,
params
);
.
emit
(
eventName
,
params
);
}
}
private
void
registerTokenRefreshHandler
()
{
private
void
registerTokenRefreshHandler
()
{
...
@@ -174,7 +180,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -174,7 +180,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
public
void
send
(
String
senderId
,
ReadableMap
payload
)
throws
Exception
{
public
void
send
(
String
senderId
,
ReadableMap
payload
)
throws
Exception
{
FirebaseMessaging
fm
=
FirebaseMessaging
.
getInstance
();
FirebaseMessaging
fm
=
FirebaseMessaging
.
getInstance
();
RemoteMessage
.
Builder
message
=
new
RemoteMessage
.
Builder
(
senderId
+
"@gcm.googleapis.com"
)
RemoteMessage
.
Builder
message
=
new
RemoteMessage
.
Builder
(
senderId
+
"@gcm.googleapis.com"
)
.
setMessageId
(
UUID
.
randomUUID
().
toString
());
.
setMessageId
(
UUID
.
randomUUID
().
toString
());
ReadableMapKeySetIterator
iterator
=
payload
.
keySetIterator
();
ReadableMapKeySetIterator
iterator
=
payload
.
keySetIterator
();
while
(
iterator
.
hasNextKey
())
{
while
(
iterator
.
hasNextKey
())
{
...
@@ -208,36 +214,36 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -208,36 +214,36 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
getReactApplicationContext
().
registerReceiver
(
new
BroadcastReceiver
()
{
getReactApplicationContext
().
registerReceiver
(
new
BroadcastReceiver
()
{
@Override
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
if
(
getReactApplicationContext
().
hasActiveCatalystInstance
())
{
if
(
getReactApplicationContext
().
hasActiveCatalystInstance
())
{
RemoteMessage
message
=
intent
.
getParcelableExtra
(
"data"
);
RemoteMessage
message
=
intent
.
getParcelableExtra
(
"data"
);
WritableMap
params
=
Arguments
.
createMap
();
WritableMap
params
=
Arguments
.
createMap
();
WritableMap
fcmData
=
Arguments
.
createMap
();
WritableMap
fcmData
=
Arguments
.
createMap
();
if
(
message
.
getNotification
()
!=
null
)
{
if
(
message
.
getNotification
()
!=
null
)
{
Notification
notification
=
message
.
getNotification
();
Notification
notification
=
message
.
getNotification
();
fcmData
.
putString
(
"title"
,
notification
.
getTitle
());
fcmData
.
putString
(
"title"
,
notification
.
getTitle
());
fcmData
.
putString
(
"body"
,
notification
.
getBody
());
fcmData
.
putString
(
"body"
,
notification
.
getBody
());
fcmData
.
putString
(
"color"
,
notification
.
getColor
());
fcmData
.
putString
(
"color"
,
notification
.
getColor
());
fcmData
.
putString
(
"icon"
,
notification
.
getIcon
());
fcmData
.
putString
(
"icon"
,
notification
.
getIcon
());
fcmData
.
putString
(
"tag"
,
notification
.
getTag
());
fcmData
.
putString
(
"tag"
,
notification
.
getTag
());
fcmData
.
putString
(
"action"
,
notification
.
getClickAction
());
fcmData
.
putString
(
"action"
,
notification
.
getClickAction
());
}
params
.
putMap
(
"fcm"
,
fcmData
);
params
.
putString
(
"collapse_key"
,
message
.
getCollapseKey
());
params
.
putString
(
"from"
,
message
.
getFrom
());
params
.
putString
(
"google.message_id"
,
message
.
getMessageId
());
params
.
putDouble
(
"google.sent_time"
,
message
.
getSentTime
());
if
(
message
.
getData
()
!=
null
){
Map
<
String
,
String
>
data
=
message
.
getData
();
Set
<
String
>
keysIterator
=
data
.
keySet
();
for
(
String
key:
keysIterator
){
params
.
putString
(
key
,
data
.
get
(
key
));
}
}
}
params
.
putMap
(
"fcm"
,
fcmData
);
sendEvent
(
"FCMNotificationReceived"
,
params
);
params
.
putString
(
"collapse_key"
,
message
.
getCollapseKey
());
params
.
putString
(
"from"
,
message
.
getFrom
());
params
.
putString
(
"google.message_id"
,
message
.
getMessageId
());
params
.
putDouble
(
"google.sent_time"
,
message
.
getSentTime
());
if
(
message
.
getData
()
!=
null
){
Map
<
String
,
String
>
data
=
message
.
getData
();
Set
<
String
>
keysIterator
=
data
.
keySet
();
for
(
String
key:
keysIterator
){
params
.
putString
(
key
,
data
.
get
(
key
));
}
}
sendEvent
(
"FCMNotificationReceived"
,
params
);
}
}
}
}
},
intentFilter
);
},
intentFilter
);
}
}
...
@@ -298,7 +304,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
...
@@ -298,7 +304,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
@Override
@Override
public
void
onNewIntent
(
Intent
intent
){
public
void
onNewIntent
(
Intent
intent
){
// don't call notification if it is started from icon
// don't call notification if it is started from icon
if
(
intent
.
getAction
()
==
"android.intent.action.MAIN"
){
if
(
intent
.
getAction
()
.
equals
(
"android.intent.action.MAIN"
)
){
return
;
return
;
}
}
sendEvent
(
"FCMNotificationReceived"
,
parseIntent
(
intent
));
sendEvent
(
"FCMNotificationReceived"
,
parseIntent
(
intent
));
...
...
android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java
View file @
35d4bf03
package
com.evollu.react.fcm
;
package
com.evollu.react.fcm
;
import
android.app.Notification
;
import
android.app.Notification
;
import
android.app.NotificationManager
;
import
android.app.PendingIntent
;
import
android.app.PendingIntent
;
import
android.content.Context
;
import
android.content.Context
;
import
android.content.Intent
;
import
android.content.Intent
;
...
@@ -17,6 +16,7 @@ import android.os.AsyncTask;
...
@@ -17,6 +16,7 @@ import android.os.AsyncTask;
import
android.os.Build
;
import
android.os.Build
;
import
android.os.Bundle
;
import
android.os.Bundle
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.util.Log
;
import
android.util.Log
;
import
java.io.IOException
;
import
java.io.IOException
;
...
@@ -62,16 +62,16 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
...
@@ -62,16 +62,16 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
}
}
NotificationCompat
.
Builder
notification
=
new
NotificationCompat
.
Builder
(
mContext
)
NotificationCompat
.
Builder
notification
=
new
NotificationCompat
.
Builder
(
mContext
)
.
setContentTitle
(
title
)
.
setContentTitle
(
title
)
.
setContentText
(
bundle
.
getString
(
"body"
))
.
setContentText
(
bundle
.
getString
(
"body"
))
.
setTicker
(
bundle
.
getString
(
"ticker"
))
.
setTicker
(
bundle
.
getString
(
"ticker"
))
.
setVisibility
(
NotificationCompat
.
VISIBILITY_PRIVATE
)
.
setVisibility
(
NotificationCompat
.
VISIBILITY_PRIVATE
)
.
setAutoCancel
(
bundle
.
getBoolean
(
"auto_cancel"
,
true
))
.
setAutoCancel
(
bundle
.
getBoolean
(
"auto_cancel"
,
true
))
.
setNumber
(
bundle
.
getInt
(
"number"
))
.
setNumber
(
bundle
.
getInt
(
"number"
))
.
setSubText
(
bundle
.
getString
(
"sub_text"
))
.
setSubText
(
bundle
.
getString
(
"sub_text"
))
.
setGroup
(
bundle
.
getString
(
"group"
))
.
setGroup
(
bundle
.
getString
(
"group"
))
.
setVibrate
(
new
long
[]{
0
,
DEFAULT_VIBRATION
})
.
setVibrate
(
new
long
[]{
0
,
DEFAULT_VIBRATION
})
.
setExtras
(
bundle
.
getBundle
(
"data"
));
.
setExtras
(
bundle
.
getBundle
(
"data"
));
if
(
bundle
.
containsKey
(
"ongoing"
)
&&
bundle
.
getBoolean
(
"ongoing"
))
{
if
(
bundle
.
containsKey
(
"ongoing"
)
&&
bundle
.
getBoolean
(
"ongoing"
))
{
notification
.
setOngoing
(
bundle
.
getBoolean
(
"ongoing"
));
notification
.
setOngoing
(
bundle
.
getBoolean
(
"ongoing"
));
...
@@ -198,10 +198,7 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
...
@@ -198,10 +198,7 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
int
notificationID
=
bundle
.
containsKey
(
"id"
)
?
bundle
.
getString
(
"id"
,
""
).
hashCode
()
:
(
int
)
System
.
currentTimeMillis
();
int
notificationID
=
bundle
.
containsKey
(
"id"
)
?
bundle
.
getString
(
"id"
,
""
).
hashCode
()
:
(
int
)
System
.
currentTimeMillis
();
PendingIntent
pendingIntent
=
PendingIntent
.
getActivity
(
mContext
,
notificationID
,
intent
,
PendingIntent
pendingIntent
=
PendingIntent
.
getActivity
(
mContext
,
notificationID
,
intent
,
PendingIntent
.
FLAG_UPDATE_CURRENT
);
PendingIntent
.
FLAG_UPDATE_CURRENT
);
NotificationManager
notificationManager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
notification
.
setContentIntent
(
pendingIntent
);
notification
.
setContentIntent
(
pendingIntent
);
...
@@ -209,9 +206,9 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
...
@@ -209,9 +206,9 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
if
(
bundle
.
containsKey
(
"tag"
))
{
if
(
bundle
.
containsKey
(
"tag"
))
{
String
tag
=
bundle
.
getString
(
"tag"
);
String
tag
=
bundle
.
getString
(
"tag"
);
notificationManager
.
notify
(
tag
,
notificationID
,
info
);
NotificationManagerCompat
.
from
(
mContext
)
.
notify
(
tag
,
notificationID
,
info
);
}
else
{
}
else
{
notificationManager
.
notify
(
notificationID
,
info
);
NotificationManagerCompat
.
from
(
mContext
)
.
notify
(
notificationID
,
info
);
}
}
}
}
//clear out one time scheduled notification once fired
//clear out one time scheduled notification once fired
...
...
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