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);
Related
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();
When my broadcast receiver is triggered it will launch a notification that directs the user to an chat activity which is build in the application.
The problem is when the user is inside the application doing things. (it's main purpose is not chatting)
let's say receives the notification while he's in the menu of the app
When he presses the notification the chat activity starts up
When pressing the back button the user goes to the home screen of the phone?
I want it to go back to the activity where he triggered the notification. This could be anywhere inside the application.
Building the notification:
notificationIntent = new Intent(mContext, BerichtenActivity.class);
Toast.makeText(context, verzender + ": " + inhoud, Toast.LENGTH_LONG).show();
Notification notification = NotificationUtils.handleNotification(mContext, notificationIntent, verzender, inhoud, R.drawable.ic_stat_chat);
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notification);
public static Notification handleNotification(Context context, Intent resultIntent, String titel, String body, int smallIcon){
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentText(body)
.setContentTitle(titel)
.setContentIntent(resultPendingIntent)
.setSound(uri)
.setLargeIcon(BitmapFactory.decodeResource(
context.getResources(), R.mipmap.ic_launcher))
.setSmallIcon(smallIcon);
return notificationBuilder.build();
}
I am using bundled notifications to group notifications by type. This works quite fine from a user's point of view when notifications are maximised, as shown below:
This does not work too well however when the child notifications are collapsed within the summary:
Now, given that the same REGISTER action can be performed on all the notifications in this group, I would like to give the user the possibility to carry out all these by simply tapping on a REGISTER ALL action in the summary notification. Adding actions to the summary however seems to have no effect. The code below - which is what I'm currently using - shows that the action on the summary notification is simply ignored:
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterAllService.getRegisterAllServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register all", actionIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(true);
NotificationManagerCompat.from(getApplicationContext()).notify(0, builder.build());
for(MyNotification myNotification: myNotifications){
final PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent actionIntent = PendingIntent.getService(this, 0, RegisterService.getRegisterServiceIntent(),PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action = new NotificationCompat.Action(R.mipmap.ic_launcher, "register", actionIntent);
NotificationCompat.Builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_notification)
.setContentTitle(myNotification.getTitle())
.setStyle(new NotificationCompat.BigTextStyle().bigText(myNotification.getSubtitle()))
.setTicker(myNotification.getSubtitle())
.setContentText(myNotification.getSubtitle())
.setAutoCancel(true)
.addAction(action)
.setGroup("confirmgroup")
.setGroupSummary(false)
.setContentIntent(pendingIntent)
NotificationManagerCompat.from(getApplicationContext()).notify(myNotification.getId(), builder.build());
}
Is there a way to make this happen?
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 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);