teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

補筆

2019/11/12 15:23

投稿

jun74
jun74

スコア338

answer CHANGED
@@ -3,20 +3,44 @@
3
3
 
4
4
  [通知チャネルを作成して管理する](https://developer.android.com/training/notify-user/channels?hl=ja)
5
5
 
6
+ 初回1回限りしか動かない処理
7
+ ```java
8
+ //アラーム通知チャンネル登録
9
+ if (android.os.Build.VERSION.SDK_INT >= 26) {
10
+ String CHANNEL_ID = "alarm";
11
+ CharSequence name = getString(R.string.alarmChannel_name);
12
+ String description = getString(R.string.alarmChannel_description);
13
+ int importance = NotificationManager.IMPORTANCE_HIGH;
14
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
15
+ channel.setDescription(description);
16
+ NotificationManager notificationManager = getSystemService(NotificationManager.class);
17
+ notificationManager.createNotificationChannel(channel);
18
+ }
19
+ ```
6
20
 
21
+ 通知処理
7
22
  ```java
23
+ //アラーム通知チャンネル使用
24
+ String channelId = "alarm";
25
+ Intent fullScreenIntent = new Intent(intent2);
26
+ PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
8
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
27
+ fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
28
+
29
+ NotificationCompat.Builder notificationBuilder =
30
+ new NotificationCompat.Builder(this, channelId)
31
+ .setSmallIcon(R.mipmap.ic_launcher_round)
9
- CharSequence name = getString(R.string.channel_name);
32
+ .setContentTitle(getString(R.string.app_name))
33
+ .setContentText(notifyMsg)
34
+ .addAction(0, "停止画面を開く", fullScreenPendingIntent)
10
- String description = getString(R.string.channel_description);
35
+ .setFullScreenIntent(fullScreenPendingIntent, true);
11
- int importance = NotificationManager.IMPORTANCE_HIGH;
36
+
12
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
13
- channel.setDescription(description);
14
- // Register the channel with the system; you can't change the importance
15
- // or other notification behaviors after this
37
+ //***/>以下は停止画面が開いてないときのみ設定するように!!
16
- NotificationManager notificationManager = getSystemService(NotificationManager.class);
38
+ notificationBuilder.addAction(0, "スヌーズ", fullScreenPendingIntent);
39
+
17
- notificationManager.createNotificationChannel(channel);
40
+ notification = notificationBuilder.build();
41
+
18
- }
42
+ // startForeground
43
+ startForeground(1, notification);
19
44
  ```
20
45
 
21
- なお、まだ、改修とテスト実施ていないめ、テスト施後にまた結果を更新します。
46
+ なお、API22から25質問に記載した方法で現できるはずです。
22
- 取り急ぎ、ご報告まで。

1

訂正

2019/11/12 15:23

投稿

jun74
jun74

スコア338

answer CHANGED
@@ -5,6 +5,7 @@
5
5
 
6
6
 
7
7
  ```java
8
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
8
9
  CharSequence name = getString(R.string.channel_name);
9
10
  String description = getString(R.string.channel_description);
10
11
  int importance = NotificationManager.IMPORTANCE_HIGH;
@@ -14,6 +15,7 @@
14
15
  // or other notification behaviors after this
15
16
  NotificationManager notificationManager = getSystemService(NotificationManager.class);
16
17
  notificationManager.createNotificationChannel(channel);
18
+ }
17
19
  ```
18
20
 
19
21
  なお、まだ、改修とテストは実施していないため、テスト実施後にまた結果を更新します。