如何在Wear OS上制作通知?

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

How to make notification for Wear Os?

问题

  1. public class MainActivity extends WearableActivity {
  2. private TextView mTextView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mTextView = (TextView) findViewById(R.id.text);
  8. int notificationId = 001;
  9. // The channel ID of the notification.
  10. String id = "my_channel_01";
  11. // Build intent for notification content
  12. Intent viewIntent = new Intent(this, MainActivity.class);
  13. viewIntent.putExtra("EXTRA_EVENT_ID", 1);
  14. PendingIntent viewPendingIntent =
  15. PendingIntent.getActivity(this, 0, viewIntent, 0);
  16. createNotificationChannel();
  17. // Notification channel ID is ignored for Android 7.1.1
  18. // (API level 25) and lower.
  19. NotificationCompat.Builder notificationBuilder =
  20. new NotificationCompat.Builder(this, id)
  21. .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
  22. .setContentTitle("Hello World")
  23. .setContentText("eventLocation")
  24. .setContentIntent(viewPendingIntent);
  25. // Get an instance of the NotificationManager service
  26. NotificationManagerCompat notificationManager =
  27. NotificationManagerCompat.from(this);
  28. // Issue the notification with notification manager.
  29. notificationManager.notify(notificationId, notificationBuilder.build());
  30. // Enables Always-on
  31. setAmbientEnabled();
  32. }
  33. private void createNotificationChannel() {
  34. // Create the NotificationChannel, but only on API 26+ because
  35. // the NotificationChannel class is new and not in the support library
  36. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  37. CharSequence name = "Test";
  38. String description = "Notification for Wear OS";
  39. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  40. NotificationChannel channel = new NotificationChannel("1", name, importance);
  41. channel.setDescription(description);
  42. // Register the channel with the system; you can't change the importance
  43. // or other notification behaviors after this
  44. NotificationManager notificationManager = getSystemService(NotificationManager.class);
  45. notificationManager.createNotificationChannel(channel);
  46. }
  47. }
  48. }
英文:

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""

  1. public class MainActivity extends WearableActivity {
  2. private TextView mTextView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mTextView = (TextView) findViewById(R.id.text);
  8. int notificationId = 001;
  9. // The channel ID of the notification.
  10. String id = "my_channel_01";
  11. // Build intent for notification content
  12. Intent viewIntent = new Intent(this, MainActivity.class);
  13. viewIntent.putExtra("EXTRA_EVENT_ID", 1);
  14. PendingIntent viewPendingIntent =
  15. PendingIntent.getActivity(this, 0, viewIntent, 0);
  16. createNotificationChannel();
  17. // Notification channel ID is ignored for Android 7.1.1
  18. // (API level 25) and lower.
  19. NotificationCompat.Builder notificationBuilder =
  20. new NotificationCompat.Builder(this, id)
  21. .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
  22. .setContentTitle("Hello World")
  23. .setContentText("eventLocation")
  24. .setContentIntent(viewPendingIntent);
  25. // Get an instance of the NotificationManager service
  26. NotificationManagerCompat notificationManager =
  27. NotificationManagerCompat.from(this);
  28. // Issue the notification with notification manager.
  29. notificationManager.notify(notificationId, notificationBuilder.build());
  30. // Enables Always-on
  31. setAmbientEnabled();
  32. }
  33. private void createNotificationChannel() {
  34. // Create the NotificationChannel, but only on API 26+ because
  35. // the NotificationChannel class is new and not in the support library
  36. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  37. CharSequence name = "Test";
  38. String description = "Notification for Wear OS";
  39. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  40. NotificationChannel channel = new NotificationChannel("1", name, importance);
  41. channel.setDescription(description);
  42. // Register the channel with the system; you can't change the importance
  43. // or other notification behaviors after this
  44. NotificationManager notificationManager = getSystemService(NotificationManager.class);
  45. notificationManager.createNotificationChannel(channel);
  46. }
  47. } }

I have used SDK 28

答案1

得分: 0

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

  1. private void createNotificationChannel() {
  2. // 仅在API 26+上创建NotificationChannel,因为
  3. // NotificationChannel类是新的,不在支持库中
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  5. String id = "my_channel_01";
  6. CharSequence name = "Test";
  7. String description = "Notification for Wear OS";
  8. int importance = NotificationManager.IMPORTANCE_DEFAULT;
  9. NotificationChannel channel = new NotificationChannel(id, name, importance);
  10. channel.setDescription(description);
  11. // 将通道注册到系统中;在此之后无法更改重要性
  12. // 或其他通知行为
  13. NotificationManager notificationManager = getSystemService(NotificationManager.class);
  14. notificationManager.createNotificationChannel(channel);
  15. }
  16. }
英文:

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

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

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:

确定