I have a notification in my app with the following code:
//Notification Start
notificationManager = (NotificationManager) getSystemService(Context.NOTIFI
public static void notifyUser(Activity activity, String header,
String message) {
NotificationManager notificationManager = (NotificationManager) activity
.getSystemService(Activity.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(
activity.getApplicationContext(), YourActivityToLaunch.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);
stackBuilder.addParentStack(YourActivityToLaunch.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent pIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(activity)
.setContentTitle(header)
.setContentText(message)
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setContentIntent(pIntent).setAutoCancel(true)
.setSmallIcon(drawable.notification_icon).build();
notificationManager.notify(2, notification);
}
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
Use my example...
public void createNotification() {
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"message", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 0, 100, 600, 100, 700};
vibrator.vibrate(pattern, -1);
Intent intent = new Intent(this, Main.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
String sms = getSharedPreferences("SMSPREF", MODE_PRIVATE).getString("incoming", "EMPTY");
notification.setLatestEventInfo(this, "message" ,
sms, activity);
notification.number += 1;
notificationManager.notify(0, notification);
}
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();
}
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);
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);