如何为 Wear Os 发出通知?

How to make notification for Wear Os?

我之前在 Android 上做过通知,我没有遇到任何问题。但是,当我尝试通过示例为 Wear OS 发出通知时,出现错误。它说:“字段 post 通道上的通知:“my_channel_01”“

public class MainActivity extends WearableActivity {

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = (TextView) findViewById(R.id.text);

    int notificationId = 001;
    // The channel ID of the notification.
    String id = "my_channel_01";
    // Build intent for notification content
    Intent viewIntent = new Intent(this, MainActivity.class);
    viewIntent.putExtra("EXTRA_EVENT_ID", 1);
    PendingIntent viewPendingIntent =
            PendingIntent.getActivity(this, 0, viewIntent, 0);
    createNotificationChannel();
    // Notification channel ID is ignored for Android 7.1.1
    // (API level 25) and lower.
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, id)
                    .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
                    .setContentTitle("Hello World")
                    .setContentText("eventLocation")
                    .setContentIntent(viewPendingIntent);

    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager =
            NotificationManagerCompat.from(this);

    // Issue the notification with notification manager.
    notificationManager.notify(notificationId, notificationBuilder.build());

    // Enables Always-on
    setAmbientEnabled();
}

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Test";
        String description = "Notification for Wear OS";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("1", name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
} }

我用过SDK 28

在createNotificationChannel方法中,你需要像这样设置id作为id onCreate方法:

private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String id = "my_channel_01";
    CharSequence name = "Test";
    String description = "Notification for Wear OS";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(id, name, importance);
    channel.setDescription(description);
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}