@@ -73,126 +73,142 @@ public void run() {
73
73
*
74
74
* Example: {"data":{"message":"Notification Hub test notification"}}
75
75
*/
76
- public static void sendNotification (Context context , Bundle bundle , String notificationChannelID ) {
77
- try {
78
- Class intentClass = ReactNativeUtil .getMainActivityClass (context );
79
- if (intentClass == null ) {
80
- Log .e (TAG , ERROR_NO_ACTIVITY_CLASS );
81
- return ;
82
- }
83
-
84
- String message = bundle .getString (KEY_REMOTE_NOTIFICATION_MESSAGE );
85
- if (message == null ) {
86
- message = bundle .getString (KEY_REMOTE_NOTIFICATION_BODY );
87
- }
88
-
89
- if (message == null ) {
90
- Log .e (TAG , ERROR_NO_MESSAGE );
91
- return ;
92
- }
93
-
94
- Resources res = context .getResources ();
95
- String packageName = context .getPackageName ();
96
-
97
- String title = bundle .getString (KEY_REMOTE_NOTIFICATION_TITLE );
98
- if (title == null ) {
99
- ApplicationInfo appInfo = context .getApplicationInfo ();
100
- title = context .getPackageManager ().getApplicationLabel (appInfo ).toString ();
101
- }
102
-
103
- int priority = ReactNativeUtil .getNotificationCompatPriority (
104
- bundle .getString (KEY_REMOTE_NOTIFICATION_PRIORITY ));
105
- NotificationCompat .Builder notificationBuilder = ReactNativeUtil .initNotificationCompatBuilder (
106
- context ,
107
- notificationChannelID ,
108
- title ,
109
- bundle .getString (KEY_REMOTE_NOTIFICATION_TICKER ),
110
- NotificationCompat .VISIBILITY_PRIVATE ,
111
- priority ,
112
- bundle .getBoolean (KEY_REMOTE_NOTIFICATION_AUTO_CANCEL , true ));
113
-
114
- String group = bundle .getString (KEY_REMOTE_NOTIFICATION_GROUP );
115
- if (group != null ) {
116
- notificationBuilder .setGroup (group );
117
- }
118
-
119
- notificationBuilder .setContentText (message );
120
-
121
- String subText = bundle .getString (KEY_REMOTE_NOTIFICATION_SUB_TEXT );
122
- if (subText != null ) {
123
- notificationBuilder .setSubText (subText );
124
- }
125
-
126
- String numberString = bundle .getString (KEY_REMOTE_NOTIFICATION_NUMBER );
127
- if (numberString != null ) {
128
- notificationBuilder .setNumber (Integer .parseInt (numberString ));
129
- }
130
-
131
- int smallIconResId = ReactNativeUtil .getSmallIcon (bundle , res , packageName );
132
- notificationBuilder .setSmallIcon (smallIconResId );
133
-
134
- String largeIcon = bundle .getString (KEY_REMOTE_NOTIFICATION_LARGE_ICON );
135
- int largeIconResId = ReactNativeUtil .getLargeIcon (bundle , largeIcon , res , packageName );
136
- Bitmap largeIconBitmap = BitmapFactory .decodeResource (res , largeIconResId );
137
- if (largeIconResId != 0 && (largeIcon != null || Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP )) {
138
- notificationBuilder .setLargeIcon (largeIconBitmap );
139
- }
140
-
141
- String bigText = bundle .getString (KEY_REMOTE_NOTIFICATION_BIG_TEXT );
142
- if (bigText == null ) {
143
- bigText = message ;
144
- }
145
- notificationBuilder .setStyle (ReactNativeUtil .getBigTextStyle (bigText ));
146
-
147
- // Create notification intent
148
- Intent intent = ReactNativeUtil .createNotificationIntent (context , bundle , intentClass );
149
-
150
- if (!bundle .containsKey (KEY_REMOTE_NOTIFICATION_PLAY_SOUND ) || bundle .getBoolean (KEY_REMOTE_NOTIFICATION_PLAY_SOUND )) {
151
- Uri soundUri = ReactNativeUtil .getSoundUri (context , bundle );
152
- notificationBuilder .setSound (soundUri );
153
- }
154
-
155
- if (bundle .containsKey (KEY_REMOTE_NOTIFICATION_ONGOING )) {
156
- notificationBuilder .setOngoing (bundle .getBoolean (KEY_REMOTE_NOTIFICATION_ONGOING ));
157
- }
158
-
159
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
160
- notificationBuilder .setCategory (NotificationCompat .CATEGORY_CALL );
161
-
162
- String color = bundle .getString (KEY_REMOTE_NOTIFICATION_COLOR );
163
- if (color != null ) {
164
- notificationBuilder .setColor (Color .parseColor (color ));
76
+ public static void sendNotification (final Context context ,
77
+ final Bundle bundle ,
78
+ final String notificationChannelID ) {
79
+ ReactNativeUtil .runInWorkerThread (new Runnable () {
80
+ public void run () {
81
+ try {
82
+ Class intentClass = ReactNativeUtil .getMainActivityClass (context );
83
+ if (intentClass == null ) {
84
+ Log .e (TAG , ERROR_NO_ACTIVITY_CLASS );
85
+ return ;
86
+ }
87
+
88
+ String message = bundle .getString (KEY_REMOTE_NOTIFICATION_MESSAGE );
89
+ if (message == null ) {
90
+ message = bundle .getString (KEY_REMOTE_NOTIFICATION_BODY );
91
+ }
92
+
93
+ if (message == null ) {
94
+ Log .e (TAG , ERROR_NO_MESSAGE );
95
+ return ;
96
+ }
97
+
98
+ Resources res = context .getResources ();
99
+ String packageName = context .getPackageName ();
100
+
101
+ String title = bundle .getString (KEY_REMOTE_NOTIFICATION_TITLE );
102
+ if (title == null ) {
103
+ ApplicationInfo appInfo = context .getApplicationInfo ();
104
+ title = context .getPackageManager ().getApplicationLabel (appInfo ).toString ();
105
+ }
106
+
107
+ int priority = ReactNativeUtil .getNotificationCompatPriority (
108
+ bundle .getString (KEY_REMOTE_NOTIFICATION_PRIORITY ));
109
+ NotificationCompat .Builder notificationBuilder = ReactNativeUtil .initNotificationCompatBuilder (
110
+ context ,
111
+ notificationChannelID ,
112
+ title ,
113
+ bundle .getString (KEY_REMOTE_NOTIFICATION_TICKER ),
114
+ NotificationCompat .VISIBILITY_PRIVATE ,
115
+ priority ,
116
+ bundle .getBoolean (KEY_REMOTE_NOTIFICATION_AUTO_CANCEL , true ));
117
+
118
+ String group = bundle .getString (KEY_REMOTE_NOTIFICATION_GROUP );
119
+ if (group != null ) {
120
+ notificationBuilder .setGroup (group );
121
+ }
122
+
123
+ notificationBuilder .setContentText (message );
124
+
125
+ String subText = bundle .getString (KEY_REMOTE_NOTIFICATION_SUB_TEXT );
126
+ if (subText != null ) {
127
+ notificationBuilder .setSubText (subText );
128
+ }
129
+
130
+ String numberString = bundle .getString (KEY_REMOTE_NOTIFICATION_NUMBER );
131
+ if (numberString != null ) {
132
+ notificationBuilder .setNumber (Integer .parseInt (numberString ));
133
+ }
134
+
135
+ int smallIconResId = ReactNativeUtil .getSmallIcon (bundle , res , packageName );
136
+ notificationBuilder .setSmallIcon (smallIconResId );
137
+
138
+ if (bundle .getString (KEY_REMOTE_NOTIFICATION_AVATAR_URL ) == null ) {
139
+ String largeIcon = bundle .getString (KEY_REMOTE_NOTIFICATION_LARGE_ICON );
140
+ int largeIconResId = ReactNativeUtil .getLargeIcon (bundle , largeIcon , res , packageName );
141
+ Bitmap largeIconBitmap = BitmapFactory .decodeResource (res , largeIconResId );
142
+ if (largeIconResId != 0 && (
143
+ largeIcon != null ||
144
+ Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP )) {
145
+ notificationBuilder .setLargeIcon (largeIconBitmap );
146
+ }
147
+ } else {
148
+ Bitmap largeIconBitmap = ReactNativeUtil .fetchImage (
149
+ bundle .getString (KEY_REMOTE_NOTIFICATION_AVATAR_URL ));
150
+ if (largeIconBitmap != null ) {
151
+ notificationBuilder .setLargeIcon (largeIconBitmap );
152
+ }
153
+ }
154
+
155
+ String bigText = bundle .getString (KEY_REMOTE_NOTIFICATION_BIG_TEXT );
156
+ if (bigText == null ) {
157
+ bigText = message ;
158
+ }
159
+ notificationBuilder .setStyle (ReactNativeUtil .getBigTextStyle (bigText ));
160
+
161
+ // Create notification intent
162
+ Intent intent = ReactNativeUtil .createNotificationIntent (context , bundle , intentClass );
163
+
164
+ if (!bundle .containsKey (KEY_REMOTE_NOTIFICATION_PLAY_SOUND ) || bundle .getBoolean (KEY_REMOTE_NOTIFICATION_PLAY_SOUND )) {
165
+ Uri soundUri = ReactNativeUtil .getSoundUri (context , bundle );
166
+ notificationBuilder .setSound (soundUri );
167
+ }
168
+
169
+ if (bundle .containsKey (KEY_REMOTE_NOTIFICATION_ONGOING )) {
170
+ notificationBuilder .setOngoing (bundle .getBoolean (KEY_REMOTE_NOTIFICATION_ONGOING ));
171
+ }
172
+
173
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
174
+ notificationBuilder .setCategory (NotificationCompat .CATEGORY_CALL );
175
+
176
+ String color = bundle .getString (KEY_REMOTE_NOTIFICATION_COLOR );
177
+ if (color != null ) {
178
+ notificationBuilder .setColor (Color .parseColor (color ));
179
+ }
180
+ }
181
+
182
+ int notificationID = bundle .getString (KEY_REMOTE_NOTIFICATION_ID ).hashCode ();
183
+ PendingIntent pendingIntent = PendingIntent .getActivity (context , notificationID , intent ,
184
+ PendingIntent .FLAG_UPDATE_CURRENT );
185
+ notificationBuilder .setContentIntent (pendingIntent );
186
+
187
+ if (!bundle .containsKey (KEY_REMOTE_NOTIFICATION_VIBRATE ) || bundle .getBoolean (KEY_REMOTE_NOTIFICATION_VIBRATE )) {
188
+ long vibration = bundle .containsKey (KEY_REMOTE_NOTIFICATION_VIBRATION ) ?
189
+ (long ) bundle .getDouble (KEY_REMOTE_NOTIFICATION_VIBRATION ) : DEFAULT_VIBRATION ;
190
+ if (vibration == 0 )
191
+ vibration = DEFAULT_VIBRATION ;
192
+ notificationBuilder .setVibrate (new long []{0 , vibration });
193
+ }
194
+
195
+ // Process notification's actions
196
+ ReactNativeUtil .processNotificationActions (context , bundle , notificationBuilder , notificationID );
197
+
198
+ Notification notification = notificationBuilder .build ();
199
+ NotificationManager notificationManager = (NotificationManager ) context .getSystemService (
200
+ Context .NOTIFICATION_SERVICE );
201
+ if (bundle .containsKey (KEY_REMOTE_NOTIFICATION_TAG )) {
202
+ String tag = bundle .getString (KEY_REMOTE_NOTIFICATION_TAG );
203
+ notificationManager .notify (tag , notificationID , notification );
204
+ } else {
205
+ notificationManager .notify (notificationID , notification );
206
+ }
207
+ } catch (Exception e ) {
208
+ Log .e (TAG , ERROR_SEND_PUSH_NOTIFICATION , e );
165
209
}
166
210
}
167
-
168
- int notificationID = bundle .getString (KEY_REMOTE_NOTIFICATION_ID ).hashCode ();
169
- PendingIntent pendingIntent = PendingIntent .getActivity (context , notificationID , intent ,
170
- PendingIntent .FLAG_UPDATE_CURRENT );
171
- notificationBuilder .setContentIntent (pendingIntent );
172
-
173
- if (!bundle .containsKey (KEY_REMOTE_NOTIFICATION_VIBRATE ) || bundle .getBoolean (KEY_REMOTE_NOTIFICATION_VIBRATE )) {
174
- long vibration = bundle .containsKey (KEY_REMOTE_NOTIFICATION_VIBRATION ) ?
175
- (long ) bundle .getDouble (KEY_REMOTE_NOTIFICATION_VIBRATION ) : DEFAULT_VIBRATION ;
176
- if (vibration == 0 )
177
- vibration = DEFAULT_VIBRATION ;
178
- notificationBuilder .setVibrate (new long []{0 , vibration });
179
- }
180
-
181
- // Process notification's actions
182
- ReactNativeUtil .processNotificationActions (context , bundle , notificationBuilder , notificationID );
183
-
184
- Notification notification = notificationBuilder .build ();
185
- NotificationManager notificationManager = (NotificationManager ) context .getSystemService (
186
- Context .NOTIFICATION_SERVICE );
187
- if (bundle .containsKey (KEY_REMOTE_NOTIFICATION_TAG )) {
188
- String tag = bundle .getString (KEY_REMOTE_NOTIFICATION_TAG );
189
- notificationManager .notify (tag , notificationID , notification );
190
- } else {
191
- notificationManager .notify (notificationID , notification );
192
- }
193
- } catch (Exception e ) {
194
- Log .e (TAG , ERROR_SEND_PUSH_NOTIFICATION , e );
195
- }
211
+ });
196
212
}
197
213
198
214
private ReactNativeNotificationsHandler () {
0 commit comments