Removing launcher activity from stack on pendingIntent - android

I have some issue. When my app not in the background (removed from recent) everything works fine. But when my app is in the recent and then I open "ResponseActivity" via notification pending intent, on back click at "ResponseActivity" I getting to my MainActivity (Launcher activity).
I've added FLAG_ACTIVITY_NEW_TASK but it not seem to do it.
Intent intent = new Intent(this, ResponseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(0, "Other", pendingIntent)
.setLargeIcon(getCircleBitmap(bitmap))
.setContentTitle(userDB.getName())
.setContentText(smallText)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
channel.setDescription("");
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
mNotificationManager.createNotificationChannel(channel);
}
mNotificationManager.notify(1000, mBuilder.build());
Manifest:
<application
android:name=".ApplicationContextProvider"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".MyFirebaseInstanceService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".MainActivity" />
<activity android:name=".LoginActivity" />
<activity android:name="verification.MyVerifyPhoneActivity" />
<activity android:name=".ResponseActivity"
android:theme="#style/AppTheme2">
</activity>
</application>

If you want to be sure that only ResponseActivity is in the stack when you select the Notification, you can do this:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
instead of:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

Related

Pending Intent or on click of Notification Does Not Open My desire Activity

please find the code of sendNotfication. I tried lots of things but still not working. Do I need to change in some Mainfest file ?
private void sendNotification(String messageBody) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestID /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(requestID/* ID of notification */, notificationBuilder.build());
}
Please find the mainfest file. This is the same that i found example in firbase. but still when i click on notfication it always went to SplashScreen.
I tried the recent comments but still not working.
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:fullBackupContent="#xml/backup_descriptor"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar">
<activity
android:name=".SplashScreenActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait" />
<!-- [START firebase_service] -->
<service android:name=".FCMTest.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_service] -->
<service
android:name=".FCMTest.MyJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/abc" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<activity android:name="com.example.guptasachin.myapplication.NotificationActivity"></activity>
You are using request Id in many places..
Please check the below working code to open the desired Activity :
Here , activity = CurrentActivity context,
mNotificationId = unique constatnt number like 200,201..e.t.c
Try the below code :
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.transparent_img);
} else {
mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.app_logo);
}
mBuilder.setContentTitle(title)
.setContentText(msg);
Intent i = new Intent();
i.setComponent(new ComponentName(currentActivity.this, DesiredActivity.class));
PendingIntent pIntent = PendingIntent.getActivity(activity, 0, i, 0);
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mNotificationId, mBuilder.build());

android notification is not taking me to the activity when the app is hidden but works when the app is open

I'm unable to go the activity(dosomething.class) when the app is not open but it works when the app's mainactivity.class is open. How can I go to the doactivty.class when the app is running in the background or hidden?
Currently, it takes me the mainactivity.class when I click on the notification also when the mainactivity.class is not shown then the notification sound is not played. I've tried many ways to get it to work but currently nothing is working.
Code:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="com.example.example.MainActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.example.Firebase.MyFirebaseInstanceService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="com.example.example.Firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:exported="true"
android:launchMode="singleTask"
android:name="com.example.example.dosomething"
android:icon="#drawable/logo"
android:label="example"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar" />
<activity
</application>
</manifest>
I tried this :
Intent intent = new Intent(this, dosomething.class);
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)//
.setContentTitle("title")//
.setContentText("text")//
.setStyle(new NotificationCompat.BigTextStyle().bigText("title"))//
.setSmallIcon(R.drawable.logo)//
.setContentIntent(pendingIntent)//
.setTicker("testapp")//
.build();
notification.defaults |= Notification.DEFAULT_ALL;
NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Service.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
and this :
Intent intent = new Intent(getApplicationContext(), dosomething.class);
intent.putExtra("data", tasks);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("title")
.setContentText("text")
.setAutoCancel(true)
.setLights(Color.parseColor("#0086dd"), 2000, 2000);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(dosomething.class);
stackBuilder.addNextIntent(intent);
// PendingIntent contentIntent = PendingIntent.getActivity(this,0, intent,PendingIntent.FLAG_ONE_SHOT);
PendingIntent contentIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
and this :
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this,"channelid");
//Create the intent that’ll fire when the user taps the notification//
Intent intent = new Intent(getApplicationContext(), dosomething.class);
intent.putExtra("data", tasks);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setSmallIcon(R.drawable.logo);
mBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
mBuilder.setContentText(remoteMessage.getNotification().getBody());
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());

Why onDestroy() is get called when I start a notification? How to prevent it?

I have two Activities,ChatActivity and ChatOneActivity
I want to build a notification in the ChatActivity,and when I press the notification, I found the onDestroy() method is called. Why? I can understand why the onStop method getting called, because the view is no longer visible. But onDestroy()? Why? I didn't finish the ChatActivity!
How do I prevent it? I just want the onStop method to get called, I don't want the Activity to get killed.
Here is how I build the notification
private void showNotification(String id, String message) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getApplicationContext())
.setSmallIcon(android.R.drawable.sym_action_email)
.setContentTitle("You have a new message")
.setContentText(message);
Intent intent = new Intent(ChatActivity.this, ChatOneActivity.class);
intent.putExtra("toId", id);
TaskStackBuilder stackBuilder = TaskStackBuilder
.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());
}
And also my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webmobilegroupchat"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.webmobilegroupchat.MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatActivity"
android:label="#string/title_activity_chat" >
</activity>
<activity
android:name="com.example.webmobilegroupchat.ChatOneActivity"
android:label="#string/title_activity_chat_one" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.webmobilegroupchat.ChatActivity" />
</activity>
<activity
android:name="com.example.webmobilegroupchat.SplashActivity"
android:label="#string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Because you called this code:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ChatActivity.this);
stackBuilder.addParentStack(ChatOneActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_CANCEL_CURRENT);
TaskStackBuilder.create will return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.
Replacing them with a regular pendingintent like this solved it:
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

Android deep linking - restore navigation

I have two activities: MainActivity and SettingsActivity. From a notification I launch the SettingsActivity. When I press back -> MainActivity is shown, but I expect MainActivity is not recreated if it was active when a notification comes. Now every time MainActivity is recreated (onCreate() is called)
private void sendNotification(HashMap<String, String> data) {
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra("fcm", data);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SettingsActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setColor(getResources().getColor(R.color.colorNotification))
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(getResources().getString(R.string.notification))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MESSAGE_NOTIFICATION_ID, notificationBuilder.build());
}
Manifest:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:label="#string/settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>

Android: Navigation Drawer not starting detail activity

My manifest file is like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.navigationdrawer" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:label="#string/title_activity_detail"
android:parentActivityName=".MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
>
</activity>
</application>
</manifest>
and the code I am using to start the DetailActivity from MainActivity is
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, builder.build());

Categories

Resources