更新时间:2022-03-21 GMT+08:00
自定义通知
接口描述
该接口用于自定义前台通知
注意事项
- 使用自定义通知将彻底替换默认通知,需要对应用进行充分测试,否则可能影响到前台服务的拉起。
方法定义
/**
* 设置自定义通知
* @param customNotification 自定义通知对象 * @param notificationId 通知id
*/
void setCustomNotification(Notification customNotification, int notificationId);
参数描述
参数 |
是否必须 |
类型 |
描述 |
---|---|---|---|
customNotification |
是 |
Notification |
自定义通知对象 |
notificationId |
是 |
int |
自定义通知id |
示例代码
shareServiceController.setCustomNotification(createNotification(), GlobalConstant.NOTIFICATION_ID); private Notification createNotification() { // Android8.0以上的系统,新建消息通道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 用户可见的通道名称 String channelName = getString(R.string.air_presence_app_name); // 通道的重要程度 int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel notificationChannel = new NotificationChannel(GlobalConstant.CHANNEL_ID, channelName, importance); NotificationManager notificationManager = getSystemService(NotificationManager.class); if (notificationManager != null) { notificationManager.createNotificationChannel(notificationChannel); } } NotificationCompat.Builder builder = new NotificationCompat.Builder(this, GlobalConstant.CHANNEL_ID); // 通知小图标 builder.setSmallIcon(R.drawable.ic_launcher); // 通知标题 builder.setContentTitle(getString(R.string.ideashare_return_application)); // 通知时间 builder.setWhen(System.currentTimeMillis()); // 设定启动的内容 Intent activityIntent = new Intent(this, ProjectionActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, GlobalConstant.JUMP_BACK_CODE, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); // 通知按钮 Intent stopShareIntent = new Intent(this, NotificationReceiver.class); stopShareIntent.setAction(NotificationReceiver.ACTION_STOP_PROJECTION); PendingIntent broadcastIntent = PendingIntent.getBroadcast(this, GlobalConstant.STOP_SHARE_CODE, stopShareIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.addAction(GlobalConstant.NO_ICON, getString(R.string.air_presence_stop_share), broadcastIntent); // 创建通知并返回 return builder.build(); }
父主题: 业务接口(主动调用)