1

I am trying to execute another app from my app using PendingIntent. I get other apps' pending Intent value and do this (I got pending intent value in String type)

public class NotificationCrawlingService extends NotificationListenerService {

AppDatabase appDatabase;
WordDatabase wordDatabase;
NotificationDatabase notificationDatabase;
Context context;
PackageManager pm;
NotificationEntity ne;
AllFragment allFragment;
RecyclerViewAdapter rv;
List<NotiData> notiData;
NotificationEntity noti;
Notification notification;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    context = getApplicationContext();
    appDatabase = AppDatabase.getAppDatabase(context);
    wordDatabase = WordDatabase.getWordDatabase(context);
    notificationDatabase = NotificationDatabase.getNotificationDatabase(context);
    pm = context.getPackageManager();
    notification = sbn.getNotification();
    Bundle extras = notification.extras;
    String pakage_name = sbn.getPackageName();
    String app_name = findApp_name(pakage_name);
    PendingIntent pendingIntent = notification.contentIntent;


Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(pendingIntent));
        startActivity(intent1);
        

However it won't work. Shows this error in Log

2020-08-17 16:29:27.197 4503-4503/com.example.alimseolap1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.alimseolap1, PID: 4503 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=PendingIntent{78fb01: android.os.BinderProxy@b9c2b85} } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2018) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673) at android.app.Activity.startActivityForResult(Activity.java:4689) at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676) at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234) at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:791) at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:933) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1185) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1173) at com.example.alimseolap1.views.Fragment.AllFragment$2.onReceive(AllFragment.java:170) at androidx.localbroadcastmanager.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313) at androidx.localbroadcastmanager.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7050) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

What might be the problem?

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Hyeon
  • 13
  • 1
  • 4

1 Answers1

0

You can't convert a PendingIntent into a Uri like that. Instead of:

PendingIntent pendingIntent = notification.contentIntent;
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse(pendingIntent));
startActivity(intent1);

You do this:

PendingIntent pendingIntent = notification.contentIntent;
pendingIntent.send();

This launches the Intent that was wrapped in the PendingIntent.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I get red underline on that send() method. – Hyeon Aug 18 '20 at 09:56
  • There's some overloading methods related to send(). Maybe they could solve the problem? – Hyeon Aug 18 '20 at 10:00
  • calling `send()` can throw `PendingIntent.CanceledException`, so you probably need to wrap the call in a `try/catch` block. – David Wasser Aug 18 '20 at 13:03
  • Thank you so much for helping me! I have last question, if I want to store pendingintent in database to redirect user whenever user want to, how do I pass that pendingintent to databae? Only strings and integers can be saved into databases right? – Hyeon Aug 18 '20 at 14:30
  • You can't store a `PendingIntent` in a database. It is just a token pointing to the real thing and you can't get all the data from the real thing. `PendingIntent` is just a transient thing, you can't make it persistent. – David Wasser Aug 18 '20 at 20:16
  • https://play.google.com/store/apps/details?id=com.tenqube.notisave&hl=en_US This application redirects users to a specific activity of 3rd party apps with self stored notifications. How does this work then? – Hyeon Aug 19 '20 at 01:00