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
Related
I have two buttons in my notification which opens the same activity but sending as extra different data. Do I really need to create 2 pending intents and 2 intents? There is maybe some shorten version of this?
Intent intent1 = new Intent(this, ResponseActivity.class); //same
intent1.putExtra("RES","a");
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(this, ResponseActivity.class); //same
intent2.putExtra("RES","b"); //but different
intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.addAction(0, "A", pendingIntent1)
.addAction(0, "B", pendingIntent2)
.setLargeIcon(bitmap)
.setContentTitle(userDB.getName())
.setContentText(smallText)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setColor(getResources().getColor(R.color.colorPrimary))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
You have a problem with your code. Your code will end up setting the same PendingIntent with the same Intent on both buttons. This is because the "extras" are not considered when comparing the Intents to determine if a PendingIntent already exists for a given Intent. In your case, the 2 Intents look the same, so the 2 calls to PendingIntent.getActivity() will return the same PendingIntent.
You need to change your code to look like this:
Intent intent = new Intent(this, ResponseActivity.class); //same
intent.putExtra("RES","a");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent1 = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Update the "extra" in the Intent
intent.putExtra("RES","b"); //but different
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
I used a single Intent to produce 2 different PendingIntents. The key here is to use different values for the requestCode (which is the 2nd parameter to PendingIntent.getActivity()). This ensures that you get 2 different PendingIntents.
Yes
An Intent isn't a call to open an activity, but is actually more of a message to an activity (which in most cases is a message to start the activity).
And so unfortunately, if you want two different messages, you need two different intents.
Considering putEtra() returns the Intent you could shorten your code like this :
public static PendingIntent getPendingIntent(Intent intent) {
return PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Intent intent = new Intent(this, ResponseActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
.addAction(0, "A", getPendingIntent(intent.putExtra("RES","a")))
.addAction(0, "B", getPendingIntent(intent.putExtra("RES","b")))
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.
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 issue is only seen on Lollipop devices and best way to reproduce is:
1. launch the app.
2. Back ground the app.
3. Receive a push notification.
4. Click on the notification and try with 3-4 push notifications.
Below is my code: I have also tried the following:
adding Intent.ACTION_MAIN
Intent.CATEGORY_LAUNCHER to the intent
PendingIntent.FLAG_UPDATE_CURRENT.
android:exported="true" in my AndroidManifest.xml
but nothing works. I am seeing this issue only on Lollipop devices.
Please note I see this issue intermittently. Is this something related to the intent getting cached and not getting delivered. Please help.
PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(payload.getMessage())
.setTicker(payload.getMessage())
.setContentIntent(pendingIntent)
.setSound(soundUri);
private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
int requestID = (int) System.currentTimeMillis();
Intent intent = new Intent(context, DeepLinkActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage(BuildConfig.APPLICATION_ID);
intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}
try adding Intent.FLAG_ACTIVITY_CLEAR_TASK flag in the intent
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this seems to solve my problem
Have you set the pending intent in the notification setContentIntent method?
Try this, it works for me on all api versions
Intent intent = new Intent(this, TestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle(getResources().getString(R.string.notification_title))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
I have GCMIntentService implemented and whenever i get the push notification i need to show
the notification in notification menu and open an activity with some bundle values in the
intent.
I can see the notification in the notification menu but clicking on it just doesn't do anything. Following is the code which i am using :-
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent i = new Intent(this, ChatDetail.class);
Bundle b = new Bundle();
b.putString("my_id", "5356b178b130a74a57019fe9");
b.putString("you_id", youId);
i.putExtras(b);
// PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
// new Intent(this, MainActivity.class), 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
i,PendingIntent.FLAG_CANCEL_CURRENT );
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.pool_my_ride_icon)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(text))
.setContentText(text);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
I can see the notification but when i click on it the notification menu slides up and doesn't
do anything doesn't opens any activity.
If i don't send any bundle in the extras then i can open the activity but when i send
bundle values then i can't open the activity.
Thanks in advance
I'd try to add the extras to the intent one at a time :
i.putExtra("my_id", "5356b178b130a74a57019fe9");
i.putExtra("you_id", youId);
hey try this it worked for me
Intent i = new Intent(this, ChatDetail.class);
Bundle b = new Bundle();
b.putString("my_id", "5356b178b130a74a57019fe9");
b.putString("you_id", youId);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtras(b);
the important point here is to send the additional flags to actually want your intent to be delivered
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Just let me know if it worked for you!
My app is getting called by an intent that is passing information(pendingintent in statusbar) but when I hit the home button and reopen my app by holding the home button it calls the intent again and the same extras are still there...
is there any way to clear the intent? or check whether it has been used before?
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("reportID", reportID);
intent.putExtra("message", message);
intent.putExtra("toast_show", true);
intent.putExtra("detail_open", true);
intent.putExtra("SplashActivity", true);
PendingIntent pendingIntent = PendingIntent
.getActivity(context.getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
// Intent.FLAG_ACTIVITY_NEW_TASK
/* get the system service that manage notification NotificationManager */
NotificationManager notificationManager = (NotificationManager) context
.getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context.getApplicationContext())
.setWhen(when)
.setContentText(message)
.setContentTitle(title)
.setSmallIcon(smalIcon)
.setAutoCancel(true)
.setTicker(message)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
/* Create notification with builder */
Notification notification = notificationBuilder.build();
notificationManager.notify(0, notification);
The problem starts when I receive a notification. If I start the app from the notification, it starts the correct screen and I can use the app => ok. But when I quit the app (with home or back button) it do not appears anymore in the "recent app" list. More precisely:
Does anyone knows how to keep the app in the "recent app" list after being started form a notification?
Update this line and try
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),intent,PendingIntent.FLAG_UPDATE_CURRENT);
What you need is the PendingIntent.FLAG_CANCEL_CURRENT :
If the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it.
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
This will ensure that your latest data will pe present on your Intent.