如何在Wear OS上制作通知?

huangapple go评论69阅读模式
英文:

How to make notification for Wear Os?

问题

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);
        }
    }
}
英文:

I have done notification on Android before and I haven't any problems with it. But, when I have tried to make notification by example for Wear OS I have an error. It says that: "Field to post a notification on channel: "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);
}
} }

I have used SDK 28

答案1

得分: 0

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

private void createNotificationChannel() {
    // 仅在API 26+上创建NotificationChannel,因为
    // NotificationChannel类是新的,不在支持库中
    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);
        // 将通道注册到系统中;在此之后无法更改重要性
        // 或其他通知行为
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
英文:

In createNotificationChannel method, you need to set up id as id onCreate method like this:

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

huangapple
  • 本文由 发表于 2020年8月8日 20:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63315207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定