Android notification to open DialogFragment - android

I Have a class that extends DialogFragment. I want to make my app do a Notification and when the user selects the notification, opens the DialogFragment with its layout and some data.
Is there are any source code to do this?
Hope anyone helps me. Thanks in advance.

To answer, you can't open a dialog via onclick of a notfication, there is no way in android, what you can do is,
to Start activity themed as Dialog
<activity android:theme="#android:style/Theme.Dialog">
now when startActivity() is called, its displayed like dialog, so moove everything you want to display here and call this activity by pending intent.
Also to add further, to make sure your activity wont appear in recent task list you can add the following excludeFromRecents=true

Just make the pendingintent open up one of your activities and have your activity be complete transparent and just open a dialog.
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Related

Can I launch a dialog activity from a notification so it appears over my app's window instead of on a blank background?

I am writing an app with two main activities:
MainActiviy
DialogActivity
MainActivity is a typical application window, and DialogActivity is an activity that I have styled to appear like a dialog by setting android:theme="#style/Theme.AppCompat.Light.Dialog"
From inside MainActivity I can launch the DialogActivity with the following code.
val intent = Intent(this, DialogActivity::class.java)
startActivity(intent)
The dialog window then appears over the main one like this:
In addition to that, I would also like to be able to launch the dialog by tapping on a notification. If I have a different application open and tap the notification the dialog appears on top of the other app, like I want:
However, if I currently have MainActivity open and then tap the notification the dialog appears on top of a blank background:
This happens despite the fact that the dialog appears "on top" of the main activity window in the overview screen:
So here is my question: If I have MainActivity in the foreground and then launch the DialogActivity by tapping on an appropriate notification, can I have the DialogActivity appear on top of the MainActivity window that was open?
In other words when I tap the notification I want it to look like the first picture if the MainActivity is already open in the foreground, like the second picture if another app is in the foreground, and like the third one only if there are no apps currently open.
I am using the following PendingIntent to launch the activity from the notification. Perhaps there is a is a special set of intent flags that do what I want? Or maybe I need to do something more drastic like merging the two separate activities into a single activity?
val intent = Intent(this, DialogActivity::class.java)
intent.flags = 0
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
The screenshots are from an emulator running Android 9 (Api Level 28)
Instead of moving DialogActivity launch application by passing argument along with intent then handle the argument value. Based on that decide launch dialogactivity or not
The key to achieve the result I wanted was to set android:taskAffinity="" in the app manifest. The full steps are explained in this section of the documentation. It might also be worth reading this related question.
In the app manifest, set the taskAffinity to the empty string, so the dialog does not cover the old app window in the overview screen
If desired, set excludefromRecents="true" so the dialog does not appear as a separate entry in the overview. It is effectively dismisse if the user navigates away from it.
<activity
android:name=".DialogActivity"
android:theme="#style/Theme.AppCompat.Light.Dialog"
android:excludeFromRecents="true"
android:taskAffinity="">
</activity>
Pass the NEW_TASK and CLEAR_TASK flags when launching the dialog activity:
val intent = Intent(this, DialogActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

Android: Reopen app when click on notification

This is the code I use to create the PendingIntent for my notification.
Intent notificationIntent = new Intent(context, Activity1.class);
PendingIntent myIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
This PendingIntent launches Activity1 when the notification is click.
How can I simply reopen the app and go to the most recent Activity (as though clicking on the launcher icon) instead of launching a new Activity when the notification is clicked?
Activity1 is just an example. I have multiple Activity in the app. I just want to reopen the app and go to the most recent Activity
NOTE: this looks like wrong design for me, because notification should allow user to enter activity that is in context with the notification.
Technically, you can create redirecting activity and your notification intent should launch it when tapped. In its onCreate() you check what activity you want user to be redirected (you can keep this info in SharedPreferences, and each activity would write this info in onCreate() (or make that in your base class if you have it). Then in redirector you call regular startActivity() to go last activity and call finish() to conclude your redirector. Moreover, your redirector activity does not need any layout so add
android:theme="#android:style/Theme.NoDisplay"
to its Manifest entry (of course you also need no call to setContentView())
create Activity1 as a singletask activity , by changing the launchMode of the activity to singleTask...
set your activity to launchMode="singleTop" in your Manifest.xml then use this code instead of what you are using above to reopen the active one:
Title = "YourAppName";
Text = "open";
notificationIntent = new Intent(this, Activity1.class);
cIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), Title, Text, cIntent);
You can change the android:launchMode in the manifest file for the activity targeted by the pending intent.
Typically, you can use singleTop, which will reuse the same instance when the targeted activity is already on top of the task stack (i.e.: Activity is shown before you left your app).
You can also consider SingleTask and SingleInstance, if you want to keep only a single instance of the activity.

finish activity in which a pending intent starts

In my app, i created a custom notification that contain a button that show recent apps(instead of home button long press)
when user open main activity and press home button to go to home screen then drag the notification drawer and click on the button:
Desired result is that the recent apps are shown and the main activity is one of the recent apps.
Actual result is that the recent apps are shown but the main activity is not in them and the main activity resumes.
My code to start "RecentApps (is a dummy class that show recent apps)" class with pending intent
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think that the problem is getBaseContext, so when the main activity is alive and not finished the recent apps are shown but with context is the main activity.
I tried getApplication and getApplicationContext but not working.
I also tried to use flags for "recentAppIntent" but it is not working, It is a half solution to use
recentAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
but it not what i need.
So, my question is "How to finish the main activity in which the pending intent starts".
Thanks in advance, Mostafa
You need to ensure that your MainActivity and your RecentApps don't belong to the same task. The "Recent apps" doesn't actually show recent apps. It shows recent tasks.
To make sure that your RecentApps isn't in the same task as your MainActivity, you can add the following to the <activity> definition in the manifest for RecentApps:
aandroid:taskAffinity=""
Also, when creating the notification, add FLAG_ACTIVITY_NEW_TASK to the Intent, like this:
Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
I think FLAG_ACTIVITY_CLEAR_TOP will help as a flag.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Explanation for Intent flags are available here: link

Activity not closing after being started my Notification

I am experiencing a weird behavior on my android application. When I open my application, I see my DashboardActivity, then I hit home button or back button and my application closes. This is ok. Then I receive a push message and with this push message I create a notification. The notification works fine, I click the notification and it opens my activity, using the code below:
Intent notificationIntent = new Intent(context, BookingOfferActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle b = new Bundle();
b.putSerializable("booking", booking);
notificationIntent.putExtras(b);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Then I execute some task in this BookingOfferActivity activity and call the method finish() to make sure this activity will be finished no matter what. Then I open my application again, but instead of seeing the DashboardActivity I am still seeing BookingOfferActivity.
I have tried the solution proposed here:
Prevent new activity instance after clicking on notification
but it just doesnt work.
Is there a way to force my application to always open on the DashboardActivity?
Thanks
T
That is strange behaviour considering you are calling finish()
Try setting
android:noHistory="true"
android:launchMode="singleInstance"
in the manifest for the BookingOfferActivity
i don't know exactly, but u can try to finish all activities in onStop() method.
in onResume() method start your DashboardActivity.
Try removing SINGLE_TOP from your intent. CLEAR_TOP should be all you want.
From the Android Developer documentation
FLAG_ACTIVITY_SINGLE_TOP -> If set, the activity will not be launched if
it is already running at the top of the history stack.

Modify activity after clicking on a notification

I have a normal notification system that looks like this:
Notification notification new Notification(
R.drawable.alerts_notification,
alertTitle,
System.currentTimeMillis());
Intent intent = new Intent(mContext, MyActivity.class);
intent.setAction(MyActivity.ONE_ACTION);
PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
mNotifMan.notify(ID, notification);
Notice that I'm using ONE_ACTION as the action of the intent. What I do is verify the action on the activity and select one of the tabs (it's a TabActivity).
All that works fine if the activity is closed, because the Intent will open the activity and then I will decide what to do depending on the action in the Intent. But, if the activity is already opened, it launches a new activity. On the other hand, if I add the flag Intent.FLAG_ACTIVITY_SINGLE_TOP, the activity is not launched twice but I can't the tab is not chosen either.
So, how can choose a tab by clicking on the notification?
Have your intent open your tab activity and put an extra in it denoting the tab. When you detect action resume get a handle on your tab controller and change the tab through code.
OK, I found how to do it... it seems I hadn misread the documentation. What I did was:
Add this to the activity in the AndroidManifest.xml file: android:launchMode="singleInstance"
Overwrite the onNewIntent(Intent intent) method on the activity and put there all the logic to select the tabs etc.
Launch the intent with the flag Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
#schwiz, thanks for your answer though.

Categories

Resources