Bắt đầu một hoạt động trên kính AI hiển thị từ một thông báo

Các thiết bị XR được hỗ trợ
Hướng dẫn này giúp bạn xây dựng các trải nghiệm cho những loại thiết bị XR sau.
Kính AI

Bạn có thể chỉ định một PendingIntent khác được gọi khi một thông báo được nhấn trên màn hình kính. PendingIntent này khác với tập hợp ý định mặc định cho điện thoại bằng cách sử dụng setContentIntent. Ví dụ: khi người dùng nhấn vào thông báo trên kính AI có màn hình, một hoạt động cụ thể của kính sẽ chạy trên kính AI có màn hình.

Để thêm các hành vi dành riêng cho kính, hãy sử dụng ProjectedExtender. API này cho phép bạn tuỳ chỉnh hành vi của thông báo trên kính mà không ảnh hưởng đến cách thông báo xuất hiện trên điện thoại.

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());