问题
I'm trying to send a notification to an Android Wear SmartWatch when a button is clicked. It's working in all devices I have tested, except for the ones with Android L. Does anyone have any idea about what may be the problem? I have even paired the devices with an Android Wear SmartWatch emulator, but, again, it does not work with Android L devices.
Below is my button:
// the button click listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MyNotification(getApplicationContext()).sendNotification();
}
});
And MyNotification class:
public class MyNotification {
private static final AtomicInteger ID_COUNT = new AtomicInteger(0);
private int mNotificationID;
private Context mContext;
public MyNotification(Context context) {
mNotificationID = ID_COUNT.incrementAndGet();
mContext = context;
}
private void sendNotification() {
final Intent viewIntent = new Intent(mContext, MainActivity.class);
final PendingIntent viewPendingIntent =
PendingIntent.getActivity(mContext, 0, viewIntent, 0);
final Bitmap bgBitmap = BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_launcher);
NotificationCompat.WearableExtender wearableExtender;
final Intent intent = new Intent(mContext, MyReceiver.class);
final PendingIntent pendingIntent =
PendingIntent.getBroadcast(mContext, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Action action =
new NotificationCompat.Action.Builder(R.drawable.ic_launcher, "Connect",
pendingIntent).build();
wearableExtender =
new NotificationCompat.WearableExtender()
.addAction(action)
.setBackground(bgBitmap);
final NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setContentTitle("Title")
.setContentText("Text")
.setContentIntent(viewPendingIntent)
.setGroup(NOTIFICATION_GROUP)
.setOnlyAlertOnce(true)
.setDefaults(android.app.Notification.DEFAULT_VIBRATE)
.extend(wearableExtender);
final NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(mContext);
notificationManager.notify(mNotificationID, notificationBuilder.build());
}
}
Thanks in advance.
EDIT
The problem was that I was not setting the small icon of the builder. So, my code was supposed to be as follows:
final NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(mContext)
.setContentTitle("Title")
.setContentText("Text")
.setSmallIcon(R.drawable.someIcon)
.setContentIntent(viewPendingIntent)
.setGroup(NOTIFICATION_GROUP)
.setOnlyAlertOnce(true)
.setDefaults(android.app.Notification.DEFAULT_VIBRATE)
.extend(wearableExtender);
I don't know why, but it works well on Android KitKat devices, but not on Android Lollipop.
来源:https://stackoverflow.com/questions/28637570/android-l-devices-not-sending-notification-card-to-android-wear-smartwatch