I have an Activity that push Notification. If the Activity is running background i want that the notification open the activity in foreground (the same activity, not an another instance). Basically the Activity should open itself in foregroung.
this is my code (in the class MyActivity):
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setPriority(2)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.notifica)
.setLargeIcon(bmp)
.setContentTitle(Titolo)
.setContentText(Testo);
Intent resultIntent = new Intent(this, MyActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
mBuilder.setVibrate(pattern);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
mNotificationManager.notify(mId, mBuilder.build());
I also added this line in the manifest:
android:launchMode="singleTop"
But doesn't work, it create a new instance of MyActivity.
How can i do? tnx
It looks like you set PendingIntent to builder before initializing it.
I compared your code with my working code and the only difference which I see is that your context is not the same as mine.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Could you try to replace getBaseContext() with the Context from FirebaseMessagingService? The FirebaseMessagingService extends Service, which has a Context.
Related
I am developing an android app with GCM push notification ,in which whenever the notification opens the page it clears all activities from backstack.
Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
long[] vibrate = {0, 100, 200, 300};
Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Bitmap largIcon = BitmapFactory.decodeResource(this.getResources(),
R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.mipmap.small_icon_notification)
.setContentTitle(getResources().getString(R.string.app_name))
.setLargeIcon(largIcon)
.setSound(notification)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg). setSound(notification)
.setStyle((new NotificationCompat.BigTextStyle()).bigText(msg)) .setAutoCancel(true);
mBuilder.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
To prevent Notification from clearing all activities in backstack, you must setup a Special Activity PendingIntent. A special Activity doesn't need a back stack. Use the manifest to set up the activity task options and create the PendingIntent by calling getActivity().
A PendingIntent specifies an action to take in the future. It lets you pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as your application, whether or not your application is still around when the Intent is eventually invoked
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity).
The following code snippet demonstrates the process:
// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Creates an Intent for the Activity
Intent notifyIntent =
new Intent(new ComponentName(this, ResultActivity.class));
// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Creates the PendingIntent
PendingIntent notifyIntent = PendingIntent.getActivity(this,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);
// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());
Here's a useful link How to Setup a Special Activity PendingIntent:
http://developer.android.com/training/notify-user/navigation.html
From
PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
To
PendingIntent contentIntent = stackBuilder.getPendingIntent(99,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
Just change the requestCode and it works.
Clicking on the notification doesn't launch any activity. I followed the developers code but something isn't working. Any suggestions?
Intent dismissIntent = new Intent(getActivity(), Activity1.class);
dismissIntent.setAction("action1");
PendingIntent piDismiss = PendingIntent.getService(getActivity(), 0, dismissIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Intent notifyIntent = new Intent(getActivity(), Activity2.class);
notifyIntent.setAction("action2");
PendingIntent piNotify = PendingIntent.getService(getActivity(), 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Alert")
.setContentText("Alert text")
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Big text message"))
.addAction(R.drawable.icon1,
"Dismiss", piDismiss)
.addAction(R.drawable.icon2,
"Notify", piNotify);
NotificationManager manager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
I also added android:exported="true" to the activity in the Manifest
I think the problem is that you are calling
PendingIntent.getService()
instead of
PendingIntent.getActivity()
as the doc say:
PendingIntent.getService :
Retrieve a PendingIntent that will start a service, like calling Context.startService(). The start arguments given to the service will come from the extras of the Intent.
PendingIntent getActivity : Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent). Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.
I want to resume main activity when user call app from notification but this code is recreating the main activity.How can I stop it ?
int mId=1;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("My notification")
.setAutoCancel(true)
.setSmallIcon(R.drawable.twitter)
.setContentText("Hello World!")
.setNumber(15);
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
Also I placed android:launchMode="singleTop" to my main activity in my manifest file.How can I resolve this ?
For my app I use this code to do this:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
And in the manifest I set the lunch mode of the MainActivity as singleTask
android:launchMode="singleTask"
I know there are other similar questions, and I've tried them all, WITHOUT success. That's why I'm posting my code here, in case someone can visualize the proper solution for my case and suggest a specific action in the code, please help.
I've tried: Adding some tags to the manifest file in the activity called by the intent, adding flags and actions and categories to the actual intent in the code, creating a dummy activity to be call in the intent with finish(), etc.
Thanks for any suggestion.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.lampp)))
.setAutoCancel(true)
.setContentText(getString(R.string.lampp));
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
Thanks to a combination of the answer posted by #Merlevede and a previous post, this is how it was solved:
Use this code for your Notification:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(getString(R.string.lampp)))
.setAutoCancel(false)
.setContentText(getString(R.string.lampp));
Intent resultIntent = new Intent(this, NotiActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
And this is the code for the dummy Activity called in the intent NotiActivity.class:
public class NotiActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
}
Hope this helps someone out there.
I don't know if this might be useful to you. You might be missing some flags on your notification intent, specially take a look at FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
I use a notification for a service using this code.
Intent notificationIntent = new Intent(this, ActivityMain.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent)
There are a few differences with your code. Also I'm using this notification from a service so I'm using startForeground to set the notification.
It's worth giving it a try.
I'm trying to use notifications in my app, but I've got a problem using following code:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("title")
.setContentText("message");
builder.setOngoing(true);
Intent resultIntent = new Intent(this,StartActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(StartActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(1, builder.build());
When I return to homescreen and click the notification in the status bar StartActivity gets destroyed and created again. But, I just want the Activity to show again like clicking the launcher icon would do in this ,moment. How can I change that?
regards
Try adding
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Before you create the PendingIntent
Source:
How to make notification intent resume rather than making a new intent?
Following worked for me:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("title")
.setContentText("message");
builder.setOngoing(true);
Intent resultIntent = new Intent(this,NotificationActivity.class);///!!!!
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
notificationManager.notify(1, builder.build());
NotificationActivity is special class, which finishs immediately in onCreate() and brings the task of the app to foreground. Source: Notification to restore a task rather than a specific activity?