How can I start and stop service from notification just by tapping it(No button), I somehow managed to start the pending intent, but don't know where to put stop service()/stopself() command.
PendingIntent pi = PendingIntent.getService(MainActivity.this,0, new Intent(MainActivity.this, MainService.class), 0);
final Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setTicker("MY FIRST NOTIFICATION TOGGLE")
.setSmallIcon(R.drawable.ic_blur_on_black_24dp)
.setContentTitle("Click to toggle on/off")
.setContentText("Click Again")
.setContentIntent(pi)
.setOngoing(true)
.build();
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Add action to your notification:
final Notification notification = new
NotificationCompat.Builder(MainActivity.this)
.setTicker("MY FIRST NOTIFICATION TOGGLE")
.setSmallIcon(R.drawable.ic_blur_on_black_24dp)
.setContentTitle("Click to toggle on/off")
.setContentText("Click Again")
.setContentIntent(pi)
.setOngoing(true)
.addAction(R.drawable.some_icon, "start service", pi)
.build();
Related
I want to display a simple notification when my app is running and doing certain things. I want my app to open when the user clicks my notification.
So far, the behaviour works like I want it to.
However, when the user clicks the notification, while the app is opened and the notification is still there, the notification icon in the notification bar (left to the time, before swiping down) disappears. Edit: I just observed that the notification icon reappears in the notification bar once any other notification (WhatsApp) arrives.
How can I prevent this behaviour?
The notification looks as following:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent intent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
builder.setContentTitle("Running")
.setContentText("App is running.")
.setSmallIcon(R.drawable.ic_lock_outline_black_24dp)
.setOngoing(true)
.setContentIntent(intent);
notifyManager.notify(appRunningNotificationID, builder.build());
Look code below:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Running")
.setContentText("App is running")
.setTicker("My app")
.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
long[] vibrate = { 500,1000 };
notification.vibrate = vibrate;
mNotificationManager.notify(appRunningNotificationID, notification);
I'm trying to launch a FullScreenIntent from a service working in the background, but I realised that the intent would not be shown another time if I do not clear the notification from the task bar. I have tried removing the notification by removing
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setContentText("A client is calling")
but that just removes my option of clearing the notification. Can somebody help?
My code is as shown:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification")
.setContentText("A client is calling")
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setAutoCancel(true);
Intent resultIntent = new Intent(this, MainActivity.class);
String strName = ((String) message).substring(9);
resultIntent.putExtra("Msg",strName);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setFullScreenIntent(resultPendingIntent,true);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager)this.getSystemService(this.NOTIFICATION_SERVICE);
// Builds the notification and issues it.
Notification m = mBuilder.build();
m.flags = Notification.FLAG_AUTO_CANCEL;
mNotifyMgr.notify(mNotificationId, m);
i want to create a notification that user can't close it by drag to left or right.
user must click o that to close notification
my code is :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
Notification n = new Notification.Builder(getApplicationContext())
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_tt)
.setLargeIcon( BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_tt))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setOngoing(true).build();
n.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
but it dosnot work
any idea ?
You need to use both functions to retain the notification,
nBuilder.setOngoing(true);
nBuilder.setAutoCancel(true);
It can be cancelled only when user taps on it.
Find setAutoCancel in this Managing Notifications.
Hope this helped!
I'm not sure what I'm doing wrong, the notification is the way I like it but I cannot get it to make the watch vibrate, and it shows as minimized. the effect I want to achieve should look like hangouts notification, which vibrate and go fullscreen. here is the code I'm using (on the watch):
Intent actionIntent = new Intent(this, ConvActivity.class);
actionIntent.putExtra("num",num);
PendingIntent actionPendingIntent =
PendingIntent.getActivity(this, 0, actionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_launcher,"Reply"
, actionPendingIntent)
.build();
NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(body).setBigContentTitle(name);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(name)
.setContentText(body)
//.setContentIntent(viewPendingIntent)
//.addAction(R.drawable.ic_map,
// getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle).setAutoCancel(true)
.addAction(R.drawable.ic_launcher,"Reply"
, actionPendingIntent);
if(pic != null)
notificationBuilder.setLargeIcon(BitmapFactory.decodeByteArray(pic,0,pic.length));
//.extend(new NotificationCompat.WearableExtender().addAction(action)) ;
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Issue the notification with notification manager.
notificationManager.notify(0, notificationBuilder.build());
Only notifications that vibrate/sound will vibrate an Android Wear device and show more text.
You should consider using the NotificationCompat.Builder.setDefaults() method:
// Light, sound, and vibrate
builder.setDefaults(Notification.DEFAULT_ALL);
// Just sound
builder.setDefaults(Notification.DEFAULT_SOUND);
// Just vibrate
builder.setDefaults(Notification.DEFAULT_VIBRATE);
I'm currently building a notification on the mobile device, and including a wear-specific intent to launch an activity. I want the notification to go away once the user hits that button on the notification, but I haven't gotten it to work. Here's my code
Intent actionIntent = new Intent(context, LauncherService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, actionIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Create the action
NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.buzz_icon_white,
"Respond with Buzz", pendingIntent)
.build();
// Build the notification and add the action with WearableExtender
Notification notification =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.buzz_icon_white)
.setContentTitle("Buzz")
.setContentText(name + ": " + message)
.extend(new WearableExtender().addAction(action))
.setAutoCancel(true)
.setPriority(1)
.setVibrate(pattern)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(context);
// Issue the notification with notification manager.
notificationManager.notify(notificationId, notification);