Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow foreground service notification to open the application #652

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ private void registerPhoneAccount(Context appContext) {
telecomManager.registerPhoneAccount(account);
}

private void sendEventToJS(String eventName, @Nullable WritableMap params) {
public void sendEventToJS(String eventName, @Nullable WritableMap params) {
boolean isBoundToJS = this.reactContext.hasActiveCatalystInstance();
Log.v(TAG, "[RNCallKeepModule] sendEventToJS, eventName: " + eventName + ", bound: " + isBoundToJS + ", hasListeners: " + hasListeners + " args : " + (params != null ? params.toString() : "null"));

Expand Down
12 changes: 12 additions & 0 deletions android/src/main/java/io/wazo/callkeep/VoiceConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Activity;
import android.content.res.Resources;
import android.content.Intent;
import android.content.Context;
Expand Down Expand Up @@ -82,6 +84,7 @@ public class VoiceConnectionService extends ConnectionService {
private static ConnectionRequest currentConnectionRequest;
private static PhoneAccountHandle phoneAccountHandle;
private static String TAG = "RNCallKeep";
private static int NOTIFICATION_ID = -4567;

// Delay events sent to RNCallKeepModule when there is no listener available
private static List<Bundle> delayedEvents = new ArrayList<Bundle>();
Expand Down Expand Up @@ -304,9 +307,18 @@ private void startForegroundService() {
assert manager != null;
manager.createNotificationChannel(chan);

Activity currentActivity = RNCallKeepModule.instance.getCurrentReactActivity();
Intent notificationIntent = new Intent(this, currentActivity.getClass());
Copy link

@carlosmg95 carlosmg95 Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! When I have to display a notification in background, my app crashes because of java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference. I think it happens because there is no currentActivity. There was a similar issue that was fixed with this PR: https://github.com/react-native-webrtc/react-native-callkeep/pull/576/files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes my bad, it's fixed in #659, will be released today.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thank you very much!!

By the way, great library.

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

final int flag = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;

PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, notificationIntent, flag);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setOngoing(true)
.setContentTitle(foregroundSettings.getString("notificationTitle"))
.setContentIntent(pendingIntent)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE);

Expand Down