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

feat(android): make foreground notification clickable by using intent #1650

Closed
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 @@ -3,6 +3,7 @@
import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
Expand All @@ -11,6 +12,7 @@
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import com.getcapacitor.*;
import com.getcapacitor.annotation.CapacitorPlugin;
Expand Down Expand Up @@ -237,6 +239,21 @@ public static void sendRemoteMessage(RemoteMessage remoteMessage) {
}
}

@NonNull
private Intent buildIntent() {
Intent intent;
if (getActivity() != null) {
intent = new Intent(getContext(), getActivity().getClass());
} else {
String packageName = getContext().getPackageName();
intent = getContext().getPackageManager().getLaunchIntentForPackage(packageName);
}
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}

public void fireNotification(RemoteMessage remoteMessage) {
JSObject remoteMessageData = new JSObject();

Expand Down Expand Up @@ -277,13 +294,24 @@ public void fireNotification(RemoteMessage remoteMessage) {
if (bundle != null && bundle.getInt("com.google.firebase.messaging.default_notification_icon") != 0) {
pushIcon = bundle.getInt("com.google.firebase.messaging.default_notification_icon");
}

Intent intent = buildIntent();
intent.putExtras(remoteMessage.toIntent().getExtras());
int flags = PendingIntent.FLAG_CANCEL_CURRENT;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
flags = flags | PendingIntent.FLAG_MUTABLE;
}
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, flags);

NotificationCompat.Builder builder = new NotificationCompat.Builder(
getContext(),
NotificationChannelManager.FOREGROUND_NOTIFICATION_CHANNEL_ID
)
.setSmallIcon(pushIcon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager.notify(0, builder.build());
}
Expand Down