Make notification launch an activity - android

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);

Related

How do I make my notification launch an activity when clicked?

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

When ever i get notification my application opens in background just show the notification and not open the app?

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);

How can I catch the click on notification?

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.

PendingIntent Notification (OnClick not launch) 23.0.1

I have a problem when I try click my notification in versiĆ³n 23.0.1, (I updated latest version) and my notification not is called, I tried some methods but not work.
What I do wrong ?
long mId = System.currentTimeMillis();
Intent intent = new Intent(this, FragmentNotif.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
/*intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);*/
/*intent.putExtra("test","test1");*/
//PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent , PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)mId , intent , PendingIntent.FLAG_UPDATE_CURRENT);
pendingIntent.cancel();
//pendingIntent.FLAG_CANCEL_CURRENT;
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setWhen(mId)
.setLargeIcon(notificationLargeIconBitmap)//(R.drawable.icona_notificacio)
.setSmallIcon(getNotificationIcon())
.setContentTitle("XX XX")
.setContentText(data.getStringExtra("message"))
.setContentIntent(pendingIntent);
Note my class extends from : GCMBaseIntentService
you try change to source code
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
PendingIntent intent = PendingIntent.getActivity(
your main.this, (int)mId, new Intent(your main.this, your NotificationMessage.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification;
notification = new Notification.Builder(.this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(android.R.drawable.ic_input_add)
.setLargeIcon(bitmap).setWhen(System.currentTimeMillis())
.setSound(Uri.parse(
Environment.getExternalStorageDirectory().getPath()+"/.mp3"))
.build();
2. NotificationCompat.Builder
Notification = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(text)
.setTicker("Notification")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
Please refer to http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

android status bar notyfication icon

I'm trying to set an icon to the status bar, I can not view it as soon as you click on the button.
The problem there is the option to delete. I want to set up so you will not be deleted as long as those entering the application and delete
public void setNotificationToStatusBar(){
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
}
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
/** I ADD THIS **/ n.flags=Notification.FLAG_ONGOING_EVENT;
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,0);
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
This is how I create a notification. Using setOngoing(true) I make it persistent
http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)
Intent intent = new Intent("MY_INTENT");
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Click here")
.setSmallIcon(R.drawable.img2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img1))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
If you want to keep your own way of creating the notification and not use the Notification.Builder, you could add this line of code to edit the flag field of the notification
notification.flags |= Notification.FLAG_ONGOING_EVENT;
you can do like this too
int icon = R.drawable.icon_notification;
String msg= "hey";
Intent notificationIntent = new Intent(context,Activity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setPriority(PRIORITY_LOW)
.setContentText(msg)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

Categories

Resources