I'm working on a custom notification for Android Wear that should display a custom activity when the notification is swiped upwards. The notification is displayed, and even the wearable features (like the background and custom size) are visible, however the activity is never loaded nor displayed.
Also, MainActivity
and PlaybackNotificationActivity
should both write a message to the log when they're created. MainActivity
writes the message, but the latter doesn't.
What could be the problem?
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new NotificationBuilder(this).showNotification();
Log.e("MainActivity", "Reached");
finish();
}
}
NotificationBuilder.java
public class NotificationBuilder {
private final Context mContext;
public NotificationBuilder(Context context) {
mContext = context;
}
public Notification buildNotification() {
Notification.Builder builder = new Notification.Builder(mContext)
.setContentTitle("Title")
.setContentText("Content text")
.setSmallIcon(R.mipmap.ic_launcher);
Intent playbackIntent = new Intent(mContext, PlaybackNotificationActivity.class);
PendingIntent playbackPendingIntent = PendingIntent.getActivity(
mContext, 0, playbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.WearableExtender extender = new Notification.WearableExtender()
.setBackground(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
.setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM)
.setDisplayIntent(playbackPendingIntent);
builder.extend(extender);
return builder.build();
}
public void showNotification() {
Notification notification = buildNotification();
NotificationManager manager = (NotificationManager)
mContext.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
}
PlaybackNotificationActivity.java
public class PlaybackNotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playback_notification);
Log.e("NotificationActivity", "Reached");
Toast.makeText(this, "Toast", Toast.LENGTH_SHORT).show();
}
}
It turns out that the emulator for Android Wear is to blame, it works perfectly on a physical smartwatch but not on the emulator.