Build and issue the notification: 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() .
Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.
See below code. I am using that and it is opening my HomeActivity.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Here's example using NotificationCompact.Builder class which is the recent version to build notification.
private void startNotification() {
Log.i("NextActivity", "startNotification");
// Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotificationManager.notify(mNotificationId, mBuilder.build());
}
Looks like you missed this part,
notification.contentIntent = pendingIntent;
Try adding this and it should work.
Please use below code for complete example of simple notification, in this code you can open application after clicking on Notification, it will solve your problem.
public class AndroidNotifications extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button notificationButton = (Button) findViewById(R.id.notificationButton);
notificationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Notification Title and Message
Notification("Dipak Keshariya (Android Developer)",
"This is Message from Dipak Keshariya (Android Developer)");
}
}, 0);
}
});
}
// Notification Function
private void Notification(String notificationTitle,
String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(
R.drawable.ic_launcher, "Message from Dipak Keshariya! (Android Developer)",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this,
notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
}
And see below link for more information.
Simple Notification Example
Use below code for create notification for open activity. It works for me. For full code
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("Do Something...")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
public void addNotification()
{
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(MainActivity.this);
mBuilder.setSmallIcon(R.drawable.email);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi,This notification for you let me check");
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent conPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(conPendingIntent);
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,mBuilder.build());
Toast.makeText(MainActivity.this, "Notification", Toast.LENGTH_SHORT).show();
}
use this:
Notification mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_music)
.setContentTitle(songName).build();
mBuilder.contentIntent= PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
contentIntent will take care of openning activity when notification clicked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With