How do smart watches receive text messages from mobile phone? I am looking to make a similar application and I am not sure where to start. I want to make an application for my tablet that will show text message notification.
To create a notification that is transmitted to connected Android Wear devices, you need to use the NotificationCompat class located in the Support v4-Package.
The detailed documentation how to do this can be found here.
The code essentially breaks down to this code snippet (copied from the documentation):
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
This creates a notification on the handheld as well as connected Android Wear devices, without even creating a Wear-App. The pairing and transmitting process is handled by the OS.
As a general hint, the Training section of Android Developers is always a good place to start researching.
Related
I have a foreground service with a notification, when updating this notification on a Wear device if the user is on the watch face the notification is always brought to the front, having more than one notification updating then they are constantly fighting for the screen.
Happens on Android Wear OS by Google - Version 1.2; Android OS - Version 8.0.0
On Wear OS - Version 2.12.0; Android OS - Version 7.1.1 is working correctly
I'm updating this notifications using the
NotificationManagerCompat.notify() and not the NotificationManager.notify() like the documents say and I'm keeping the NotificationCompat.Builder reference instantiated on the createNotification method and using it on the updateNotification method
Using RemoteViews instead of updating the contentText I have the same result, the only thing that works is using the .setUsesChronometer(true)but this uses RemoteViews, so there must be a way to update the notification without making it pop-up
Create notification method:
protected void showNotification(String name, String activityId) {
Intent notificationIntent = activityIntent(activityId);
notificationIntent.setAction(MAIN_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();
notificationBuilder = buildBaseNotification(name, NOTIFICATION_CHANNEL_ID)
.extend(extender)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setLocalOnly(true)
.addAction(wearableNotificationPauseAction());
startForeground(NOTIFICATION_ID, notificationBuilder.build());
}
Update notification method:
private void updateNotificationTimer(DataModel data) {
notificationBuilder.setContentText(data.getSomethingRelevant());
notificationManagerCompat.notify(NOTIFICATION_ID, notificationBuilder.build());
}
Issue in GIF format
I have been using Notifications in an app I am building. The way to build notification has changed a lot since older versions it seems, but in the end I managed to display a notification. (*)
I have two problems though:
1) The notification makes no sound. It is not a matter of increasing the volume of notifications in the tablet because notifications from gmail for example do produce sound. How do I make a notification that produce sound and vibration? (I am trying this in Android 6)
2) Even more important, how do I make a notification that "notifies" me even when the phone is sleeping? So far my notifications do happen when the phone is sleeping but I have to turn on the phone to see them. Ideally these should appear even when the screen is black
EDIT: just to be clear, the notifications are happening when the phone is sleeping. It is just that I don't see them unless I turn on the screen, then they are. I would like for the notification to "turn on the screen"
(*) My current code is
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notification= new Notification.Builder(context)
.setContentTitle(message)
.setContentText("Proximity Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pi).build(); //getNotification();
//notification.flags= Notification.FLAG_AUTO_CANCEL;
//notification.setLatestEventInfo(context, "GAST", "Proximity Alert", pi); //this is deprecated
notificationManager.notify(NOTIFICATION_ID, notification);
Yes, you can add sound while sending notification with this way.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(defaultSoundUri);
And to do it while sleeping phone you will have to use service.
Documentation : Services
Is it possible to have a notification that shows a different text (content title and content text) in the Android wear device and in the mobile device?
Not at the moment. However, you can achieve this effect in the following way:
post a notification on the phone with setLocalOnly(true)
post a DataItem using a DataAPI that describes the notification and changed text
when the wearable receives the DataItem, post the notification with different text, again setting setLocalOnly(true)
on each notification alse call setDeleteIntent so you know, when there are dismissed
when on of the notifications gets dismissed, delte the DataItem from point 2.
when the DataItem gets deleted, you will receive a callback; delete the remaining notification
There might be some corner cases here I don't see immediately, but the general approach should allow you to achieve what you want.
Yes, it's possible now with little tricky and bug on Android wear which help's us. More Over this trick doesn't need Android wear API, just a normal Notification with RemoteViews is enough.
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentText("This msg won't display in your phone, only on wear you can see.")
.setContentTitle("Hello")
.setContentIntent(
PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
NotificationManager mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_layout);
contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(50, notification);
Now run the app on your device and check the notification on both watch and phone, the content inside the RemoteViews won't display in your watch but on phone it will display and if you remove the .setContentTitle() & .setContentText(), then the RemoteViews content will be displayed on both watch and phone too.
Actually you can achieve this using
.setGroup(GROUP_KEY)
.setGroupSummary(true)
On your phone notification. Then create a notification with the data that you want to show on the watch and set
.setGroup(GROUP_KEY)
to the same group that your phone notification. This is used to show stack notifications but if you use it with only one then it does the trick.
Complete documentation: Stacking Notifications
I have Created a project for android wear devices.
I need to customize the notification style on the wearable device. Is there any method for that.
My method is
Intent displayIntent = new Intent(getApplicationContext(), CustomNotification.class);
PendingIntent displayPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification =
new NotificationCompat.Builder(context)
.extend(new WearableExtender()
.setDisplayIntent(displayPendingIntent))
.build();
int notificationId = 001;
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(notificationId , notification);
But i didn't get a notification.
Is there any method to customize with my custom layout design?
and this is the manifest part
<activity android:name="com.client.android.CustomNotification"
android:exported="true"
android:allowEmbedded="true"
android:taskAffinity=""
android:theme="#android:style/Theme.DeviceDefault.Light" />
Please take a look at the official documentation about creating custom notifications on Android Wear.
Use Big View:
You can apply styles to your notificaitons, such as BigPictureStyle, BigTextStyle or InboxStyle.
Here is an example of using BigTextStyle:
// Specify the 'big view' content to display the long
// event description that may not fit the normal content text.
BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
bigStyle.bigText(eventDescription);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setLargeIcon(BitmapFractory.decodeResource(getResources(), R.drawable.notif_background))
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent)
.addAction(R.drawable.ic_map, getString(R.string.map), mapPendingIntent)
.setStyle(bigStyle);
More info:http://developer.android.com/training/wearables/notifications/creating.html#BigView
Use custom notification layout:
You can create a layout in your Activity and embed it inside your notification:
https://developer.android.com/training/wearables/apps/layouts.html#CustomNotifications
NOTE: Please notice that this approach has some restrictions and will only work for notifications submitted directly from Android Wear device (not from phone).
Please also read the note at the end of that tutorial:
Note: When the notification is peeking on the homescreen, the system displays it with a standard template that it generates from the
notification's semantic data. This template works well on all
watchfaces. When users swipe the notification up, they'll then see the
custom activity for the notification.
If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.
Does anyone know why or any workaround? I didn't find any information in the documentation.
I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!
Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.
You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)
Solution 1 - use group:
You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// This notification will be shown only on phone
final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title phone")
.setContentText("Text phone")
.setOngoing(true)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(true);
// This notification will be shown only on watch
final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title wearable")
.setContentText("Text wearable")
.setOngoing(false)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html
Solution 2 - create wearable app:
Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.
Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276
Honestly something really weird changed in wear 1.5
so a solution would be to set an alarm
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent displayIntent = new Intent(this, WearNotif.class);
//displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("WeirdShit")
.setContentText("some random crap")
.extend(new Notification.WearableExtender()
.setDisplayIntent(displayPendingIntent))
.setSound(alarmSound)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(25454, notification);