回答編集履歴

2

補筆

2019/11/12 15:23

投稿

jun74
jun74

スコア338

test CHANGED
@@ -8,36 +8,84 @@
8
8
 
9
9
 
10
10
 
11
-
11
+ 初回1回限りしか動かない処理
12
12
 
13
13
  ```java
14
14
 
15
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
15
+ //アラーム通知チャンネル登録
16
16
 
17
- CharSequence name = getString(R.string.channel_name);
17
+ if (android.os.Build.VERSION.SDK_INT >= 26) {
18
18
 
19
- String description = getString(R.string.channel_description);
19
+ String CHANNEL_ID = "alarm";
20
20
 
21
- int importance = NotificationManager.IMPORTANCE_HIGH;
21
+ CharSequence name = getString(R.string.alarmChannel_name);
22
22
 
23
- NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
23
+ String description = getString(R.string.alarmChannel_description);
24
24
 
25
- channel.setDescription(description);
25
+ int importance = NotificationManager.IMPORTANCE_HIGH;
26
26
 
27
- // Register the channel with the system; you can't change the importance
27
+ NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
28
28
 
29
- // or other notification behaviors after this
29
+ channel.setDescription(description);
30
30
 
31
- NotificationManager notificationManager = getSystemService(NotificationManager.class);
31
+ NotificationManager notificationManager = getSystemService(NotificationManager.class);
32
32
 
33
- notificationManager.createNotificationChannel(channel);
33
+ notificationManager.createNotificationChannel(channel);
34
34
 
35
- }
35
+ }
36
36
 
37
37
  ```
38
38
 
39
39
 
40
40
 
41
- なお、まだ、改修とテストは実施していないため、テスト実施後にまた結果を更新します。
41
+ 通知処理
42
42
 
43
- 取り急ぎ、ご報告まで。
43
+ ```java
44
+
45
+ //アラーム通知チャンネル使用
46
+
47
+ String channelId = "alarm";
48
+
49
+ Intent fullScreenIntent = new Intent(intent2);
50
+
51
+ PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
52
+
53
+ fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
54
+
55
+
56
+
57
+ NotificationCompat.Builder notificationBuilder =
58
+
59
+ new NotificationCompat.Builder(this, channelId)
60
+
61
+ .setSmallIcon(R.mipmap.ic_launcher_round)
62
+
63
+ .setContentTitle(getString(R.string.app_name))
64
+
65
+ .setContentText(notifyMsg)
66
+
67
+ .addAction(0, "停止画面を開く", fullScreenPendingIntent)
68
+
69
+ .setFullScreenIntent(fullScreenPendingIntent, true);
70
+
71
+
72
+
73
+ //***/>以下は停止画面が開いてないときのみ設定するように!!
74
+
75
+ notificationBuilder.addAction(0, "スヌーズ", fullScreenPendingIntent);
76
+
77
+
78
+
79
+ notification = notificationBuilder.build();
80
+
81
+
82
+
83
+ // startForeground
84
+
85
+ startForeground(1, notification);
86
+
87
+ ```
88
+
89
+
90
+
91
+ なお、API22から25までは質問に記載した方法で実現できるはずです。

1

訂正

2019/11/12 15:23

投稿

jun74
jun74

スコア338

test CHANGED
@@ -11,6 +11,8 @@
11
11
 
12
12
 
13
13
  ```java
14
+
15
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
14
16
 
15
17
  CharSequence name = getString(R.string.channel_name);
16
18
 
@@ -30,6 +32,8 @@
30
32
 
31
33
  notificationManager.createNotificationChannel(channel);
32
34
 
35
+ }
36
+
33
37
  ```
34
38
 
35
39