I want to open an Activity when I click on the notification from the Status bar. I have seen this answered on StackOverflow but none of these answers work for me. This is my code:
notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setProgress(100, 0, false);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));
//when this notification is clicked and the upload is running, open the upload fragment
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// set intent so it does not start a new activity
PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(intent);
this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());
Manifest.xml
<activity
android:name="com.android.app.MainActivity_"
android:label="#string/app_short_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Finally I have realized that I have to write this:
Intent notificationIntent = new Intent(this, MainActivity_.class);
instead of
Intent notificationIntent = new Intent(this, MainActivity.class);
Thanks a lot for all your answers!
I don't think that you need all this flags and configurations just to open an activity from the PendingIntent. You certainly don't need
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent.FLAG_ONE_SHOT
Remove them and try again.
Furthermore: is the package name of the MainActivity as intended (com.android.app.MainActivity)?
try below solution hope it works for you
Intent intent = new Intent(this,YourActivity....class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent,
PendingIntent.FLAG_ONE_SHOT);
From Notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
.
.
.
notificationBuilder.setContentIntent(blogIntent);
Notification notification = notificationBuilder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification);
You have to add custom receiver in manifest
<receiver
android:name=".IntentReceiver"
android:exported="false">
<intent-filter>
<!-- add notification action here -->
</intent-filter>
</receiver>
In Intent receiver class override on recieve method
public class IntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
//call your activity here
}
}
Use this code
/* Invoking the default notification service */
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Test");
mBuilder.setContentText(text2);
mBuilder.setSmallIcon(R.drawable.icon);
/* Creates an explicit intent for an Activity in your app */
Intent resultIntent = new Intent(this, NotificationListActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationListActivity.class);
/* Adds the Intent that starts the Activity to the top of the stack */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* notificationID allows you to update the notification later on. */
mNotificationManager.notify(9999, mBuilder.build());
Related
I am trying to set up my notification to lead directly to a specific activity. I followed the steps outlined in the official documentation. But clicking the notification only opens the main launcher of the app.
The activity I am trying to launch via the notification is the DetailActivity.
This is how I have set up the activity hierarchy in my manifest.xml
<activity
android:name=".SplashscreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:parentActivityName=".SplashscreenActivity"/>
<activity
android:name=".DetailActivity"
android:parentActivityName=".HomeActivity"/>
In my onMessageReceived method of the FirebaseMessagingService class, I have the following:
Intent resultIntent = new Intent(this, DetailActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager.notify(
NOTIFICATION_ID,
getNotification("title", "text", intent)
);
The getNotification method:
private Notification getNotification(String title, String text, PendingIntent intent) {
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(intent)
.setOngoing(true)
.build();
}
I don't know if this issue is that the activity I am trying to launch is not the direct child of the launcher activity. And I am not sure how to debug this either. Hoping someone has run into this weird issue before!
use this:
Intent notificationIntent = new Intent(this, DetailActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent. FLAG_ONE_SHOT);
and use this pendingIntent in Notification.
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"x")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("your title")
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setPriority(Notification.PRIORITY_HIGH);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
notification code
I am using the FirebaseMessagingService for getting notifications and opening the app upon clickng the notification. But everytime i click the notification the app opens the MainActivity instead of the intended ResultActivity. I also followed docs from the PendingIntent docs and still does the same.
private void createNotification( String messageBody) {
Intent intent = new Intent( this , ResultActivity.class );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
// PendingIntent resultPending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setContentTitle("VERA")
.setContentText(messageBody)
.setAutoCancel( true )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
Here is my Manifest.
<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=".ResultActivity"
android:launchMode="singleTask"
android:excludeFromRecents="true"
android:taskAffinity=""></activity>
EDIT: I tried to pass some extra strings, but the main activity is not even receiving anything. Is it possible that the notif is only triggering its default method to launch the app?
Create an Intent that starts the Activity.
Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.
Create a PendingIntent by calling getActivity().
Like,
Intent notifyIntent = new Intent(this, ResultActivity.class);
// Set the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Then you can pass the PendingIntent to the notification as usual:
NotificationCompat.Builder mNotificationBuilder= new NotificationCompat.Builder(this, "CHANNEL_ID");
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, mNotificationBuilder.build());
Apparently there is no way to trigger the onMessageReceived by using the FCM console only. It will only be triggered if i use other ways to send data messages.
I'm trying to dismiss a notification from an .addAction() without having to open the app. The problem is when the button is pressed nothing happens, the onReceive() method doesn't trigger.
Here is the code on the MainActivity:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", SOMENUMBER);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(t);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(SOMENUMBER, notification.build());
And on other class I have the reciever:
public class Notification extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
And the reciever on the AndroidManifest.xml file:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="io.github.seik.Notification" />
</intent-filter>
</receiver>
Your naming conventions are confusing. Android already has a class called Notification, so you probably shouldn't call your receiver Notification :-(
If MainActivity extends Activity then you need to have a manifest entry for it that looks like this:
<activity android:name=".MainActivity"/>
For your BroadcastReceiver, you need a manifest entry like this:
<receiver android:name=".Notification"
android:exported="true"/>
Since you are using an explicit Intent to launch your BroadcastReceiver, you don't need to provide an <intent-filter> for it. Since the BroadcastReceiver will be started by the NotificationManager, you need to make sure that it is exported.
You then need to create the PendingIntent so that it actually launches your BroadcastReceiver, so change this:
Intent notificationIntent = new Intent(mContext, MainActivity.class);
to this:
Intent notificationIntent = new Intent(mContext, Notification.class);
I am using a notification where I added an "action" which should start an activity.
I have two main activities: Login Activity and MainActivity. When the LoginActivity is started, it automatically logs in the user (if the user was previously logged in), does some other stuff, and then starts MainActivity.
MainActivity is the host for several fragments.
Now when I add LoginActivity like this:
Intent notificationIntent;
notificationIntent = new Intent(getActivity(), MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentp =
PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);
NotificationCompat.Builder nBuilder;
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
nBuilder = new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Smart Share - " + "")
.setLights(Color.BLUE, 500, 500).setContentText("message")
.setAutoCancel(true).setTicker("Notification from smartshare")
.setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
.setSound(alarmSound)
.setAutoCancel(true)
.setContentIntent(intentp);
NotificationManager nNotifyMgr = (NotificationManager) getActivity()
.getSystemService(getActivity().NOTIFICATION_SERVICE);
Notification not = nBuilder.build();
nNotifyMgr.notify(2 + 2, not);
Everything works fine. When I put MainActivity into the notificationIntent, a click on the notification does nothing.
How come?
EDIT:
Snippets from my manifest file:
<activity
android:name="<packagename>.LoginActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateVisible|adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="<packagename>.MainActivity"
android:label="#string/tab_title_mapview"
android:screenOrientation="portrait">
</activity>
try with this dude actually u miss mManager.notify(100, notification);
NotificationManager mManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Tittle", System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getBaseContext().getPackageName(), R.layout.customnotification_alert);
contentView.setProgressBar(R.id.progressBar, 10, 0, false);
contentView.setTextViewText(R.id.text, "text ");
notification.contentView = contentView;
Intent notificationIntent = new Intent(getBaseContext(), NotifyMessage.class);
PendingIntent contentIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mManager.notify(100, notification);
try this :
PendingIntent contentIntent = PendingIntent.getActivity(getActivity(),0,PendingIntent.FLAG_UPDATE_CURRENT| PendingIntent.FLAG_ONE_SHOT);
try this
Notification notification = new Notification(icon, message, when);
notificationIntent = new Intent(getActivity(), LoginActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentp =
PendingIntent.getActivity(getActivity(), 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, notificationIntent );
Had the same problem, I solved it by doing to following:
Add this to your manifest (inside the activity tag)
android:launchMode="singleTop"
And set the following flags to the intent you want to start:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Hope it helps!
I am trying to create an notification that when clicked brings up a message in my application, then when the user clicks the back button, I want it to go back to my main screen.
The problem is right now it creates a brand new instance and all the former data gets lost. What am I doing wrong?
NotificationManager nm = (NotificationManager) getSystemService("notification");
Intent intent = new Intent(this, beerwarn.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(beerwarn.class);
stackBuilder.addNextIntent(intent);
PendingIntent pIntent = stackBuilder.getPendingIntent
(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pIntent)
//Notification noti = new Notification.Builder(this)
.setContentTitle("BAC Level Notice")
.setContentText("Your BAC has dropped below Max Legal BAC")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.beerwarn);
//builder.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, builder.build());
Add the android: launchMode = singleTask or singleInstance in your activity in the androidmanifest.xml
<activity
android:name="yourmainscreenActivity"
android:exported="false"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Here's my suggestion, when you bring up the message screen, clear every other activities.
NotificationManager nm = (NotificationManager) getSystemService("notification");
Intent intent = new Intent(this, beerwarn.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent , PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pIntent)
.setContentTitle("BAC Level Notice")
.setContentText("Your BAC has dropped below Max Legal BAC")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.beerwarn);
nm.notify(1, builder.build());
On your beerwarn.class, overrride the onBackPressed function, check for whether your mainscreen activity is running, assuming is it named MainScreen.class, if not, launch it.
ActivityManager mngr = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);
for (ActivityManager.RunningTaskInfo rti : taskList) {
if (rti.topActivity.getClassName().equals(this.getClass().getName())) {
if (!rti.baseActivity.getClassName().equals(MainScreen.class.getName())) {
Intent i = new Intent(this, MainScreen.class);
i.putExtra("currentTab", getIntent().getIntExtra("currentTab", 1));
startActivity(i);
break;
}
}
}
super.onBackPressed();