17
17
import android .os .Build ;
18
18
import android .os .Bundle ;
19
19
import android .support .v4 .app .NotificationCompat ;
20
+ import android .support .v4 .content .LocalBroadcastManager ;
20
21
import android .util .Log ;
21
22
22
23
import com .facebook .react .bridge .ReadableMap ;
23
24
24
25
import org .json .JSONArray ;
25
26
import org .json .JSONException ;
27
+ import org .json .JSONObject ;
26
28
27
29
import com .microsoft .windowsazure .notifications .NotificationsHandler ;
28
30
31
+ import java .util .Set ;
32
+
29
33
public class ReactNativeNotificationsHandler extends NotificationsHandler {
30
- private static final String LOG_TAG = "AzureNotificationHub" ;
34
+ public static final String TAG = "ReactNativeNotificationsHandler" ;
35
+
31
36
private static final long DEFAULT_VIBRATION = 300L ;
32
37
33
38
private Context context ;
@@ -36,6 +41,21 @@ public class ReactNativeNotificationsHandler extends NotificationsHandler {
36
41
public void onReceive (Context context , Bundle bundle ) {
37
42
this .context = context ;
38
43
sendNotification (bundle );
44
+
45
+ JSONObject json = new JSONObject ();
46
+ Set <String > keys = bundle .keySet ();
47
+ for (String key : keys ) {
48
+ try {
49
+ json .put (key , bundle .get (key ));
50
+ } catch (JSONException e ) {
51
+ }
52
+ }
53
+
54
+ LocalBroadcastManager localBroadcastManager = LocalBroadcastManager .getInstance (context );
55
+ Intent event = new Intent (TAG );
56
+ event .putExtra ("event" , ReactNativeNotificationHubModule .DEVICE_NOTIF_EVENT );
57
+ event .putExtra ("data" , json .toString ());
58
+ localBroadcastManager .sendBroadcast (event );
39
59
}
40
60
41
61
private Class getMainActivityClass () {
@@ -58,18 +78,18 @@ private void sendNotification(Bundle bundle) {
58
78
try {
59
79
Class intentClass = getMainActivityClass ();
60
80
if (intentClass == null ) {
61
- Log .e (LOG_TAG , "No activity class found for the notification" );
81
+ Log .e (TAG , "No activity class found for the notification" );
62
82
return ;
63
83
}
64
84
65
85
if (bundle .getString ("message" ) == null ) {
66
- Log .e (LOG_TAG , "No message specified for the notification" );
86
+ Log .e (TAG , "No message specified for the notification" );
67
87
return ;
68
88
}
69
89
70
90
String notificationIdString = bundle .getString ("id" );
71
91
if (notificationIdString == null ) {
72
- Log .e (LOG_TAG , "No notification ID specified for the notification" );
92
+ Log .e (TAG , "No notification ID specified for the notification" );
73
93
return ;
74
94
}
75
95
@@ -211,7 +231,7 @@ private void sendNotification(Bundle bundle) {
211
231
try {
212
232
actionsArray = bundle .getString ("actions" ) != null ? new JSONArray (bundle .getString ("actions" )) : null ;
213
233
} catch (JSONException e ) {
214
- Log .e (LOG_TAG , "Exception while converting actions to JSON object." , e );
234
+ Log .e (TAG , "Exception while converting actions to JSON object." , e );
215
235
}
216
236
217
237
if (actionsArray != null ) {
@@ -224,7 +244,7 @@ private void sendNotification(Bundle bundle) {
224
244
try {
225
245
action = actionsArray .getString (i );
226
246
} catch (JSONException e ) {
227
- Log .e (LOG_TAG , "Exception while getting action from actionsArray." , e );
247
+ Log .e (TAG , "Exception while getting action from actionsArray." , e );
228
248
continue ;
229
249
}
230
250
@@ -249,7 +269,7 @@ private void sendNotification(Bundle bundle) {
249
269
notificationManager .notify (notificationID , info );
250
270
}
251
271
} catch (Exception e ) {
252
- Log .e (LOG_TAG , "failed to send push notification" , e );
272
+ Log .e (TAG , "failed to send push notification" , e );
253
273
}
254
274
}
255
275
0 commit comments