Skip to content

Commit 3b2733b

Browse files
fix: notifications
1 parent d89863a commit 3b2733b

File tree

6 files changed

+316
-267
lines changed

6 files changed

+316
-267
lines changed

android/src/main/java/com/hoxfon/react/RNTwilioVoice/CallNotificationManager.java

+30-33
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@
1515
import android.graphics.Color;
1616
import android.os.Build;
1717
import android.os.Bundle;
18+
import android.util.Log;
19+
1820
import androidx.core.app.NotificationCompat;
1921

2022
import com.facebook.react.bridge.ReactApplicationContext;
2123

2224
import java.util.List;
2325

2426
import static android.content.Context.ACTIVITY_SERVICE;
25-
import static com.hoxfon.react.RNTwilioVoice.Constants.ACTION_HANGUP_CALL;
26-
import static com.hoxfon.react.RNTwilioVoice.Constants.ACTION_INCOMING_CALL;
27-
import static com.hoxfon.react.RNTwilioVoice.Constants.ACTION_MISSED_CALL;
28-
import static com.hoxfon.react.RNTwilioVoice.Constants.INCOMING_CALL_NOTIFICATION_ID;
29-
import static com.hoxfon.react.RNTwilioVoice.Constants.CALL_SID_KEY;
30-
import static com.hoxfon.react.RNTwilioVoice.Constants.MISSED_CALLS_GROUP;
31-
import static com.hoxfon.react.RNTwilioVoice.Constants.MISSED_CALLS_NOTIFICATION_ID;
32-
import static com.hoxfon.react.RNTwilioVoice.Constants.HANGUP_NOTIFICATION_ID;
33-
import static com.hoxfon.react.RNTwilioVoice.Constants.PREFERENCE_KEY;
34-
import static com.hoxfon.react.RNTwilioVoice.Constants.ACTION_CLEAR_MISSED_CALLS_COUNT;
35-
import static com.hoxfon.react.RNTwilioVoice.Constants.CLEAR_MISSED_CALLS_NOTIFICATION_ID;
27+
28+
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.TAG;
3629

3730
public class CallNotificationManager {
3831

@@ -73,12 +66,15 @@ public static Class getMainActivityClass(Context context) {
7366
}
7467

7568
public void createMissedCallNotification(ReactApplicationContext context, String callSid, String callFrom) {
76-
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
69+
if (BuildConfig.DEBUG) {
70+
Log.d(TAG, "createMissedCallNotification()");
71+
}
72+
SharedPreferences sharedPref = context.getSharedPreferences(Constants.PREFERENCE_KEY, Context.MODE_PRIVATE);
7773
SharedPreferences.Editor sharedPrefEditor = sharedPref.edit();
7874

7975
Intent intent = new Intent(context, getMainActivityClass(context));
80-
intent.setAction(ACTION_MISSED_CALL)
81-
.putExtra(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID)
76+
intent.setAction(Constants.ACTION_MISSED_CALL)
77+
.putExtra(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.MISSED_CALLS_NOTIFICATION_ID)
8278
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
8379

8480
PendingIntent pendingIntent = PendingIntent.getActivity(
@@ -91,24 +87,24 @@ public void createMissedCallNotification(ReactApplicationContext context, String
9187
PendingIntent clearMissedCallsCountPendingIntent = PendingIntent.getBroadcast(
9288
context,
9389
0,
94-
new Intent(ACTION_CLEAR_MISSED_CALLS_COUNT)
95-
.putExtra(INCOMING_CALL_NOTIFICATION_ID, CLEAR_MISSED_CALLS_NOTIFICATION_ID),
90+
new Intent(Constants.ACTION_CLEAR_MISSED_CALLS_COUNT)
91+
.putExtra(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.CLEAR_MISSED_CALLS_NOTIFICATION_ID),
9692
0
9793
);
9894
/*
9995
* Pass the notification id and call sid to use as an identifier to open the notification
10096
*/
10197
Bundle extras = new Bundle();
102-
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID);
103-
extras.putString(CALL_SID_KEY, callSid);
98+
extras.putInt(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.MISSED_CALLS_NOTIFICATION_ID);
99+
extras.putString(Constants.CALL_SID_KEY, callSid);
104100

105101
/*
106102
* Create the notification shown in the notification drawer
107103
*/
108104
String title = context.getString(R.string.call_missed_title);
109105
NotificationCompat.Builder notification =
110106
new NotificationCompat.Builder(context, VOICE_CHANNEL)
111-
.setGroup(MISSED_CALLS_GROUP)
107+
.setGroup(Constants.MISSED_CALLS_GROUP)
112108
.setGroupSummary(true)
113109
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
114110
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
@@ -122,16 +118,16 @@ public void createMissedCallNotification(ReactApplicationContext context, String
122118
.setDeleteIntent(clearMissedCallsCountPendingIntent)
123119
.setContentIntent(pendingIntent);
124120

125-
int missedCalls = sharedPref.getInt(MISSED_CALLS_GROUP, 0);
121+
int missedCalls = sharedPref.getInt(Constants.MISSED_CALLS_GROUP, 0);
126122
missedCalls++;
127123
if (missedCalls == 1) {
128124
inboxStyle = new NotificationCompat.InboxStyle();
129125
inboxStyle.setBigContentTitle(title);
130126
} else {
131-
inboxStyle.setBigContentTitle(String.valueOf(missedCalls) + context.getString(R.string.call_missed_title_plural));
127+
inboxStyle.setBigContentTitle(String.valueOf(missedCalls) + " " + context.getString(R.string.call_missed_title_plural));
132128
}
133-
inboxStyle.addLine(context.getString(R.string.call_missed_more) +callFrom);
134-
sharedPrefEditor.putInt(MISSED_CALLS_GROUP, missedCalls);
129+
inboxStyle.addLine(context.getString(R.string.call_missed_more) + " " + callFrom);
130+
sharedPrefEditor.putInt(Constants.MISSED_CALLS_GROUP, missedCalls);
135131
sharedPrefEditor.commit();
136132

137133
notification.setStyle(inboxStyle);
@@ -146,13 +142,13 @@ public void createMissedCallNotification(ReactApplicationContext context, String
146142
}
147143

148144
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
149-
notificationManager.notify(MISSED_CALLS_NOTIFICATION_ID, notification.build());
145+
notificationManager.notify(Constants.MISSED_CALLS_NOTIFICATION_ID, notification.build());
150146
}
151147

152148
public void createHangupNotification(ReactApplicationContext context, String callSid, String caller) {
153149
Intent intent = new Intent(context, getMainActivityClass(context));
154-
intent.setAction(ACTION_INCOMING_CALL)
155-
.putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID)
150+
intent.setAction(Constants.ACTION_OPEN_CALL_IN_PROGRESS)
151+
.putExtra(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.HANGUP_NOTIFICATION_ID)
156152
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
157153

158154
PendingIntent pendingIntent = PendingIntent.getActivity(
@@ -165,21 +161,21 @@ public void createHangupNotification(ReactApplicationContext context, String cal
165161
PendingIntent hangupPendingIntent = PendingIntent.getBroadcast(
166162
context,
167163
0,
168-
new Intent(ACTION_HANGUP_CALL)
169-
.putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID),
164+
new Intent(Constants.ACTION_HANGUP_CALL)
165+
.putExtra(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.HANGUP_NOTIFICATION_ID),
170166
PendingIntent.FLAG_UPDATE_CURRENT
171167
);
172168

173169
Bundle extras = new Bundle();
174-
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID);
175-
extras.putString(CALL_SID_KEY, callSid);
170+
extras.putInt(Constants.INCOMING_CALL_NOTIFICATION_ID, Constants.HANGUP_NOTIFICATION_ID);
171+
extras.putString(Constants.CALL_SID_KEY, callSid);
176172

177173
Notification notification;
178174
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
179175
String title = context.getString(R.string.call_in_progress);
180176
String actionText = context.getString(R.string.hangup);
181177
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
182-
notification = new Notification.Builder(context, createChannel(title, notificationManager))
178+
notification = new NotificationCompat.Builder(context, createChannel(title, notificationManager))
183179
.setContentTitle(title)
184180
.setContentText(caller)
185181
.setSmallIcon(R.drawable.ic_call_white_24dp)
@@ -191,6 +187,7 @@ public void createHangupNotification(ReactApplicationContext context, String cal
191187
.addAction(0, actionText, hangupPendingIntent)
192188
.build();
193189
} else {
190+
// noinspection deprecation
194191
notification = new NotificationCompat.Builder(context)
195192
.setContentTitle(title)
196193
.setContentText(caller)
@@ -205,7 +202,7 @@ public void createHangupNotification(ReactApplicationContext context, String cal
205202
.addAction(0, actionText, hangupPendingIntent)
206203
.build();
207204
}
208-
notificationManager.notify(HANGUP_NOTIFICATION_ID, notification);
205+
notificationManager.notify(Constants.HANGUP_NOTIFICATION_ID, notification);
209206
}
210207

211208
@TargetApi(Build.VERSION_CODES.O)
@@ -221,6 +218,6 @@ private String createChannel(String channelName, NotificationManager notificatio
221218

222219
public void removeHangupNotification(ReactApplicationContext context) {
223220
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
224-
notificationManager.cancel(HANGUP_NOTIFICATION_ID);
221+
notificationManager.cancel(Constants.HANGUP_NOTIFICATION_ID);
225222
}
226223
}

android/src/main/java/com/hoxfon/react/RNTwilioVoice/Constants.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ public class Constants {
1414
public static final String CANCELLED_CALL_INVITE = "CANCELLED_CALL_INVITE";
1515
public static final String CANCELLED_CALL_INVITE_ERROR = "CANCELLED_CALL_INVITE_ERROR";
1616
public static final String INCOMING_CALL_NOTIFICATION_ID = "INCOMING_CALL_NOTIFICATION_ID";
17+
public static final String ACTION_OPEN_CALL_INVITE = "com.hoxfon.react.RNTwilioVoice.OPEN_CALL_INVITE";
1718
public static final String ACTION_ACCEPT = "com.hoxfon.react.RNTwilioVoice.ACTION_ACCEPT";
1819
public static final String ACTION_REJECT = "com.hoxfon.react.RNTwilioVoice.ACTION_REJECT";
19-
public static final String ACTION_MISSED_CALL = "MISSED_CALL";
20-
public static final String ACTION_HANGUP_CALL = "HANGUP_CALL";
21-
public static final String ACTION_INCOMING_CALL_NOTIFICATION = "ACTION_INCOMING_CALL_NOTIFICATION";
20+
public static final String ACTION_MISSED_CALL = "MISSED_CALL";
21+
public static final String ACTION_HANGUP_CALL = "HANGUP_CALL";
2222
public static final String ACTION_INCOMING_CALL = "ACTION_INCOMING_CALL";
2323
public static final String ACTION_CANCEL_CALL = "ACTION_CANCEL_CALL";
2424
public static final String ACTION_FCM_TOKEN = "ACTION_FCM_TOKEN";
2525
public static final String ACTION_CLEAR_MISSED_CALLS_COUNT = "CLEAR_MISSED_CALLS_COUNT";
26-
}
26+
public static final String ACTION_OPEN_CALL_IN_PROGRESS = "CALL_IN_PROGRESS";
27+
28+
public static final String CALL_SID = "call_sid";
29+
public static final String CALL_STATE = "call_state";
30+
public static final String CALL_FROM = "call_from";
31+
public static final String CALL_TO = "call_to";
32+
public static final String ERROR = "err";
33+
}

0 commit comments

Comments
 (0)