#RequiresApi(api = Build.VERSION_CODES.M)
public void showNotification(int playPauseBtn) {
Intent intent = new Intent(this, ExoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
Intent prevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 0, prevIntent,
PendingIntent.FLAG_MUTABLE);
Intent playIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 0, playIntent,
PendingIntent.FLAG_MUTABLE);
Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0, nextIntent,
PendingIntent.FLAG_MUTABLE);
Bitmap picture = BitmapFactory.decodeResource(getResources(), HelperClass.getlist().get(position).getImg());
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
.setSmallIcon(HelperClass.getlist().get(position).getImg()) //get thumbnail will have small icon pic
.setLargeIcon(picture)
.setContentTitle(HelperClass.getlist().get(position).getName())
.addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", prevPendingIntent)
.addAction(R.drawable.ic_baseline_play_arrow_24, "Play", playPendingIntent)
.addAction(R.drawable.ic_baseline_skip_next_24, "Next", nextPendingIntent)
//.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
//.setMediaSession(mediaSession.getSessionToken()))
.setPriority(Notification.PRIORITY_HIGH)
.setC`your text`ontentIntent(contentIntent)
.setOnlyAlertOnce(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
This code creates Notification wherever this 'showNotification()' function is called but what if I want to shut the app by removing the notification?
use setDeleteIntent method - when notification is removed you can send some broadcast to all your "contextual" ojects (e.g. Activity, Service)
note that there is also setOngoing method, which will stick your notification and user won't be able to remove it "manually". then you can provide "kill" action button for this notification, which may behave exacly same as one created for setDeleteIntent. in that case don't forget to remove notification when user just leave/exit your app, when "kill" action pressed or in any other "usual" way
Related
I'm attempting to make a notification that has two buttons which do two different things by running two different activities. The notification and its buttons appear on the notification bar fine, however pressing on either of the buttons fails to launch the designated activity.
Here is the code:
private void addNotification() {
int requestID = (int) System.currentTimeMillis();
NotificationCompat.Builder builder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ibutton)
.setContentTitle("Timer running..")
.setContentText(timerString);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
//Add buttons to notification
//First make the intent
Intent pauseIntent = new Intent(this, pauseActivity.class);
//Wrap it in pending intent to trigger later
PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Then add the action to your notification
builder.addAction(R.drawable.pause, "Pause", pausePendingIntent);
//Same again for stop button
Intent stopIntent = new Intent(this, stopActivity.class);
PendingIntent stopPendingIntent = PendingIntent.getActivity(this, requestID, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.stop, "Stop", stopPendingIntent);
// Add as notification
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int mNotificationId = 001;
notificationManager.notify(mNotificationId, builder.build());
}
EDIT: An example of one of the activities I am trying to launch:
public class stopActivity {
public stopActivity() {
MainActivity.stopped = true;
}
}
If there is any easier way of simply changing the value of a variable in MainActivity from the button in the notification bar then I am also keen to hear that but as far as I can tell the only option is to launch a new activity
EDIT: An example of one of the activities I am trying to launch:
public class stopActivity { public stopActivity() {
MainActivity.stopped = true; } }
This is not an Activity. An Activity extends Activity!
This is just a POJO class and you cannot start it by putting it in a PendingIntent the way you are doing.
You would be better off by just using an Intent for MainActivity with an "extra" to change a value in it. Here's an example:
Intent pauseIntent = new Intent(this, MainActivity.class);
pauseIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pauseIntent.putExtra("pause", true);
PendingIntent pausePendingIntent = PendingIntent.getActivity(this, requestID, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//Then add the action to your notification
builder.addAction(R.drawable.pause, "Pause", pausePendingIntent);
Then, in MainActivity.onNewIntent():
if (intent.hasExtra("pause")) {
this.paused = true; // whatever you want to do when "pause" is pressed
}
I get notification in my application through BroadcastReceiver, The question is , How can I parse data to an activity and make a dialog with the received data ?
this is my code but when I click on the notification , nothing happens :
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification myNotification = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture)
.setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setContentIntent(pending).build();
notificationManager.notify(1, myNotification);
I've some variablese like link , msg, onvan that contains my data and I need to send these variables to an activity and make a dialog .
How can I do so ?
Try this:
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
just make the PendingIntent open up one of your Activities and have your Activity be complete transparent and just open a Dialog.
EDIT: IF you want to open an Activity from notification click event:
Assuming that notif is your Notification object:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
For more detail visit here. Android - Prompt Dialog window when touch on Notification
You can send data from your notification to another Activity using method putExtra and put this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));
inplace of this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`
I have this code that build notification, i need to remove the small icon on top left screen, i need to ask if there any method that prevent displaying this icon when create a notification
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager =
(NotificationManager) getSystemService(ns);
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.mynotification);
Notification.Builder notification = new Notification.Builder(
getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(R.drawable.ic_launcher)// here how can i make it null or transparent
.setAutoCancel(true)
.setContentIntent(mContentIntent)
.setVibrate(mVibratePattern)
.setContent(notificationView);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(this, switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash,
pendingSwitchIntent);
notificationView.setOnClickPendingIntent(R.id.appName,
pendingSwitchIntent);
Notification not = notification.build();
not.flags=Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(1, not);
i need to leave the notification on status bar but WITHOUT displaying the small icon on top left
okay now i found the solution:O just need to make this
.setSmallIcon(android.R.color.transparent)
I am currently working on a reminders app in which the user gets a notification with the name of the reminder and is then redirected to an activity which contains the text of the reminder in detail.
I am however, only able to redirect to the same activity each time.
I am using this code :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
So this redirects to the MainActivity on clicking the notification. I would like to redirect to a separate screen and then based on a key value display a text on that activity.
How do I achieve this ?
Thanks
Just change the PendingIntent using another Activity and/or append extra information on the Intent you are using to create the PendingIntent:
Intent launchIntent = new Intent(this, AnotherActivity.class)
launchIntent.putExtra("myKey", "myValue");
//....
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
And than, in you Activity's onCreate():
//...
getIntent().getStringExtra("myKey")
//do your stuff..
Just pass the value in the intent . eg.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
contentIntent.putExtra("phone", value);
contentIntent.putExtra("name", value);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Question has already been anwered and accepted, but here's another method:
int mId = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setNumber(1);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(mId, builder.build());
I want to place my app icon in notification area while my app is running. I searched a lot and made this possible, to show it in the notification area.
public class MainActivity extends ActionBarActivity {
NotificationManager mNotificationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setTicker("This is a new notification")
.setContentTitle("Notification")
.setContentText("App runing..")
.setSmallIcon(R.mipmap.ic_launcher);
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.build();
mNotificationManager.notify(1, notif);
}
#Override
protected void onDestroy() {
super.onDestroy();
mNotificationManager.cancel(1);
}
}
But what I want is I don't want to open any activity when clicking the notification.
eg: the battery info icon comming when battery is full, it willnot open any application or activity, also tapping it willnot remove it from notification area.
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
By changing this one line code to this
PendingIntent pIntent = PendingIntent.getActivity(context, 0, null, 0);
It will work like what I want, But only on emulator, when I run this code in real device it will crash.
how to achieve this task in my app? How can I put notification icon without Intent and PendingIntent ?
You could use this code, like mentioned in the answers here and here:
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(), // add this
PendingIntent.FLAG_UPDATE_CURRENT);
It is not about the notification or notification manager
you can do it by changing your one line code to this,
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);