【问题标题】:How to make notification for Wear Os?如何为 Wear Os 发出通知?
【发布时间】:2020-08-08 12:09:32
【问题描述】:

我之前在 Android 上做过通知,我没有任何问题。但是,当我尝试通过示例为 Wear OS 发出通知时,我遇到了一个错误。它说:“在频道上发布通知的字段:“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

【问题讨论】:

    标签: java android wear-os


    【解决方案1】:

    在 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多