您可以指定在眼镜显示屏上点按通知时调用的其他 PendingIntent。此 PendingIntent 与使用 setContentIntent 为手机设置的默认 intent 不同。例如,当用户点按显示 AI 眼镜上的通知时,显示 AI 眼镜上会启动特定的眼镜 activity。
如需添加眼镜专用行为,请使用 ProjectedExtender。借助此 API,您可以自定义眼镜上的通知行为,而不会影响通知在手机上的显示方式。
Kotlin
// Intent to fire when tapped on the phone
val phoneIntent = Intent(this, MyPhoneActivity::class.java)
val phonePendingIntent = PendingIntent.getActivity(
this, 0, phoneIntent, PendingIntent.FLAG_IMMUTABLE
)
// Intent to fire when tapped on the glasses display
val glassesIntent = Intent(this, MyGlassesActivity::class.java)
val glassesPendingIntent = PendingIntent.getActivity(
this, 1, glassesIntent, PendingIntent.FLAG_IMMUTABLE
)
// Create the base notification
val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
setSmallIcon(R.drawable.ic_notification)
setContentTitle("Navigation in Progress")
setContentText("Tap to see details")
// Default intent for phone
setContentIntent(phonePendingIntent)
// Create and apply the glasses extender
val projectedExtender = ProjectedExtender().setContentIntent(glassesPendingIntent)
extend(projectedExtender)
}
// Issue the notification
notificationManager.notify(NOTIFICATION_ID, builder.build())
Java
// Intent to fire when tapped on the phone
Intent phoneIntent = new Intent(this, MyPhoneActivity.class);
PendingIntent phonePendingIntent =
PendingIntent.getActivity(this, 0, phoneIntent, PendingIntent.FLAG_IMMUTABLE);
// Intent to fire when tapped on the glasses display
Intent glassesIntent = new Intent(this, MyGlassesActivity.class);
PendingIntent glassesPendingIntent =
PendingIntent.getActivity(this, 1, glassesIntent, PendingIntent.FLAG_IMMUTABLE);
// Create the base notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("New Update")
.setContentText("Something important happened.")
// Default intent for phone
.setContentIntent(phonePendingIntent);
// Create and apply the Glasses extender
ProjectedExtender projectedExtender = new ProjectedExtender()
// glasses-specific intent
.setContentIntent(glassesPendingIntent);
builder.extend(projectedExtender);
// Issue the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());