Android: launch new activity when it not exist - android

I have a service that work in foreground, and when the Main activity launch it the notification is launched with the following code:
private void showNotification() {
Log.i(TAG, LocalService.class.getName() + "showNotification()");
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
int icontouse = R.drawable.icon;
if (app.prefs.getBoolean("prefs_useprivacyicon", false)) {
icontouse = R.drawable.icon_privacy;
}
n = new Notification(icontouse, getString(R.string.voice_pro_ready), System.currentTimeMillis());
n.contentView = getRemoteView();
n.contentIntent = contentIntent;
startForeground(myID, n);
nm.notify(myID, n);
}
my problem is the PendingIntent, if the Main activity was in pause status, then this become in front when click on notify in statusbar, but if the Main activity was closed by finish() this don't become visible.
I need to make possible that if the Activity is in memory than become visible, if not available in memory, then launch a new instance.

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
changed to notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
and try it again

Related

How to know app open from notification or app icon click android?

Actually i want to know app is open from notification click or open from app icon click, i pass some parameter from notification click like.
Intent notificationIntent = new Intent(context, SplashScreen.class);
notificationIntent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
notificationIntent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
notificationIntent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));
and this data get in Splash screen and pass it to the respected activity, and open this activity, when i back press close the app, now app is in background state, when i resume app from background it will show notification activity,
you can check:
if (!wasLaunchedFromRecents() && (myIntentData.getStringExtra("IS_FROM_NOTIFICATION").equals(Utility.NOTI_TYPE_1))) {
// from notification
}
private fun wasLaunchedFromRecents(): Boolean {
return getIntent().flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY === Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
}
Intent intent = new Intent(this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
intent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
intent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));
PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationId + 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this, "my_channel_01").setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(getString(R.string.app_name)).setContentText(remoteMessage.getData().get("title")).setContentIntent(pendingIntent1);
mBuilder1.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder1.setChannelId("my_channel_01");
CharSequence name = "My New Channel"; // The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("my_channel_01", name, importance); //Create Notification Channel
mNotificationManager1.createNotificationChannel(channel);
}
mNotificationManager1.notify((int) System.currentTimeMillis(), mBuilder1.build());
First of all when you send data in intent to splash screen add Flags to intent like below
Intent notificationIntent = new Intent(context, SplashScreen.class);
notificationIntent.putExtra("IS_FROM_NOTIFICATION", Utility.NOTI_TYPE_VERSE);
notificationIntent.putExtra("firebase_node_name", Utility.FIREBASE_AFTERNOON_BIBLE_VERSE_NOTE);
notificationIntent.putExtra("title", context.getResources().getString(R.string.menuAfternoonVerse));
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Then in notification activity onBackPressed don't call finish()

android displaying multiple activity in saved state when notification click

I have a notification on my app. It works in foreground well. When i`m in a other activity and press home button after that i click notification. It opens given activity, but i need to display previous activity in a saved state.
Notification note = new Notification(R.drawable.service_state_0,
getResources().getText(R.string.serviceStarted),
System.currentTimeMillis());
Intent i = new Intent(this, StartingActivity_.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
note.setLatestEventInfo(this,
getResources().getText(R.string.app_name), getServiceStateDescription(CustomService.this), pi);
note.flags |= Notification.FLAG_NO_CLEAR;
note.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, note);
It always shows StartingActivity, the question is that how to call resuming activity when notification is clicked. If the activity has not already been destroyed and i can just call that instance back(resume it) and therefore not needing to load it again and won't need to add another activity to my stack. Is it possible?
I find a solution like open recently activity in Notification:
NotificationManager nm = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.flag = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Intent nIntent = new Intent();
nIntent = getPreviousIntent();
PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
notification.setLatestEventInfo(getContext(), "some Title", "some text", pi);
nm.notify(0,notification);
private Intent getPreviousIntent(Intent newIntent) {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
String myPkgNm = getPackageName();
if (!recentTaskInfos.isEmpty()) {
ActivityManager.RecentTaskInfo recentTaskInfo;
for (int i = 0; i < recentTaskInfos.size(); i++) {
recentTaskInfo = recentTaskInfos.get(i);
if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
newIntent = recentTaskInfo.baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
return newIntent;
}
you have to add <uses-permission android:name="android.permission.GET_TASKS" />

How can i enter in my application through notification?

This is the code of my notification. I want that onClick my application starts. Right now nothing happen
private void CheckNoti(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
service.this);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Context");
notificationBuilder.setTicker("TickerText");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);
Intent notificationIntent = new Intent(this, service.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
mNotificationManager.notify(1,
notificationBuilder.build());
} }
This is a code inside a class. It's not inside the MainActivity. So i can't do something like:
intent.setClassName("your.package.name", "ActivityToLaunch");
i think because i already doing Intent notificationIntent = new Intent(this, service.class);
To bring your app to the foreground if it is running already you need to set different flags on your intent:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.
See the answer here

Android - GCM push notification that sends user to Activity does not result in onCreate call

I am able to create push notifications, and to send the user to whichever Activity I want, but I notice that whenever the user lands on that Activity, the onCreate function does not get called.
Is that supposed to be the case? How do I set it so that the onCreate of the Activity is called?
Here is my Activity:
public class QuestionActivity extends BaseListActivity
{
#Override
public void onCreate(Bundle savedInstanceState)
and here is how the notification is generated:
Intent notificationIntent = new Intent(context, MainActivity.class);
if ( notification_type != null && notification_type.equals("question"))
{
notificationIntent = new Intent(context, QuestionActivity.class);
}
else
if ( notification_type != null && notification_type.equals("plan"))
{
notificationIntent = new Intent(context, TopicActivity.class);
}
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(intent)
.setSmallIcon(icon)
.setLights(Color.YELLOW, 1, 2)
.setAutoCancel(true)
.setSound(defaultSound)
.build();
notificationManager.notify(0, notification);
Thanks!
The flags that you are using Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP mean that if the activity is already running in the current task, then instead of launching a new instance of that activity, the old existing activity will be used. In that case onCreate won't be called, since the activity already exists.

Android notification click, run the main intent?

Hy!
public static void addNotification(String sAppname, String sDescription) {
NotificationManager notifManager = (NotificationManager) mycontext.getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, sDescription, System.currentTimeMillis());
Intent i = new Intent (mycontext, mStart.class)
.setAction(Intent.ACTION_MAIN)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(mycontext, 0, i, 0);
note.setLatestEventInfo(mycontext, sAppname, sDescription, contentIntent);
notifManager.notify(NOTIFY_ID, note);
}
mStart is my first activity
if i click on the notification this code is "re-create" the intent, and show up a new one.
i dont want this. i d like to show up the existing main intent. how to?
The solution is:
in manifest file add the following: singleTask="true"

Categories

Resources