-
Notifications
You must be signed in to change notification settings - Fork 763
/
PushNotification.java
255 lines (215 loc) · 9.84 KB
/
PushNotification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.wix.reactnativenotifications.core.notification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import com.facebook.react.bridge.ReactContext;
import com.wix.reactnativenotifications.core.AppLaunchHelper;
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLifecycleFacade.AppVisibilityListener;
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.JsIOHelper;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
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_BACKGROUND_EVENT_NAME;
import static com.wix.reactnativenotifications.Defs.LOGTAG;
public class PushNotification implements IPushNotification {
final protected Context mContext;
final protected AppLifecycleFacade mAppLifecycleFacade;
final protected AppLaunchHelper mAppLaunchHelper;
final protected JsIOHelper mJsIOHelper;
final protected PushNotificationProps mNotificationProps;
final protected AppVisibilityListener mAppVisibilityListener = new AppVisibilityListener() {
@Override
public void onAppVisible() {
mAppLifecycleFacade.removeVisibilityListener(this);
dispatchImmediately();
}
@Override
public void onAppNotVisible() {
}
};
final private String DEFAULT_CHANNEL_ID = "channel_01";
final private String DEFAULT_CHANNEL_NAME = "Channel Name";
public static IPushNotification get(Context context, Bundle bundle) {
Context appContext = context.getApplicationContext();
if (appContext instanceof INotificationsApplication) {
return ((INotificationsApplication) appContext).getPushNotification(context, bundle, AppLifecycleFacadeHolder.get(), new AppLaunchHelper());
}
return new PushNotification(context, bundle, AppLifecycleFacadeHolder.get(), new AppLaunchHelper(), new JsIOHelper());
}
protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper JsIOHelper) {
mContext = context;
mAppLifecycleFacade = appLifecycleFacade;
mAppLaunchHelper = appLaunchHelper;
mJsIOHelper = JsIOHelper;
mNotificationProps = createProps(bundle);
initDefaultChannel(context);
}
@Override
public void onReceived() throws InvalidNotificationException {
if (!mAppLifecycleFacade.isAppVisible()) {
postNotification(null);
notifyReceivedBackgroundToJS();
} else {
notifyReceivedToJS();
}
}
@Override
public void onOpened() {
digestNotification();
}
@Override
public int onPostRequest(Integer notificationId) {
return postNotification(notificationId);
}
@Override
public PushNotificationProps asProps() {
return mNotificationProps.copy();
}
protected int postNotification(Integer notificationId) {
if (mNotificationProps.isDataOnlyPushNotification()) {
return -1;
}
final PendingIntent pendingIntent = NotificationIntentAdapter.createPendingNotificationIntent(mContext, mNotificationProps);;
final Notification notification = buildNotification(pendingIntent);
return postNotification(notification, notificationId);
}
protected void digestNotification() {
if (!mAppLifecycleFacade.isReactInitialized()) {
setAsInitialNotification();
launchOrResumeApp();
return;
}
final ReactContext reactContext = mAppLifecycleFacade.getRunningReactContext();
if (reactContext.getCurrentActivity() == null) {
setAsInitialNotification();
}
if (mAppLifecycleFacade.isAppVisible()) {
dispatchImmediately();
} else if (mAppLifecycleFacade.isAppDestroyed()) {
launchOrResumeApp();
} else {
dispatchUponVisibility();
}
}
protected PushNotificationProps createProps(Bundle bundle) {
return new PushNotificationProps(bundle);
}
protected void setAsInitialNotification() {
InitialNotificationHolder.getInstance().set(mNotificationProps);
}
protected void dispatchImmediately() {
notifyOpenedToJS();
}
protected void dispatchUponVisibility() {
mAppLifecycleFacade.addVisibilityListener(getIntermediateAppVisibilityListener());
// Make the app visible so that we'll dispatch the notification opening when visibility changes to 'true' (see
// above listener registration).
launchOrResumeApp();
}
protected AppVisibilityListener getIntermediateAppVisibilityListener() {
return mAppVisibilityListener;
}
protected Notification buildNotification(PendingIntent intent) {
return getNotificationBuilder(intent).build();
}
protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
final Notification.Builder notification = new Notification.Builder(mContext)
.setContentTitle(mNotificationProps.getTitle())
.setContentText(mNotificationProps.getBody())
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
setUpIcon(notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = mNotificationProps.getChannelId();
NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
notification.setChannelId(channel != null ? channelId : DEFAULT_CHANNEL_ID);
}
return notification;
}
private void setUpIcon(Notification.Builder notification) {
int iconResId = getAppResourceId("notification_icon", "drawable");
if (iconResId != 0) {
notification.setSmallIcon(iconResId);
} else {
notification.setSmallIcon(mContext.getApplicationInfo().icon);
}
setUpIconColor(notification);
}
private void setUpIconColor(Notification.Builder notification) {
int colorResID = getAppResourceId("colorAccent", "color");
if (colorResID != 0) {
int color = mContext.getResources().getColor(colorResID);
notification.setColor(color);
}
}
protected int postNotification(Notification notification, Integer notificationId) {
int id = notificationId != null ? notificationId : createNotificationId(notification);
postNotification(id, notification);
return id;
}
protected void postNotification(int id, Notification notification) {
final NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
}
protected int createNotificationId(Notification notification) {
return (int) System.nanoTime();
}
private void notifyReceivedToJS() {
try {
Bundle bundle = mNotificationProps.asBundle();
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, bundle, mAppLifecycleFacade.getRunningReactContext());
} catch (NullPointerException ex) {
Log.e(LOGTAG, "notifyReceivedToJS: Null pointer exception");
}
}
private void notifyReceivedBackgroundToJS() {
try {
Bundle bundle = mNotificationProps.asBundle();
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME, bundle, mAppLifecycleFacade.getRunningReactContext());
} catch (NullPointerException ex) {
Log.e(LOGTAG, "notifyReceivedBackgroundToJS: Null pointer exception");
}
}
private void notifyOpenedToJS() {
Bundle response = new Bundle();
try {
response.putBundle("notification", mNotificationProps.asBundle());
mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, response, mAppLifecycleFacade.getRunningReactContext());
} catch (NullPointerException ex) {
Log.e(LOGTAG, "notifyOpenedToJS: Null pointer exception");
}
}
protected void launchOrResumeApp() {
if (NotificationIntentAdapter.canHandleTrampolineActivity(mContext)) {
final Intent intent = mAppLaunchHelper.getLaunchIntent(mContext);
mContext.startActivity(intent);
}
}
private int getAppResourceId(String resName, String resType) {
return mContext.getResources().getIdentifier(resName, resType, mContext.getPackageName());
}
private void initDefaultChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager.getNotificationChannels().size() == 0) {
NotificationChannel defaultChannel = new NotificationChannel(
DEFAULT_CHANNEL_ID,
DEFAULT_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(defaultChannel);
}
}
}
}