Android Wear: Timer like notification card on wear device - android

I'm working on implementation of Pomodoro app for Android Wear.
I want to make similar to standard timer UI/UX, I guess it's implemented using displaying/updating notification with timer as title, so I'm showing notification and updating it periodically from Service:
private void updateNotification() {
Intent stopActionIntent = new Intent(this, PomodoroActivity.class);
stopActionIntent.setAction(PomodoroActivity.ACTION_STOP);
PendingIntent stopActionPendingIntent = PendingIntent.getActivity(this, 0, stopActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action stopAction = new NotificationCompat.Action.Builder(
R.drawable.ic_stop,
getString(R.string.stop_pomodoro),
stopActionPendingIntent)
.build();
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bg_pomodoro_timer))
.setContentTitle(convertDiffToTimer(System.currentTimeMillis(), timerDeadlineMs))
.setPriority(Notification.PRIORITY_MAX)
.setOngoing(true)
.extend(new NotificationCompat.WearableExtender().addAction(stopAction))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, notification);
}
Unpleasure problem with such solution is blinking of notificaiton.
Any ideas how escape blinking? Or maybe other way to achieve target behaviour?

To update ongoing/existing notification -
Use Same Id of Notification in Builder
Use .setOnlyAlertOnce(true)
NotificationCompat.Builder notificationBuilder;
public void generateNotificationForTimer(String timeInString, boolean isFirstTime) {
if (isFirstTime) {
notificationBuilder = new NotificationCompat.Builder(this)
.setStyle(new NotificationCompat.BigPictureStyle())
.setOnlyAlertOnce(true)
.setContentTitle("Timer Notification Demo")
.setContentText("Time - " + timeInString)
.setSmallIcon(R.drawable.common_signin_btn_icon_dark);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
} else {
notificationBuilder.setContentText(timeInString);
NotificationManagerCompat.from(this).notify(110, notificationBuilder.build());
}
}

Related

BigPictureStyle Notification not working in AndroidX

Code:
here is the code to popup notification on button click
notificationbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getActivity(),OrderTrack.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
Notification builder=new NotificationCompat.Builder(getActivity(),"1")
.setSmallIcon(R.drawable.half_bicycle)
.setContentTitle("title")
.setContentText("Text")
.setLargeIcon(bitmap)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
Notification notif = new Notification.Builder(getContext())
.setContentTitle("New photo from ")
.setContentText("subject")
.setSmallIcon(R.drawable.half_bicycle)
.setLargeIcon(bitmap)
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bitmap))
.build();
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);
});
Here I am trying to use BigPictureSt
yle notification. I hava tried two variation of code. But both doesn't give any result/notification.
All the above code is inside a fragment.Is this a reason why it's not working?
I have set the App style to NoActionBar.Is this a reason why it's not working?
Please answer this questions with a proper solution !!!!
Adding the Following code doesn't solve the problem
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);
The code is fine and there is nothing wrong with it but most importantly you forgot to show the notification itself i.e you are not using the objects that are created.
So show the notification like this...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);//101 is notification id
EDIT:
If you are targetting Android Oreo and above you need to create the channels for notification like this. Use this function to create notification channel and set this channel id on a notification while creating it...
String channelId = "some_channel_id";
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Complete code:
notificationbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createNotificationChannel(); //Create notification channel
Toast.makeText(getContext(), "noti", Toast.LENGTH_SHORT).show();
Intent i=new Intent(getActivity(),OrderTrack.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(),(int) Calendar.getInstance().getTimeInMillis(),i,0);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.half_bicycle);
Notification builder=new NotificationCompat.Builder(getActivity(),"1")
.setSmallIcon(R.drawable.half_bicycle)
.setContentTitle("title")
.setContentText("Text")
.setLargeIcon(bitmap)
.setChannelId(CHANNEL_ID) // Channel Id
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.build();
Notification notif = new Notification.Builder(getContext())
.setContentTitle("New photo from ")
.setContentText("subject")
.setSmallIcon(R.drawable.half_bicycle)
.setLargeIcon(bitmap)
.setChannelId(CHANNEL_ID) // Channel Id
.setStyle(new Notification.BigPictureStyle()
.bigPicture(bitmap))
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getActivity());
notificationManager.notify(101, notif);//101 is notification id
}
});
For more info visit this : https://developer.android.com/training/notify-user/channels

Android - notification does not appear in status bar

When starting an IntentService to upload a file in the background, I want to show a notification to the user that the upload is in progress by calling showNotification() from inside my service:
private void showNotification() {
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_cloud_upload_black_24dp)
.setWhen(System.currentTimeMillis())
.setContentTitle("Uploading")
.setContentText("Your upload is in progress.")
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Now here's my problem: the notification appears on the lock screen, but not in the status bar when the screen is unlocked. What am I missing?
Deployment target is API level 24, so a missing NotificationChannel should not be the cause.
try it :
PendingIntent pendingIntent = PendingIntent.getActivity(getmContext(), 0, new Intent(getmContext(), MainActivity.class), 0);
android.app.Notification pp = new NotificationCompat.Builder(getmContext())
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentText(getMessage())
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getmContext().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, pp);
This is my code which is working fine with target SDK 25
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
where NOTIFICATION_ID
public static final int NOTIFICATION_ID = 100;
I feel so dumb. Turns out the notification was displayed all the time, but with a black icon on black background.
Changed the icon colour in the xml from
android:fillColor="#FF000000"
to
android:fillColor="#FFFFFFFF"
and it works like a charm

Grouping Android Notifications

The below code is working fine with me in terms of creating the notification, with its voice and actions.
but looks the .setGroup(GROUP_KEY_Fonix) is not working, as the notifications are not grouped together, am I missing something here!
public int notificationID = 0;
final static String GROUP_KEY_Fonix = "fonix_notification";
private void Notify(String notificationTitle, String notificationMessage){
notificationID++;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(MainActivity.this, NotificationView.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(this)
.setGroup(GROUP_KEY_Fonix)
.setContentTitle("New note: " + notificationTitle)
.setContentText(notificationMessage)
.setSmallIcon(R.drawable.ic_stat_new_message)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ic_stat_new_message, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// hide the notification after its selected
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationID, notif);
}
Also, you should create group summary notification. For this just use this code:
Notification notificationSummary = new NotificationCompat.Builder(this)
//Set content
.setGroup(GROUP_KEY_Fonix)
.setGroupSummary(true)
.build();
notificationManager.notify(notificationSummaryId, notificationSummary);
Learn more here.

Update Notification without Collasping notification drawer

i have a notification manager that launches an activity when clicked.
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContent(remoteViews)
.setAutoCancel(false);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Launch.class).putExtra("type", 0), 0);
mBuilder.setContentIntent(pendingIntent);
the function performed in this can also be be asynchronous depending on the users preference. I want to update the notification to an ongoing progress. something of this nature:
mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setOngoing(true)
.setContentTitle("Sending details")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setProgress(100, 0, true)
.setOnlyAlertOnce(true)
.setAutoCancel(false);
when the notification is selected. This is achieved but what i want to do is update the notification from the former to the later without having to first slide up the navigation drawer.
Try this..
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
you need NOTIFICATION_ID to updated the specific notification.
Please refer this

Implement expand and collapse notification android

I need to implement expand and collapse notification in status bar for android 4.0 and above version. I have search on google for this but didn't getting any code solution for implementation does anybody have I idea how to implement this
Thank You in advance
An expandable Notification is a special case of a Notification Big View. If the Big View is not at the top of the notification drawer, it it shown 'closed' and is expandable by swipe. Quote from Android Developers:
A notification's big view appears only when the notification is expanded, which happens when the notification is at the top of the notification drawer, or when the user expands the notification with a gesture. Expanded notifications are available starting with Android 4.1.
The Big View Notification can be created as follows:
Notification notification = new Notification.BigTextStyle(builder)
.bigText(myText).build();
or
Notification notification = new Notification.BigPictureStyle(builder)
.bigPicture(
BitmapFactory.decodeResource(getResources(),
R.drawable.my_picture)).build();
Here is a tutorial.
Notification noti = new Notification.Builder()
... // The same notification properties as the others
.setStyle(new Notification.BigPictureStyle().bigPicture(mBitmap))
.build();
You change
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
along with the announcement OK !!!
notification = new NotificationCompat.Builder(context)
Here is an example:
Expandable Notifications and Notifications Groups
You can set Code
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
// bigStyle.bigText((CharSequence) context);
notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(th_title)
.setContentText(th_alert)
.setAutoCancel(true)
// .setStyle(new Notification.BigTextStyle().bigText(th_alert) ตัวเก่า
// .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))
.setContentIntent(pendingIntent)
.setNumber(++numMessages)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
or
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
// .setContentTitle(notification.getTitle())
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(notification.getBody())
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notification.getBody()))
.setContentInfo(notification.getTitle())
.setLargeIcon(icon)
.setColor(Color.RED)
.setSmallIcon(R.drawable.logo);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setLights(Color.YELLOW, 1000, 300);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
27/07/2021 :
You should can read detail this : Expandable Notifications and Notifications Groups
I Couldn't able to set new instance of new NotificationCompat.BigTextStyle() in .setStyle() method of Notification. So I have used the below one, new instance of new Notification.BigTextStyle() in .setStyle().
Notification builder =new Notification.Builder(this)
.setSmallIcon(Notification_icons[icon])
.setContentTitle(title)
.setContentText(description)
.setChannelId(channelID_Default)
.setOngoing(true)
.setStyle(new Notification.BigTextStyle()
.bigText(description))
.build();
There is a method for this function you can show a small icon when notification collapsed and show a large one when a notification expanded.
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.notification)
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.new_post)
.setContentTitle(imageTitle)
.setContentText(imageDescription)
.setLargeIcon(bitmap)
.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(bitmap)
.bigLargeIcon(null))
.build()
(2021) Stumbled upon this question when I was having issues with expanding my notification which had a larger text.
Solved by referring to the Android docs on Expandable Notifications: Create an Expandable Notification
Nimisha V's answer shows how to expand a large image on a notification. The below code is used for expanding large text on a notification.
var notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setStyle(NotificationCompat.BigTextStyle()
.bigText(notification_message))
.build()
We can't create expandable notification in below android 4.1 versions.
But Instead of this we can do that we can stacked the notifications and then we can set a pending intent to our pretty custom activity which shows all notification in list.
User will happy to see this :)

Categories

Resources