Android10 NotificationCompat.Builder使用

1.弃用构造函数

@Deprecated 
public Builder(android.content.Context context)

2.新方法使用

参考链接

参考链接:https://www.cnblogs.com/chunshu/p/10317960.html

可用构造函数

public Builder(@NonNull android.content.Context context,
               @NonNull String channelId)
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    
    

        Notification noti = new NotificationCompat.Builder(this, "channelid1")
                .setContentTitle("标题")
                .setContentText("Hello Content.")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //高版本需要渠道
        if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
    
    
            //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);
            //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,通知才能正常弹出
            manager.createNotificationChannel(notificationChannel);
        }

        manager.notify(1, noti);
        return super.onStartCommand(intent, flags, startId);
    }

3.结果

Notification in service

猜你喜欢

转载自blog.csdn.net/weixin_37627774/article/details/109204444