1 /* 2 * Copyright (C) 2017 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.wearable.wear.common.util; 17 18 import android.app.Notification; 19 import android.app.NotificationChannel; 20 import android.app.NotificationManager; 21 import android.content.Context; 22 import android.os.Build; 23 24 import com.example.android.wearable.wear.common.mock.MockDatabase; 25 26 /** 27 * Simplifies common {@link Notification} tasks. 28 */ 29 public class NotificationUtil { 30 31 public static String createNotificationChannel( 32 Context context, 33 MockDatabase.MockNotificationData mockNotificationData) { 34 35 // NotificationChannels are required for Notifications on O (API 26) and above. 36 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 37 38 // The id of the channel. 39 String channelId = mockNotificationData.getChannelId(); 40 41 // The user-visible name of the channel. 42 CharSequence channelName = mockNotificationData.getChannelName(); 43 // The user-visible description of the channel. 44 String channelDescription = mockNotificationData.getChannelDescription(); 45 int channelImportance = mockNotificationData.getChannelImportance(); 46 boolean channelEnableVibrate = mockNotificationData.isChannelEnableVibrate(); 47 int channelLockscreenVisibility = 48 mockNotificationData.getChannelLockscreenVisibility(); 49 50 // Initializes NotificationChannel. 51 NotificationChannel notificationChannel = 52 new NotificationChannel(channelId, channelName, channelImportance); 53 notificationChannel.setDescription(channelDescription); 54 notificationChannel.enableVibration(channelEnableVibrate); 55 notificationChannel.setLockscreenVisibility(channelLockscreenVisibility); 56 57 // Adds NotificationChannel to system. Attempting to create an existing notification 58 // channel with its original values performs no operation, so it's safe to perform the 59 // below sequence. 60 NotificationManager notificationManager = 61 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 62 notificationManager.createNotificationChannel(notificationChannel); 63 64 return channelId; 65 } else { 66 // Returns null for pre-O (26) devices. 67 return null; 68 } 69 } 70 } 71