How to show notification from Android wear watchface - android

I am trying to show a notification from my Watch face, as I want to show additional information when the user first opens the watchface.
This is similar to the GMT watch face that is installed with the LG G Watch R, that asks the user to swipe left to select the timezone.
This code does not seem to work for me:
Notification.Builder builder = new Notification.Builder(context)
.setContentTitle("Title")
.setContentText("Content");
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.notify(1, builder.build());
This was copied from one of Android wear's examples (was not used in a watchface example, just a regular wear app)

You are most likely missing the icon, you need to specify the icon for Android to show your notification. Try specifying everything that the example from the docs mentions:
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();

Related

Android notification won't show up as a heads up notification

I am using the following code to show my notification:
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context,
context.getString(R.string.notification_general_id))
.setSmallIcon(R.drawable.iit)
.setContentTitle("something")
.setContentText(notification)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
// https://stackoverflow.com/a/28251192/2287994
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.parseInt(last4Str);
Log.d(TAG, "notificationId " + notificationId);
notificationManager.notify(notificationId, builder.build());
I create notification channel using the following code:
// for showing general notifications
NotificationChannel generalNotificationsChannel = new NotificationChannel(
getString(R.string.notification_general_id),
getString(R.string.notification_general_name),
NotificationManager.IMPORTANCE_HIGH
);
nearbyAnchorsChannel.setDescription(getString(R.string.notification_general_desc));
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
On Google Pixel 3a, the notification only vibrates and shows up as a blip on the top left. However, on the OnePlus 6, it shows up as a proper heads-up notification that we can immediately swipe or tap on. I tried looking through the settings of my Pixel 3a (it is Android 12) but I cannot find any option that I can change to enable a heads-up display of notifications. Tbh, I am not even sure if there is something wrong with my code or the phone I am testing it on. Is it because of my OnePlus 6's Android version (it is Android 11)? Or is it due to the code that I have written? If it is due to the former then can someone please explain to me how I can change settings on my Pixel 3a to show a proper swipeable heads-up notification?
My bad, when you change channel importance, you need to uninstall the app and install it again. That's why it worked on the OnePlus 6 (because I installed it after changing the notification importance) and not on the Google Pixel 3a (because I was still working with the same install)

Notification Group not working without summary

I am implementing notifications and want to group them for display in notification bar.
Currently I am implementing the example from
Create a Group of Notifications in Android's official developer documentation.
I implemented this method:
private void makeNotification()
{
createNotificationChannel();
int SUMMARY_ID = 0;
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";
String CHANNEL_ID = "MY_CHANNEL_ID";
Notification newMessageNotification1 =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_map_position_icon)
.setContentTitle("First summary")
.setContentText("You will not believe...")
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();
Notification newMessageNotification2 =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_map_nav_position_icon_grey)
.setContentTitle("Second summary")
.setContentText("Please join us to celebrate the...")
.setGroup(GROUP_KEY_WORK_EMAIL)
.build();
Notification summaryNotification =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setContentTitle("Total summary")
.setContentText("Two new messages")
.setSmallIcon(R.drawable.ic_wdw_dont_drive)
.setGroup(GROUP_KEY_WORK_EMAIL)
.setGroupSummary(true)
.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, newMessageNotification1);
notificationManager.notify(2, newMessageNotification2);
notificationManager.notify(SUMMARY_ID, summaryNotification);
}
The method createNotificationChannel()just creates the channel, I left it out here for better readability.
Now the documentation says:
On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification.
So the last notify() call shall be optional and creating the summaryNotification, too. But this example only works for me when I notify the summary notification. When I don't do that, the notifications are not grouped.
What is going wrong here?
On Android 7.0 (API level 24) and higher, the system automatically builds a summary for your group using snippets of text from each notification.
"Summary [text]" but not "Group summary notification". This just means that whatever text you set in summary notification style it will be replaced by combined text from grouped notifications for Android >= N.
This doesn't affect the fact that the notifications are not grouped without summary.
Yes, this was highly misleading for me too, had to learn it the hard way. Try building the group on Kitkat and compare it to Nougat one.

Mixpanel push notification text not getting enlarged on Android devices

Account details on Mixpanel:
maaz.shaikh#bwellthy.com
Notification text: Hey {{USER_NICK_NAME}}, you can check your HbA1c test result by clicking on it, then click GET STARTED to start your journey with us.
Notification custom packet: { "mp_icnm_l": "app_logo", "mp_icnm_w": "notification_logo", "mp_color": "#FF7531", "sound": "yes", "vibrate": "yes"}
The notification pops up on android device like this, but does not enlarge on dragging it downwards
I am using the latest release of Mixpanel(v5.2.1) in the Android code.
Mixpanel is using BigStyle Text notifications in their library, so ideally the single line notification on dragging should enlarge on dragging it.
Can you tell me the possible reason for this?
Use BigTextStyle for setting big text in notification.
Notification notification = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.BigTextStyle()
.bigText(aVeryLongString))
.build();

Android Wear Smartwatch notification with multiple pages - small icon only shows up on 1st page?

So I have set up a 2-page Android wear notification (dispatched from the device) as such:
final NotificationCompat.Builder mainNotificationBuilder = new NotificationCompat.Builder(this)
.setDefaults(android.app.Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.icon)
.setStyle(bigStyle)
.setContentIntent(contentIntent);
android.app.Notification secondNotification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(balanceBackgroundImage)
.setStyle(bigStyle2)
.build();
android.app.Notification twoPageNotif = mainNotificationBuilder
.extend(new NotificationCompat.WearableExtender()
.setContentIcon(R.drawable.icon)
.setBackground(backgroundImage)
.addPage(secondNotification)
)
.setSmallIcon(R.drawable.icon)
.build();
However, the small icon only appears on the first page, but I want it to appear on both!
Is this even possible? I have a feeling that its not, and that the icon can only appear on the first page of a multi-page watch notification, but I wanted to get confirmation from you all.
Thanks in advance :\
Your thinking is correct: the small icon only appears on the first page of a notification.

Android Notification- Display full message

My Android Application has to be able to send short alerts out to a large group of people. The obvious place to do this is in the notification center. The full notification shows up in the ticker without a problem, but in the notification center a user can only see the first couple words and then an elipsis. The notifications are not long at all, just 10-15 words at the most. How can I make the text wrap down to a new line?
My code to build the notifications is here
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.splash)
.setContentTitle("Student Engauge")
.setContentText(extras.getString("message"))
.setAutoCancel(true)
.setTicker(extras.getString("message"));
final int notificationId = 1;
NotificationManager nm = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(notificationId, mBuilder.build());
To show large chunk of text, use the BigTextStyle. Here is a sample code as given in BigTextStyle. This notification will one line of text and will expand to more lines if needed.
Notification noti = new Notification.Builder()
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new Notification.BigTextStyle()
.bigText(aVeryLongString))
.build();
For android support library
Notification noti = new Notification.Builder()
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(aVeryLongString))
.build();
For Android 4.1 and later devices, big view is the most suitable solution to show large amount of text. For pre 4.1 devices, you can use custom notification layout to show more data like mentioned here. But you should keep in mind two things:
From the official documentation
Caution: When you use a custom notification layout, take special care to ensure that your custom layout works with different device orientations and resolutions. While this advice applies to all View layouts, it's especially important for notifications because the space in the notification drawer is very restricted. Don't make your custom layout too complex, and be sure to test it in various configurations.
Custom notification layouts have some limitations. Too long texts are not shown fully, but 10-15 words probably fit to the custom layout. This answer has more info about the limitation of custom notification layouts

Categories

Resources