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
6402f88b
Commit
6402f88b
authored
Nov 30, 2017
by
Libin Lu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use local intents
parent
3a5fed83
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
43 deletions
+47
-43
android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
...rc/main/java/com/evollu/react/fcm/FIRMessagingModule.java
+40
-39
android/src/main/java/com/evollu/react/fcm/InstanceIdService.java
...src/main/java/com/evollu/react/fcm/InstanceIdService.java
+2
-1
android/src/main/java/com/evollu/react/fcm/MessagingService.java
.../src/main/java/com/evollu/react/fcm/MessagingService.java
+3
-2
android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java
.../main/java/com/evollu/react/fcm/SendNotificationTask.java
+2
-1
No files found.
android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
View file @
6402f88b
...
...
@@ -25,6 +25,7 @@ import com.google.firebase.messaging.RemoteMessage.Notification;
import
android.app.Application
;
import
android.os.Bundle
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.support.v4.content.LocalBroadcastManager
;
import
android.util.Log
;
import
android.content.Context
;
...
...
@@ -38,7 +39,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
private
final
static
String
TAG
=
FIRMessagingModule
.
class
.
getCanonicalName
();
private
FIRLocalMessagingHelper
mFIRLocalMessagingHelper
;
private
BadgeHelper
mBadgeHelper
;
public
FIRMessagingModule
(
ReactApplicationContext
reactContext
)
{
super
(
reactContext
);
mFIRLocalMessagingHelper
=
new
FIRLocalMessagingHelper
((
Application
)
reactContext
.
getApplicationContext
());
...
...
@@ -49,12 +50,12 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
registerMessageHandler
();
registerLocalMessageHandler
();
}
@Override
public
String
getName
()
{
return
"RNFIRMessaging"
;
}
@ReactMethod
public
void
getInitialNotification
(
Promise
promise
){
Activity
activity
=
getCurrentActivity
();
...
...
@@ -64,7 +65,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
promise
.
resolve
(
parseIntent
(
activity
.
getIntent
()));
}
@ReactMethod
public
void
requestPermissions
(
Promise
promise
){
if
(
NotificationManagerCompat
.
from
(
getReactApplicationContext
()).
areNotificationsEnabled
()){
...
...
@@ -73,7 +74,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
promise
.
reject
(
null
,
"Notification disabled"
);
}
}
@ReactMethod
public
void
getFCMToken
(
Promise
promise
)
{
try
{
...
...
@@ -84,7 +85,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
promise
.
reject
(
null
,
e
.
getMessage
());
}
}
@ReactMethod
public
void
deleteInstanceId
(
Promise
promise
){
try
{
...
...
@@ -95,19 +96,19 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
promise
.
reject
(
null
,
e
.
getMessage
());
}
}
@ReactMethod
public
void
presentLocalNotification
(
ReadableMap
details
)
{
Bundle
bundle
=
Arguments
.
toBundle
(
details
);
mFIRLocalMessagingHelper
.
sendNotification
(
bundle
);
}
@ReactMethod
public
void
scheduleLocalNotification
(
ReadableMap
details
)
{
Bundle
bundle
=
Arguments
.
toBundle
(
details
);
mFIRLocalMessagingHelper
.
sendNotificationScheduled
(
bundle
);
}
@ReactMethod
public
void
cancelLocalNotification
(
String
notificationID
)
{
mFIRLocalMessagingHelper
.
cancelLocalNotification
(
notificationID
);
...
...
@@ -116,27 +117,27 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
public
void
cancelAllLocalNotifications
()
{
mFIRLocalMessagingHelper
.
cancelAllLocalNotifications
();
}
@ReactMethod
public
void
removeDeliveredNotification
(
String
notificationID
)
{
mFIRLocalMessagingHelper
.
removeDeliveredNotification
(
notificationID
);
}
@ReactMethod
public
void
removeAllDeliveredNotifications
(){
mFIRLocalMessagingHelper
.
removeAllDeliveredNotifications
();
}
@ReactMethod
public
void
subscribeToTopic
(
String
topic
){
FirebaseMessaging
.
getInstance
().
subscribeToTopic
(
topic
);
}
@ReactMethod
public
void
unsubscribeFromTopic
(
String
topic
){
FirebaseMessaging
.
getInstance
().
unsubscribeFromTopic
(
topic
);
}
@ReactMethod
public
void
getScheduledLocalNotifications
(
Promise
promise
){
ArrayList
<
Bundle
>
bundles
=
mFIRLocalMessagingHelper
.
getScheduledLocalNotifications
();
...
...
@@ -146,26 +147,26 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
promise
.
resolve
(
array
);
}
@ReactMethod
public
void
setBadgeNumber
(
int
badgeNumber
)
{
mBadgeHelper
.
setBadgeCount
(
badgeNumber
);
}
@ReactMethod
public
void
getBadgeNumber
(
Promise
promise
)
{
promise
.
resolve
(
mBadgeHelper
.
getBadgeCount
());
}
private
void
sendEvent
(
String
eventName
,
Object
params
)
{
getReactApplicationContext
()
.
getJSModule
(
DeviceEventManagerModule
.
RCTDeviceEventEmitter
.
class
)
.
emit
(
eventName
,
params
);
}
private
void
registerTokenRefreshHandler
()
{
IntentFilter
intentFilter
=
new
IntentFilter
(
"com.evollu.react.fcm.FCMRefreshToken"
);
getReactApplicationContext
(
).
registerReceiver
(
new
BroadcastReceiver
()
{
LocalBroadcastManager
.
getInstance
(
getReactApplicationContext
()
).
registerReceiver
(
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
if
(
getReactApplicationContext
().
hasActiveCatalystInstance
())
{
...
...
@@ -175,13 +176,13 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
},
intentFilter
);
}
@ReactMethod
public
void
send
(
String
senderId
,
ReadableMap
payload
)
throws
Exception
{
FirebaseMessaging
fm
=
FirebaseMessaging
.
getInstance
();
RemoteMessage
.
Builder
message
=
new
RemoteMessage
.
Builder
(
senderId
+
"@gcm.googleapis.com"
)
.
setMessageId
(
UUID
.
randomUUID
().
toString
());
ReadableMapKeySetIterator
iterator
=
payload
.
keySetIterator
();
while
(
iterator
.
hasNextKey
())
{
String
key
=
iterator
.
nextKey
();
...
...
@@ -190,7 +191,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
fm
.
send
(
message
.
build
());
}
private
String
getStringFromReadableMap
(
ReadableMap
map
,
String
key
)
throws
Exception
{
switch
(
map
.
getType
(
key
))
{
case
String:
...
...
@@ -207,18 +208,18 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
throw
new
Exception
(
"Unknown data type: "
+
map
.
getType
(
key
).
name
()
+
" for message key "
+
key
);
}
}
private
void
registerMessageHandler
()
{
IntentFilter
intentFilter
=
new
IntentFilter
(
"com.evollu.react.fcm.ReceiveNotification"
);
getReactApplicationContext
(
).
registerReceiver
(
new
BroadcastReceiver
()
{
LocalBroadcastManager
.
getInstance
(
getReactApplicationContext
()
).
registerReceiver
(
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
if
(
getReactApplicationContext
().
hasActiveCatalystInstance
())
{
RemoteMessage
message
=
intent
.
getParcelableExtra
(
"data"
);
WritableMap
params
=
Arguments
.
createMap
();
WritableMap
fcmData
=
Arguments
.
createMap
();
if
(
message
.
getNotification
()
!=
null
)
{
Notification
notification
=
message
.
getNotification
();
fcmData
.
putString
(
"title"
,
notification
.
getTitle
());
...
...
@@ -233,7 +234,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
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
();
...
...
@@ -242,16 +243,16 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
}
sendEvent
(
"FCMNotificationReceived"
,
params
);
}
}
},
intentFilter
);
}
private
void
registerLocalMessageHandler
()
{
IntentFilter
intentFilter
=
new
IntentFilter
(
"com.evollu.react.fcm.ReceiveLocalNotification"
);
getReactApplicationContext
(
).
registerReceiver
(
new
BroadcastReceiver
()
{
LocalBroadcastManager
.
getInstance
(
getReactApplicationContext
()
).
registerReceiver
(
new
BroadcastReceiver
()
{
@Override
public
void
onReceive
(
Context
context
,
Intent
intent
)
{
if
(
getReactApplicationContext
().
hasActiveCatalystInstance
())
{
...
...
@@ -260,7 +261,7 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
}
},
intentFilter
);
}
private
WritableMap
parseIntent
(
Intent
intent
){
WritableMap
params
;
Bundle
extras
=
intent
.
getExtras
();
...
...
@@ -277,30 +278,30 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
WritableMap
fcm
=
Arguments
.
createMap
();
fcm
.
putString
(
"action"
,
intent
.
getAction
());
params
.
putMap
(
"fcm"
,
fcm
);
params
.
putInt
(
"opened_from_tray"
,
1
);
return
params
;
}
@Override
public
void
onHostResume
()
{
mFIRLocalMessagingHelper
.
setApplicationForeground
(
true
);
}
@Override
public
void
onHostPause
()
{
mFIRLocalMessagingHelper
.
setApplicationForeground
(
false
);
}
@Override
public
void
onHostDestroy
()
{
}
@Override
public
void
onActivityResult
(
Activity
activity
,
int
requestCode
,
int
resultCode
,
Intent
data
)
{
}
@Override
public
void
onNewIntent
(
Intent
intent
){
sendEvent
(
"FCMNotificationReceived"
,
parseIntent
(
intent
));
...
...
android/src/main/java/com/evollu/react/fcm/InstanceIdService.java
View file @
6402f88b
...
...
@@ -2,6 +2,7 @@ package com.evollu.react.fcm;
import
android.content.Intent
;
import
android.os.Bundle
;
import
android.support.v4.content.LocalBroadcastManager
;
import
android.util.Log
;
import
com.google.firebase.iid.FirebaseInstanceId
;
...
...
@@ -29,6 +30,6 @@ public class InstanceIdService extends FirebaseInstanceIdService {
Bundle
bundle
=
new
Bundle
();
bundle
.
putString
(
"token"
,
refreshedToken
);
i
.
putExtras
(
bundle
);
sendBroadcast
(
i
);
LocalBroadcastManager
.
getInstance
(
getApplicationContext
()).
sendBroadcast
(
i
);
}
}
android/src/main/java/com/evollu/react/fcm/MessagingService.java
View file @
6402f88b
...
...
@@ -5,6 +5,7 @@ import android.content.Intent;
import
android.os.Bundle
;
import
android.os.Handler
;
import
android.os.Looper
;
import
android.support.v4.content.LocalBroadcastManager
;
import
android.util.Log
;
import
com.facebook.react.ReactApplication
;
...
...
@@ -41,12 +42,12 @@ public class MessagingService extends FirebaseMessagingService {
ReactContext
context
=
mReactInstanceManager
.
getCurrentReactContext
();
// If it's constructed, send a notification
if
(
context
!=
null
)
{
context
.
sendOrderedBroadcast
(
message
,
null
);
LocalBroadcastManager
.
getInstance
(
getApplicationContext
()).
sendBroadcast
(
message
);
}
else
{
// Otherwise wait for construction, then send the notification
mReactInstanceManager
.
addReactInstanceEventListener
(
new
ReactInstanceManager
.
ReactInstanceEventListener
()
{
public
void
onReactContextInitialized
(
ReactContext
context
)
{
context
.
sendOrderedBroadcast
(
message
,
null
);
LocalBroadcastManager
.
getInstance
(
getApplicationContext
()).
sendBroadcast
(
message
);
}
});
if
(!
mReactInstanceManager
.
hasStartedCreatingInitialContext
())
{
...
...
android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java
View file @
6402f88b
...
...
@@ -17,6 +17,7 @@ import android.os.Build;
import
android.os.Bundle
;
import
android.support.v4.app.NotificationCompat
;
import
android.support.v4.app.NotificationManagerCompat
;
import
android.support.v4.content.LocalBroadcastManager
;
import
android.util.Log
;
import
java.io.IOException
;
...
...
@@ -191,7 +192,7 @@ public class SendNotificationTask extends AsyncTask<Void, Void, Void> {
Log
.
d
(
TAG
,
"broadcast intent if it is a scheduled notification"
);
Intent
i
=
new
Intent
(
"com.evollu.react.fcm.ReceiveLocalNotification"
);
i
.
putExtras
(
bundle
);
mContext
.
sendOrderedBroadcast
(
i
,
null
);
LocalBroadcastManager
.
getInstance
(
mContext
).
sendBroadcast
(
i
);
}
if
(!
mIsForeground
||
bundle
.
getBoolean
(
"show_in_foreground"
)){
...
...
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