0

Am trying to getting pending intent. Its returning intent fine when i click notification. But it not returning any thing when i open my app through launcher icon its not returning the pending intent. I searched many links but am not get anything.

creating pending intent from service

Intent in = new Intent(this, MainActivity.class);
in.setAction(action);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
in.putExtras(bundle);

PendingIntent pendIntent = PendingIntent.getActivity(this, 0, in,0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

builder.setContentTitle("Wiphone")
       .setContentText(callStatus)
       .setSmallIcon(R.drawable.ic_wifi)
       .setWhen(System.currentTimeMillis())
       .setAutoCancel(false)
       .setOngoing(true)
       .setPriority(Notification.FLAG_FOREGROUND_SERVICE)
       .setContentIntent(pendIntent);

Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(10, notification);       

getting pending intent in activity when user launch app through launcher

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 Intent alarmIntent = new Intent();
 alarmIntent.setAction(action);
 PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, alarmIntent,
                                PendingIntent.FLAG_NO_CREATE);
 if (pendingIntent != null) {
     Log.d(TAG, "has pending intent");
    } 
 else {
     Log.d(TAG, "no pending intent");
    }
}
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
user2634966
  • 179
  • 1
  • 16
  • I assume the ACTION you are using is the same in both cases. I assume you want to get the `PendingIntent` in your `Activity` while the `Notification` is still active (the `PendingIntent` will be deleted when the `Notification` is no longer active). Try using the application `Context` (`getApplicationContext()`) when calling `PendingIntent.getActivity()` in both cases. – David Wasser Jan 08 '17 at 20:56
  • @DavidWasser i tried as per suggestion still like same, no luck :( – user2634966 Jan 11 '17 at 05:36
  • Why are you trying to get the `PendingIntent` anyway? This is probably the wrong approach. Please explain. – David Wasser Jan 11 '17 at 07:16
  • I generating this pending intent in service which is foreground process. there am keeping important states of app. so i kept my data in the bundle and create pending intent. if user removed app from recent, on user opening the app (through launcher not from notification) i need those data to check the status of app. so i tried above approach. correct me if am wrong. – user2634966 Jan 11 '17 at 08:13
  • Keeping state in a `Bundle` in `PendingIntent` is the wrong approach. Use shared preferences. – David Wasser Jan 11 '17 at 08:53
  • okay i will use shared preferences. But have i can check still the pending intent is available or not. – user2634966 Jan 11 '17 at 09:00
  • You can if you want. But you say it isn't working for you. – David Wasser Jan 11 '17 at 11:49

0 Answers0