Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-notifications
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
冷佳娟
react-native-notifications
Commits
dcdbe3f4
Commit
dcdbe3f4
authored
Nov 29, 2016
by
d4vidi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tesing + more refactoring
parent
c569ebcc
Changes
10
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
267 additions
and
80 deletions
+267
-80
android/src/main/java/com/wix/reactnativenotifications/core/AppLifecycleFacade.java
...wix/reactnativenotifications/core/AppLifecycleFacade.java
+3
-0
android/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java
...ava/com/wix/reactnativenotifications/core/JsIOHelper.java
+26
-0
android/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java
.../reactnativenotifications/core/RNNotificationsModule.java
+1
-1
android/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java
...eactnativenotifications/core/ReactAppLifecycleFacade.java
+10
-0
android/src/main/java/com/wix/reactnativenotifications/core/ReactContextAdapter.java
...ix/reactnativenotifications/core/ReactContextAdapter.java
+0
-51
android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
...tivenotifications/core/notification/PushNotification.java
+9
-9
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java
...ons/core/notificationdrawer/IPushNotificationsDrawer.java
+1
-1
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java
...ions/core/notificationdrawer/PushNotificationsDrawer.java
+1
-1
android/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java
...notifications/core/notification/PushNotificationTest.java
+152
-17
android/src/test/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawerTest.java
.../core/notificationdrawer/PushNotificationsDrawerTest.java
+64
-0
No files found.
android/src/main/java/com/wix/reactnativenotifications/core/AppLifecycleFacade.java
View file @
dcdbe3f4
package
com.wix.reactnativenotifications.core
;
package
com.wix.reactnativenotifications.core
;
import
com.facebook.react.bridge.ReactContext
;
public
interface
AppLifecycleFacade
{
public
interface
AppLifecycleFacade
{
interface
AppVisibilityListener
{
interface
AppVisibilityListener
{
...
@@ -8,6 +10,7 @@ public interface AppLifecycleFacade {
...
@@ -8,6 +10,7 @@ public interface AppLifecycleFacade {
}
}
boolean
isReactInitialized
();
boolean
isReactInitialized
();
ReactContext
getRunningReactContext
();
boolean
isAppVisible
();
boolean
isAppVisible
();
void
addVisibilityListener
(
AppVisibilityListener
listener
);
void
addVisibilityListener
(
AppVisibilityListener
listener
);
void
removeVisibilityListener
(
AppVisibilityListener
listener
);
void
removeVisibilityListener
(
AppVisibilityListener
listener
);
...
...
android/src/main/java/com/wix/reactnativenotifications/core/JsIOHelper.java
0 → 100644
View file @
dcdbe3f4
package
com.wix.reactnativenotifications.core
;
import
android.os.Bundle
;
import
com.facebook.react.bridge.Arguments
;
import
com.facebook.react.bridge.ReactContext
;
import
com.facebook.react.bridge.WritableMap
;
import
com.facebook.react.modules.core.DeviceEventManagerModule
;
public
class
JsIOHelper
{
public
boolean
sendEventToJS
(
String
eventName
,
Bundle
data
,
ReactContext
reactContext
)
{
if
(
reactContext
!=
null
)
{
sendEventToJS
(
eventName
,
Arguments
.
fromBundle
(
data
),
reactContext
);
return
true
;
}
return
false
;
}
public
boolean
sendEventToJS
(
String
eventName
,
WritableMap
data
,
ReactContext
reactContext
)
{
if
(
reactContext
!=
null
)
{
reactContext
.
getJSModule
(
DeviceEventManagerModule
.
RCTDeviceEventEmitter
.
class
).
emit
(
eventName
,
data
);
return
true
;
}
return
false
;
}
}
android/src/main/java/com/wix/reactnativenotifications/core/RNNotificationsModule.java
View file @
dcdbe3f4
...
@@ -80,7 +80,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
...
@@ -80,7 +80,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
@ReactMethod
@ReactMethod
public
void
cancelLocalNotification
(
int
notificationId
)
{
public
void
cancelLocalNotification
(
int
notificationId
)
{
IPushNotificationsDrawer
notificationsDrawer
=
PushNotificationsDrawer
.
get
(
getReactApplicationContext
().
getApplicationContext
());
IPushNotificationsDrawer
notificationsDrawer
=
PushNotificationsDrawer
.
get
(
getReactApplicationContext
().
getApplicationContext
());
notificationsDrawer
.
onNotificationClear
(
notificationId
);
notificationsDrawer
.
onNotificationClear
Request
(
notificationId
);
}
}
@Override
@Override
...
...
android/src/main/java/com/wix/reactnativenotifications/core/ReactAppLifecycleFacade.java
View file @
dcdbe3f4
...
@@ -54,6 +54,16 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade {
...
@@ -54,6 +54,16 @@ public class ReactAppLifecycleFacade implements AppLifecycleFacade {
return
mReactContext
.
hasActiveCatalystInstance
();
return
mReactContext
.
hasActiveCatalystInstance
();
}
}
@Override
public
ReactContext
getRunningReactContext
()
{
ReactContext
reactContext
=
mReactContext
;
if
(
reactContext
==
null
)
{
return
null
;
}
return
mReactContext
;
}
@Override
@Override
public
boolean
isAppVisible
()
{
public
boolean
isAppVisible
()
{
return
mIsVisible
;
return
mIsVisible
;
...
...
android/src/main/java/com/wix/reactnativenotifications/core/ReactContextAdapter.java
deleted
100644 → 0
View file @
c569ebcc
package
com.wix.reactnativenotifications.core
;
import
android.content.Context
;
import
android.os.Bundle
;
import
com.facebook.react.ReactApplication
;
import
com.facebook.react.ReactInstanceManager
;
import
com.facebook.react.ReactNativeHost
;
import
com.facebook.react.bridge.Arguments
;
import
com.facebook.react.bridge.ReactContext
;
import
com.facebook.react.bridge.WritableMap
;
import
com.facebook.react.modules.core.DeviceEventManagerModule
;
public
class
ReactContextAdapter
{
public
ReactContext
getRunningReactContext
(
Context
context
)
{
final
ReactNativeHost
rnHost
=
((
ReactApplication
)
context
.
getApplicationContext
()).
getReactNativeHost
();
if
(!
rnHost
.
hasInstance
())
{
return
null
;
}
final
ReactInstanceManager
instanceManager
=
rnHost
.
getReactInstanceManager
();
final
ReactContext
reactContext
=
instanceManager
.
getCurrentReactContext
();
if
(
reactContext
==
null
||
!
reactContext
.
hasActiveCatalystInstance
())
{
return
null
;
}
return
reactContext
;
}
public
void
sendEventToJS
(
String
eventName
,
Bundle
data
,
Context
context
)
{
final
ReactContext
reactContext
=
getRunningReactContext
(
context
);
if
(
reactContext
!=
null
)
{
sendEventToJS
(
eventName
,
data
,
reactContext
);
}
}
public
void
sendEventToJS
(
String
eventName
,
WritableMap
data
,
Context
context
)
{
final
ReactContext
reactContext
=
getRunningReactContext
(
context
);
if
(
reactContext
!=
null
)
{
sendEventToJS
(
eventName
,
data
,
reactContext
);
}
}
public
void
sendEventToJS
(
String
eventName
,
Bundle
data
,
ReactContext
reactContext
)
{
sendEventToJS
(
eventName
,
Arguments
.
fromBundle
(
data
),
reactContext
);
}
public
void
sendEventToJS
(
String
eventName
,
WritableMap
data
,
ReactContext
reactContext
)
{
reactContext
.
getJSModule
(
DeviceEventManagerModule
.
RCTDeviceEventEmitter
.
class
).
emit
(
eventName
,
data
);
}
}
android/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java
View file @
dcdbe3f4
...
@@ -14,7 +14,7 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityLis
...
@@ -14,7 +14,7 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityLis
import
com.wix.reactnativenotifications.core.InitialNotification
;
import
com.wix.reactnativenotifications.core.InitialNotification
;
import
com.wix.reactnativenotifications.core.NotificationIntentAdapter
;
import
com.wix.reactnativenotifications.core.NotificationIntentAdapter
;
import
com.wix.reactnativenotifications.core.ProxyService
;
import
com.wix.reactnativenotifications.core.ProxyService
;
import
com.wix.reactnativenotifications.core.
ReactContextAdapt
er
;
import
com.wix.reactnativenotifications.core.
JsIOHelp
er
;
import
static
com
.
wix
.
reactnativenotifications
.
Defs
.
NOTIFICATION_OPENED_EVENT_NAME
;
import
static
com
.
wix
.
reactnativenotifications
.
Defs
.
NOTIFICATION_OPENED_EVENT_NAME
;
import
static
com
.
wix
.
reactnativenotifications
.
Defs
.
NOTIFICATION_RECEIVED_EVENT_NAME
;
import
static
com
.
wix
.
reactnativenotifications
.
Defs
.
NOTIFICATION_RECEIVED_EVENT_NAME
;
...
@@ -24,7 +24,7 @@ public class PushNotification implements IPushNotification {
...
@@ -24,7 +24,7 @@ public class PushNotification implements IPushNotification {
final
protected
Context
mContext
;
final
protected
Context
mContext
;
final
protected
AppLifecycleFacade
mAppLifecycleFacade
;
final
protected
AppLifecycleFacade
mAppLifecycleFacade
;
final
protected
AppLaunchHelper
mAppLaunchHelper
;
final
protected
AppLaunchHelper
mAppLaunchHelper
;
final
protected
ReactContextAdapter
mReactContextAdapt
er
;
final
protected
JsIOHelper
mJsIOHelp
er
;
final
protected
PushNotificationProps
mNotificationProps
;
final
protected
PushNotificationProps
mNotificationProps
;
final
protected
AppVisibilityListener
mAppVisibilityListener
=
new
AppVisibilityListener
()
{
final
protected
AppVisibilityListener
mAppVisibilityListener
=
new
AppVisibilityListener
()
{
@Override
@Override
...
@@ -47,14 +47,14 @@ public class PushNotification implements IPushNotification {
...
@@ -47,14 +47,14 @@ public class PushNotification implements IPushNotification {
if
(
appContext
instanceof
INotificationsApplication
)
{
if
(
appContext
instanceof
INotificationsApplication
)
{
return
((
INotificationsApplication
)
appContext
).
getPushNotification
(
context
,
bundle
,
facade
,
appLaunchHelper
);
return
((
INotificationsApplication
)
appContext
).
getPushNotification
(
context
,
bundle
,
facade
,
appLaunchHelper
);
}
}
return
new
PushNotification
(
context
,
bundle
,
facade
,
appLaunchHelper
,
new
ReactContextAdapt
er
());
return
new
PushNotification
(
context
,
bundle
,
facade
,
appLaunchHelper
,
new
JsIOHelp
er
());
}
}
protected
PushNotification
(
Context
context
,
Bundle
bundle
,
AppLifecycleFacade
appLifecycleFacade
,
AppLaunchHelper
appLaunchHelper
,
ReactContextAdapter
reactContextAdapt
er
)
{
protected
PushNotification
(
Context
context
,
Bundle
bundle
,
AppLifecycleFacade
appLifecycleFacade
,
AppLaunchHelper
appLaunchHelper
,
JsIOHelper
JsIOHelp
er
)
{
mContext
=
context
;
mContext
=
context
;
mAppLifecycleFacade
=
appLifecycleFacade
;
mAppLifecycleFacade
=
appLifecycleFacade
;
mAppLaunchHelper
=
appLaunchHelper
;
mAppLaunchHelper
=
appLaunchHelper
;
m
ReactContextAdapter
=
reactContextAdapt
er
;
m
JsIOHelper
=
JsIOHelp
er
;
mNotificationProps
=
createProps
(
bundle
);
mNotificationProps
=
createProps
(
bundle
);
}
}
...
@@ -92,7 +92,7 @@ public class PushNotification implements IPushNotification {
...
@@ -92,7 +92,7 @@ public class PushNotification implements IPushNotification {
return
;
return
;
}
}
final
ReactContext
reactContext
=
m
ReactContextAdapter
.
getRunningReactContext
(
mContext
);
final
ReactContext
reactContext
=
m
AppLifecycleFacade
.
getRunningReactContext
(
);
if
(
reactContext
.
getCurrentActivity
()
==
null
)
{
if
(
reactContext
.
getCurrentActivity
()
==
null
)
{
setAsInitialNotification
();
setAsInitialNotification
();
}
}
...
@@ -159,15 +159,15 @@ public class PushNotification implements IPushNotification {
...
@@ -159,15 +159,15 @@ public class PushNotification implements IPushNotification {
}
}
protected
int
createNotificationId
(
Notification
notification
)
{
protected
int
createNotificationId
(
Notification
notification
)
{
return
(
int
)
System
.
currentTimeMillis
();
return
(
int
)
System
.
nanoTime
();
}
}
private
void
notifyReceivedToJS
()
{
private
void
notifyReceivedToJS
()
{
m
ReactContextAdapter
.
sendEventToJS
(
NOTIFICATION_RECEIVED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mContext
);
m
JsIOHelper
.
sendEventToJS
(
NOTIFICATION_RECEIVED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mAppLifecycleFacade
.
getRunningReactContext
()
);
}
}
private
void
notifyOpenedToJS
()
{
private
void
notifyOpenedToJS
()
{
m
ReactContextAdapter
.
sendEventToJS
(
NOTIFICATION_OPENED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mContext
);
m
JsIOHelper
.
sendEventToJS
(
NOTIFICATION_OPENED_EVENT_NAME
,
mNotificationProps
.
asBundle
(),
mAppLifecycleFacade
.
getRunningReactContext
()
);
}
}
protected
void
launchOrResumeApp
()
{
protected
void
launchOrResumeApp
()
{
...
...
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/IPushNotificationsDrawer.java
View file @
dcdbe3f4
...
@@ -8,5 +8,5 @@ public interface IPushNotificationsDrawer {
...
@@ -8,5 +8,5 @@ public interface IPushNotificationsDrawer {
void
onNewActivity
(
Activity
activity
);
void
onNewActivity
(
Activity
activity
);
void
onNotificationOpened
();
void
onNotificationOpened
();
void
onNotificationClear
(
int
id
);
void
onNotificationClear
Request
(
int
id
);
}
}
android/src/main/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawer.java
View file @
dcdbe3f4
...
@@ -54,7 +54,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer {
...
@@ -54,7 +54,7 @@ public class PushNotificationsDrawer implements IPushNotificationsDrawer {
}
}
@Override
@Override
public
void
onNotificationClear
(
int
id
)
{
public
void
onNotificationClear
Request
(
int
id
)
{
final
NotificationManager
notificationManager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
final
NotificationManager
notificationManager
=
(
NotificationManager
)
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
);
notificationManager
.
cancel
(
id
);
notificationManager
.
cancel
(
id
);
}
}
...
...
android/src/test/java/com/wix/reactnativenotifications/core/notification/PushNotificationTest.java
View file @
dcdbe3f4
This diff is collapsed.
Click to expand it.
android/src/test/java/com/wix/reactnativenotifications/core/notificationdrawer/PushNotificationsDrawerTest.java
0 → 100644
View file @
dcdbe3f4
package
com.wix.reactnativenotifications.core.notificationdrawer
;
import
android.app.NotificationManager
;
import
android.content.Context
;
import
com.facebook.react.bridge.ReactContext
;
import
com.wix.reactnativenotifications.core.AppLaunchHelper
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.MockitoAnnotations
;
import
org.robolectric.RobolectricTestRunner
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
PushNotificationsDrawerTest
{
@Mock
private
ReactContext
mReactContext
;
@Mock
private
Context
mContext
;
@Mock
private
NotificationManager
mNotificationManager
;
@Mock
private
AppLaunchHelper
mAppLaunchHelper
;
@Before
public
void
setup
()
throws
Exception
{
MockitoAnnotations
.
initMocks
(
this
);
when
(
mContext
.
getSystemService
(
Context
.
NOTIFICATION_SERVICE
)).
thenReturn
(
mNotificationManager
);
}
@Test
public
void
onAppInit_clearAllNotifications
()
throws
Exception
{
createUUT
().
onAppInit
();
verify
(
mNotificationManager
).
cancelAll
();
}
@Test
public
void
onAppVisible_clearAllNotifications
()
throws
Exception
{
createUUT
().
onAppVisible
();
verify
(
mNotificationManager
).
cancelAll
();
}
@Test
public
void
onNotificationOpened_clearAllNotifications
()
throws
Exception
{
createUUT
().
onNotificationOpened
();
verify
(
mNotificationManager
).
cancelAll
();
}
@Test
public
void
onNotificationClearRequest_clearSpecificNotification
()
throws
Exception
{
createUUT
().
onNotificationClearRequest
(
666
);
verify
(
mNotificationManager
).
cancel
(
eq
(
666
));
verify
(
mNotificationManager
,
never
()).
cancelAll
();
}
protected
PushNotificationsDrawer
createUUT
()
{
return
new
PushNotificationsDrawer
(
mContext
,
mAppLaunchHelper
);
}
}
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