当前位置: 代码迷 >> 综合 >> Android 通知全适配
  详细解决方案

Android 通知全适配

热度:18   发布时间:2024-02-10 23:43:55.0

 


/*** 创建通知* @param context* @param message*/private static final String CHANNEL_ID = "hou2020";private static final String MyGroupId = "houGroupId2020";public static void createNotification(Context context, NotificationMessage message) {if (message == null) return;NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);if (nm == null) return;int notificationId = message.notificationId;String title = message.notificationTitle;String content = message.notificationContent;String largeIcon = message.notificationLargeIcon;String smallIcon = message.notificationSmallIcon;//        Intent intent = new Intent(context, GuideActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(context, 12, intent, PendingIntent.FLAG_CANCEL_CURRENT);Notification.Builder builder = new Notification.Builder(context).setContentTitle(title)
//                .setSubText(context.getResources().getString(R.string.app_name)).setContentText(content).setAutoCancel(true).setSmallIcon(R.mipmap.ic_launcher)
//                .setContentIntent(pendingIntent).setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {builder.setChannelId(CHANNEL_ID);NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup(MyGroupId, "自定义通知组");nm.createNotificationChannelGroup(notificationChannelGroup);NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "自定义通知", NotificationManager.IMPORTANCE_HIGH);notificationChannel.setGroup(MyGroupId);notificationChannel.enableLights(true);notificationChannel.enableVibration(true);
//                notificationChannel.setSound("android.resource://包名/raw/铃声文件", null);    //设置自定义铃声nm.createNotificationChannel(notificationChannel);}DLog.d(TAG, "createNotification: " + notificationId);nm.notify(notificationId, builder.build());}

 

  相关解决方案