88import android .app .NotificationManager ;
99import android .app .Service ;
1010import android .content .Context ;
11+ import android .content .pm .ServiceInfo ;
12+ import android .os .Build ;
1113
1214import com .pax .market .android .app .sdk .msg .R ;
1315
1416
1517public class NotificationUtils {
1618 private static final int NOTIFICATION_ID = 1002 ;
1719
18- private static final String LIBRARY_PACKAGE_NAME = "com.pax.market.android.app.sdk.msg" ;
20+ public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1 << 0 ;
1921
22+ private static final String LIBRARY_PACKAGE_NAME = "com.pax.market.android.app.sdk.msg" ;
2023 /**
21- *
22- * @param service
23- * @param smallIcon
24- * @param content
24+ * @deprecated This method is deprecated because it does not support the mandatory
25+ * foreground service types required by Android 14 (API 34).
26+ * <b>Migration Guide:</b><br>
27+ * Please migrate to {@link #showForeGround(Service, int, String, int)} to explicitly
28+ * provide the correct service type for your business logic.
2529 */
2630 public static void showForeGround (Service service , int smallIcon , String content ) {
27- NotificationChannel mChannel = null ;
28- NotificationManager notificationManager = ( NotificationManager ) service . getApplicationContext (). getSystemService ( Context . NOTIFICATION_SERVICE );
31+ showForeGround ( service , smallIcon , content , 0 ) ;
32+ }
2933
30- if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
31- mChannel = new NotificationChannel (LIBRARY_PACKAGE_NAME , service .getApplicationContext ().getString (R .string .app_name ), NotificationManager .IMPORTANCE_NONE );
32- mChannel .setSound (null , null );
33- mChannel .enableVibration (false );
34- notificationManager .createNotificationChannel (mChannel );
34+ /**
35+ * @deprecated This method is deprecated because it does not support the mandatory
36+ * foreground service types required by Android 14 (API 34).
37+ * <b>Migration Guide:</b><br>
38+ * Please migrate to {@link #showForeGround(Service, String, int)} to explicitly
39+ * provide the correct service type for your business logic.
40+ */
41+ @ Deprecated
42+ public static void showForeGround (Service service , String content ) {
43+ showForeGround (service , content , 0 );
44+ }
3545
36- Notification .Builder builder = new Notification .Builder (service .getApplicationContext (), LIBRARY_PACKAGE_NAME );
37- builder .setContentText (content );
38- builder .setSmallIcon (smallIcon );
39- builder .setAutoCancel (true );
40- builder .setShowWhen (true );
4146
42- // 这里两个通知使用同一个id且必须按照这个顺序后调用startForeground
43- service .startForeground (NOTIFICATION_ID , builder .build ());
44- }
47+ /**
48+ * Updated method to support Android 14 foreground service types.
49+ *
50+ * @param service The Service instance.
51+ * @param content Content to display in the notification.
52+ * @param foregroundServiceType The type(s) of foreground service,
53+ * e.g., {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_DATA_SYNC}.
54+ */
55+ public static void showForeGround (Service service , String content , int foregroundServiceType ) {
56+ showForeGround (service , PreferencesUtils .getInt (service , SP_SMALL_LOGO_ICON , R .drawable .ic_notificaiton ), content , foregroundServiceType );
57+
4558 }
4659
60+
4761 /**
62+ * Updated method to support Android 14 foreground service types.
4863 *
49- * @param service
50- * @param content
64+ * @param service The Service instance.
65+ * @param content Content to display in the notification.
66+ * @param smallIcon The icon display in the notification.
67+ * @param foregroundServiceType The type(s) of foreground service,
68+ * e.g., {@link ServiceInfo#FOREGROUND_SERVICE_TYPE_DATA_SYNC}.
5169 */
52- public static void showForeGround (Service service , String content ) {
70+ public static void showForeGround (Service service , int smallIcon , String content , int foregroundServiceType ) {
71+
5372 NotificationChannel mChannel = null ;
5473 NotificationManager notificationManager = (NotificationManager ) service .getApplicationContext ().getSystemService (Context .NOTIFICATION_SERVICE );
5574
@@ -61,12 +80,16 @@ public static void showForeGround(Service service, String content) {
6180
6281 Notification .Builder builder = new Notification .Builder (service .getApplicationContext (), LIBRARY_PACKAGE_NAME );
6382 builder .setContentText (content );
64- builder .setSmallIcon (PreferencesUtils . getInt ( service , SP_SMALL_LOGO_ICON , R . drawable . ic_notificaiton ) );
83+ builder .setSmallIcon (smallIcon );
6584 builder .setAutoCancel (true );
6685 builder .setShowWhen (true );
6786
68- // 这里两个通知使用同一个id且必须按照这个顺序后调用startForeground
69- service .startForeground (NOTIFICATION_ID , builder .build ());
87+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) { // API 34
88+ service .startForeground (NOTIFICATION_ID , builder .build (), foregroundServiceType );
89+ } else {
90+ service .startForeground (NOTIFICATION_ID , builder .build ());
91+ }
7092 }
7193 }
94+
7295}
0 commit comments