onActivityResult for PendingIntent - android

How do i get the result of an activity started from NotificationManager?
In other words i need to get the resultCode from a PendingIntent.
public void test(Context context){
Notification notification = new Notification(null, "text", System.currentTimeMillis());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.example.com"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, "text", "text", pendingIntent);
notificationManager.notify(0, notification);
}
I want to be notified when the browser's activity ends.
OBS: This code is outside an activity, that's why it recieves the
context as parameter

Let's assume you have activity A that sets PendingIntent. This PendingIntent calls activity B. You want receive result of B.
You can do this by introducing proxy activity: A -> PendingIntent -> ProxyActivity --> startActivityForResult --> B. This way you'll receive result from B into your ProxyActivity activity.
Note that you should call startActivityForResult() in ProxyActivity.onCreate().

Let's see in simply way ,
IN Activity A :
intet.putStringExtra("from notification") ;
In Activity B:
if (getintent().getStringExtra("from notification") !=null ){
//TODO do what u want !
}

Related

How do I update Activity with intents passed via Notification

I have created a custom notification, but when i pause the activity with the home button and a notification arrives, i press the notification and it creates a new activity and doesnt resume the previews activity and when i press the back button it goes to the previews one which is the same window. I have tried singleTop, singleTask, singleIntent, it works but it doesnt update the activity when a message enters, it is like the previews activity was in pause. how can i fix it?
if there is no solution i though in resuming the activity or destroy the paused activity or maybe restart the activity, is there a way to do this?
public void CustomNotification(String strtext) {
// Using RemoteViews to bind custom layouts into Notification
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.customnotification);
// Set Notification Title
String strtitle = getString(R.string.customnotificationtitle);
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
intent.putExtra("String T", "");
//intent.putExtra("Integer C", 0);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.drawable.ic_launcher)
// Set Ticker Message
.setTicker(getString(R.string.customnotificationticker))
// Dismiss Notification
.setAutoCancel(true)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Set RemoteViews into Notification
.setContent(remoteViews);
// Locate and set the Image into customnotificationtext.xml ImageViews
remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);
// Locate and set the Text into customnotificationtext.xml TextViews
remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
remoteViews.setTextViewText(R.id.text, strtext);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
LOGS:
04-29 01:29:10.453 29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376 29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981 29501-29501/com.example.example E/STATE﹕ RESUMING
WHEN I PRESS THE NOTIFICATION:
04-29 01:33:09.654 33449-33530/com.example.example E/STATE﹕NEW ACTIVITY
I will appreciate any answer, Thanks!
EDITED
ANSWER:
the answer was to add:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
because when i was in the same window and i pressed the home button, the activity was in paused, then if some notification arrived it brought me to that window but didnt updated the messages so I needed to create a new intent and erase the previous one which was in pause. so that code did the Job.
To update your activity, you should override onNewIntent():
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
//reload your data here
}
Android Docs say:
void onNewIntent (Intent intent)
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag
when calling startActivity(Intent). In either case, when the activity
is re-launched while at the top of the activity stack instead of a new
instance of the activity being started, onNewIntent() will be called
on the existing instance with the Intent that was used to re-launch
it.
An activity will always be paused before receiving a new intent, so
you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use
setIntent(Intent) to update it to this new Intent.
Note: as the docs say, you need to set android:launchMode="singleTop" in your Activity tag (in AndroidManifest).
try this in your service code to use notification :
private static final int NOTIFY_ME_ID = 1222;
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
private void Notifi(String message){
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(Intent.ACTION_MAIN);
//CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setComponent(new ComponentName("packagenameofyourapplication",
"packagenameofyourapplication.NotificationView"));
PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notif = new Notification.Builder(this)
.setContentTitle(getString(R.string.notification_titel)).setContentText(message)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
.setContentIntent(pintent).build();
notif.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(NOTIFY_ME_ID, notif);
}

Which function will be called on starting an activity with a click on notification with a pending intent if the activity is already running

I want to open an activity when user clicking on a notification. I can do it with a pending intent which works fine even if the activity already running. But I want to do some thing on the click operation. now I am doing that in onCreate method. it only works when the activity not running. I tried with onNewIntent method which is not reaching on above condition. What I can do now ?
NOTIFICATION_ID => A number used to identify the notification to do further operations.
mContext => Application context.
ResultActivity => Activity to be opened.
//creating intent
Intent resultIntent = new Intent(mContext, ResultActivity.class);
resultIntent.putExtra("update_request", true);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//creating pending intent
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
//Build the notification using Notification.Builder
Notification.Builder builder = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentTitle("Title")
.setContentIntent(resultPendingIntent) //adding pending intent
.setContentText("content");
NotificationManager mNotificationManager =
(NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
//Get current notification
Notification mNotification = builder.getNotification();
//Show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
Now you can do your operations on protected void onNewIntent(Intent intent) function of the Resulting activity.
And you also can set the new intent as intent of the activity as setIntent(intent) from the newIntent function.
Use PendingIntent.FLAG_UPDATE_CURRENT flag in pending intent to update the current activity. And then implement your login in onResume()

How to check if Activity start from notification

I have problem my MainActivity can be created by 3 ways:
1) standart launch App
2) from Service
3) from notification click.
How I can check when it starts from notification click?
Notification code:
private void createNotification()
{
Log.d("service createNotification",MainActivity.TAG);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this,MainActivity.class);
intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(this.getString(R.string.notification_title))
.setContentText(this.getString(R.string.notification_text))
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher);
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
add
intent.putExtra("started_from","notification");
to the code that starts the intent from the notifications, and the same thing to the other startActivity calls just change the value, then inside your activity
String startedFrom = getIntent().getStringExtra("started_from");
for more refer to this question: How do I get extra data from intent on Android?

Determine if Activity is called by a Notification

I am using an Activitiy with various Tabs on it. From a different part of the application, Notifications are created to tell the user that something has changed. I now managed to Call the Activity, when the user clicks on the Notification. But how can i determine wheter a Activity is created the "normal" way during runtime or by clicking on the notification?
(Depending on the notification clicked, i want to forward to another tab instead of showing the main Tab.)
Intent intent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
// TODO: Replace with .Build() for api >= 16
Notification noti = new Notification.Builder(ctx)
.setContentTitle("Notification"
.setContentText(this.getName())
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setAutoCancel(true)
.getNotification();
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
notificationManager.notify(this.getId(), noti);
This successfully calls my MainActivity. But is there some Method that is called when the Activity is triggered by the pendingIntent?
Thought about to define something like this in the Main Activity:
onTriggeredByNotification(Notification noti){
//determinte tab, depending on Notification.
}
Pass a boolean value from notification and check for the same in the onCreate method of the activity.
Intent intent = new Intent(ctx, MainActivity.class);
intent.putExtra("fromNotification", true);
...
if (getIntent().getExtras() != null) {
Bundle b = getIntent().getExtras();
boolean cameFromNotification = b.getBoolean("fromNotification");
}
You can try this in your Notification
Intent intent=new Intent();
intent.setAction("Activity1");
In the Activity override onNewIntent() method and get action so you can determine the activity is called or not.
Better than using the reserved action field of your intent as specified by #ricintech, you could use an extra parameter in your pending intent and detect it in your onCreate method and in your onNewIntent metod inside your activity.

startActivityForResult() on selecting Notification

Is it possible to use startActivityForResult() from a status bar notification?
Say I have an activity A, which on some event starts activity B using startActivityForResult(). Now when it is in the background, on the event it shows a notification. Now on selecting the notification, how do i start activity B for result?
I do realize that activity A should have a service that runs in the background, but i guess the same question would apply even in that case.
Here's the code for the notification. This is in the Activity A.
Notification notification = new Notification(R.drawable.ic_launcher, "New Notification", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "My Notification Title";
CharSequence contentText = "My Notification Text";
Intent notificationIntent = new Intent(this, ActivityB.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
nm.notify(1, notification); //1 = id
I think that you should just start the activity A from the activity B when the activity B is opened by opening the notification and then closed.
You can pass the return value in the intent that you use to start the activity A from the activity B.
Dont use startActivityForResult. You can achieve the same functionality in different way.
Pass the result as an extra with your notification. Get this result in onResume of activity B.

Categories

Resources