This is the code for my notification, which when clicked, opens mainactivity by default, but how do I change it? I want to open SecondActivity when it is clicked.
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification.Builder(this)
.setAutoCancel(false)
.setContentIntent(
PendingIntent.getActivity(this, 0, getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("X ADB").setContentText("client in running")
.setOngoing(true).setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker message")
.setWhen(System.currentTimeMillis()).build();
noti.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(111, noti);
Try this code :
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
And pass pendingIntent in setContentIntent() instead of PendingIntent.getActivity(this, 0, getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT
Related
I have a local notification which is set in one of my receivers. Onclick of notification I want to open a specific activity. Below is my code.
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(times, builder.build());
But this is not working. On Click of notification it just removes notification from status bar. Am I missing anything ?
try this :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
instead of this :
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
use this codes before your notification builder code.
now this is the full version :
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Its Your day!!")
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(times, notificationBuilder.build());
try to use this. i hope you will get you desire answer.
UPDATE
here is the sample project : https://github.com/ImtiazDipto01/SimpleNotification
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP as Flag on your `Intent. Like -
notificationIntent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Also provide unique id for notification. like -
int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff);
PendingIntent contentIntent = PendingIntent.getActivity(context, uniqueNotificationId , notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Also on -
manager.notify(uniqueNotificationId , builder.build());
How do I launch an activity when user clicks my notification? I have tried inserting an intent but It is not doing anything-
// **non cleareble notification**//
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification.Builder(this)
.setAutoCancel(false)
.setContentIntent(
PendingIntent.getActivity(this, 0, getIntent(),
PendingIntent.FLAG_UPDATE_CURRENT))
.setContentTitle("HELLO world")
.setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
.setDefaults(Notification.DEFAULT_ALL).setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("ticker message")
.setWhen(System.currentTimeMillis()).build();
noti.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, noti);
Intent intent = new Intent(this, NotificationAction.class);
Do not use Notification.Builder, use NotificationCompat.Builder instead.
Intent intent = new Intent(this, NewActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
And then in your builder:
mBuilder.setContentIntent(pendingIntent);
It's able to show the notification but opens the app in background.
PendingIntent pendingIntent;
Intent intent = new Intent(this, ExpertActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.sendmessageicon);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(largeIcon)
.setSmallIcon(R.drawable.sendmessageicon)
.setContentTitle("Dummy")
.setContentText(messageBody)
.setAutoCancel(true)
.setDefaults(-1)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
Please try this
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, ExpertActivity.class), 0);
Try this one
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);
I need to call some methods after clicking the notification. My notification has a pendingIntent that opens a browser. But I also have to do some other things AFTER user clicks it. Oh, and I must admit that I get my notifications from GCM(push).
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlString));
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(mBitmap)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_ALL)
.setContentText(body)
.setAutoCancel(true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification myNotification = notificationBuilder.build();
myNotification.setLatestEventInfo(getBaseContext(), title, body, pendingIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(TAG, myNotification);
To handle the click on the notification itself (not an action) you need to specify a ContentIntent as following:
notificationBuilder.setContentIntent(PendingIntent.getActivity(
context,
0,
new Intent(context, YourActivity.class)
.putExtra("extra_something", something)
.setData(Uri.parse("pass an uri if needed")),
PendingIntent.FLAG_UPDATE_CURRENT
))
Hope it helps.
I want to open the activity that gave a notification and the user should be directed to the activity if s/he clicks on it. And the notification should disappear.
use this method for open activity in android
// Notification Method
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 Binesh Kumar! (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);
}
1. Create any notification something like
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
2. Now if you want to associate any activity say ResultActivity , create PendingIntent
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
3: Set the pending intent to notification builder
mBuilder.setContentIntent(resultPendingIntent);